Is the main function callable in C++ or C [duplica

2020-07-27 03:58发布

Can I call the main method in C/C++ from other functions. it seems to work but I do not know if it is a good software design in c++. please tell me the pros and cons ? Thank you.

标签: c++ c c++11
3条回答
ら.Afraid
2楼-- · 2020-07-27 04:32

In C you can. In C++ you can't.

Quoting the C++ standard (§3.6.1.3):

The function main shall not be used within a program.

There's nothing in the C standard forbidding calling main.

Whether or not calling main is good design is quite opinion-based, but usually one would be better off using a loop instead.

查看更多
看我几分像从前
3楼-- · 2020-07-27 04:33

According to C++ standard

5.2.2.9 "Recursive calls are permitted, except to the function named main"

查看更多
虎瘦雄心在
4楼-- · 2020-07-27 04:48

You've already determined it is possible. However, it makes your entire program recursive. It also could make your code a little harder to understand.

So it's really hard for me to imagine any pros for this.

查看更多
登录 后发表回答