• Welcome to ComeFromChina Community (CFC中文网)! We are the largest Chinese Canadian community forum in Ottawa. Please to participate in discussions, post topics, view images, and access full community features. Join us today! 欢迎来到CFC中文网。请登录以参与讨论、发布主题并查看图片。

精华 C++ -- 002

胡说之

Moderator
注册
2002-01-17
消息
5,167
荣誉分数
6
声望点数
168
Write any small program that will compile in "C" but not in "C++"
 
这样的中吗 :blink:

void main()
{
a();
}

int a()
{
return 0;
}
 
用gcc和g++试验了一下,土图回答正确.

如果把void main该成int main,连warning 都没有.

收入journal了.
 
#ifdef __cplusplus
#error Not Supported
#endif
 
A long long time ago, it was said that the compiler compiles .cpp in C++ and .c in C, and there was also a keyword called extern "C"...
 
这个问题的核心是:有哪个feature, 在C里合法,而在C++里不合法.一般的看法是C++是C的super set, 所以出个牛角尖让大家钻.
 
使用C++里的新保留字做变量名的程序也不可能通过C++ compiler.
 
我给个段子
void main()
{}
这个程序在gcc下能通过,但g++下过不了。
但是在vc6下是能过的。所以上面有个xd说和编译器有关我觉得也是对的。
这个问题我觉得有2种可能,1是和帅哥说的一样,用c++的保留字
2是利用c编译器对程序的要求不严密,而c++编译器下要求严密。
比如楼上的例子,其实c也是要求函数在用之前先定义的。
总之。。。还是说不清楚。
 
void main(){}

我用g++试过,能过,只给了warning

土图写的function a()很重要,如果没在使用前定义,g++不让过.
 
so which means this really depends on the compiler.
i tried this program with g++ on linux, and could not pass.
 
[root@*** tem]# ls
simpc.cpp
[root@*** tem]# cat simpc.cpp
void main()
{}
[root@*** tem]# g++ simpc.cpp -o simpc
simpc.cpp:2: `main' must return `int'
[root@*** tem]# ls
simpc.cpp
[root@*** tem]#
 
后退
顶部
首页 论坛
消息
我的