开发者

Can This Code Compile? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicates:

C++ void return type of main()

What is the proper declaration of main?开发者_如何转开发

Simple question, really.

My friend and I are perusing the Powerpoint slides of a professor we are supposed to be hearing next semester. It will be a Java course. For some reason, he has this C++ code snippet

#include <iostream.h>
main ()
{ cout << "Hello, World\n"; }

I have told my friend, "No, this won't work with any modern C++ compiler."

My question is now, can this compile at all?


It could, sure.

Consider, for example, if <iostream.h> was a header with the following contents:

#include <iostream>
using std::cout;
#define main int main


That is not standard C++. The list of issues is quite long for a piece of code that short... probably because it comes from ages ago and an old non-conforming compiler.

  • The proper name of the include header is #include <iostream>, the .h was dropped during the ANSI standarization.

  • Types must be explicitly stated in C++. You cannot declare a function without return type and get a default int (that is C, not C++).

  • The standard signatures for main are: int main(), and int main( int argc, char** ) (implementations can provide extra arguments, but the return type must be int)

  • cout is defined inside the std namespace and cannot be used without qualification unless you add a using.

The equivalent code in proper C++ would be

#include <iostream>
int main() {
   std::cout << "Hello world\n";
}


This will compile, though any decent compiler should raise a warning. Since you're using #include <iostream.h>, the compiler assumes that this is old code and compiles it in backwards-compatible mode. On my machine, gcc says:

In file included from /usr/include/c++/4.2.1/backward/iostream.h:31,
                 from oldcpp.cpp:1:
/usr/include/c++/4.2.1/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated header. 
Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ 
standard. Examples include substituting the <X> header for the <X.h> header for C++ 
includes, or <iostream> instead of the deprecated header <iostream.h>. 
To disable this warning use -Wno-deprecated.

But it still compiles it fine. On running the code, I get exactly what is expected: i.e.:

Hello, World


Is this even worth questioning? Why write corner-case C/C++? What's wrong with keeping with the standards? main() is the entry point of the program, and the OS expects a return code from the program. Now there are arguments whether void main() is acceptable, but why even defend it? Just use int main() and be a happy dude that writes good code:

#include <iostream>

int main(int argc, char* argv[]) {
    std::cout << "Hello World!\n";
    return 0;
}

I start every program with this (without the HW! then) and never ever had any issues.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜