C ++中最短的程序 [英] Shortest Program in c++
问题描述
我最近遇到了可以编译的这段代码,但是运行时出现了段错误(g ++).这是topcoder的原始链接 topcoder配置文件
I came across this code recently that compiles, but runs with segmentation fault(g++). Here's the original link from topcoder topcoder profile
#include <iostream>
int main = ( std::cout << "Hello world!\n", 42 );
这也可以编译
int main=0;
有人可以解释该程序中发生了什么.使用g ++
Can someone explain what's happening in this program. Using g++
推荐答案
这都是愚蠢的游戏.这两个程序都违反了程序应包含一个称为 main
的全局函数"(3.6.1p1)的要求.这些程序可能会愚弄一些编译器,因为它们定义了符号 main
,但是该符号根本不是函数!难怪当运行时尝试使用该 main
符号作为函数时,其中至少有一个崩溃.
This is all silly games. Both programs violate the requirement "a program shall contain a global function called main
" (3.6.1p1). Those programs may fool some compilers because they define a symbol main
, but that symbol is not a function at all! No wonder at least one of them crashes when the runtime tries to use that main
symbol as a function.
托管实现中最短的有效C ++ 03程序:
The shortest valid C++03 program in a hosted implementation:
int main(){}
这篇关于C ++中最短的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!