main()在C和C++中定义函数的正确(最有效)方法是什么- int main()或void main()- 为什么?如果int main()那么return 1或return 0?
这个问题有很多重复,包括:
main()函数的有效签名是什么?main()函数void main()和int main()?main()在C++中的签名main()? - 对于C++,确实有一个非常好的答案.main()C语言中的函数样式main()C中的方法类型int main()vs void main()在C中有关:
考虑以下计划:
#include <iostream>
int main = ( std::cout << "C++ is excellent!\n", 195 );
Run Code Online (Sandbox Code Playgroud)
在Windows 7操作系统上使用g ++ 4.8.1(mingw64),程序编译并运行正常,打印:
C++非常棒!
到控制台.main似乎是一个全局变量而不是一个函数; 如果没有该功能,该程序如何执行main()?这段代码是否符合C++标准?程序的行为是否定义明确?我也使用了该-pedantic-errors选项,但该程序仍然编译并运行.
我找到了在quora上做到这一点的方法,但我无法理解.
You can achieve this using opcodes.....
const int main[] = { -443987883, 440, 113408, -1922629632, 4149, 899584, 84869120, 15544, 266023168, 1818576901, 1461743468, 1684828783, -1017312735};
Now follow below steps....
Save that in hello.c
then gcc -o hello hello.c
then ./hello
Hola..... Hello World!
Run Code Online (Sandbox Code Playgroud)
操作码将根据其架构特定于特定硬件和控制器.如果我理解正确,那么对于不同的硬件和平台,操作码将是不同的.所以这不是一般的解决方案.但是我试图在IDEone上编译它并且它给出了运行时错误.有没有通用的解决方案?