1

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?

Community
  • 1
  • 1
IAE
  • 2,213
  • 13
  • 37
  • 71
  • 11
    Have you tried it? – Falmarri Mar 18 '11 at 22:33
  • 1
    A better duplicate would be [What is the proper declaration of `main()`?](http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main) but it's still not quite a duplicate, I suppose. – James McNellis Mar 18 '11 at 22:38

4 Answers4

21

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
James McNellis
  • 348,265
  • 75
  • 913
  • 977
7

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";
}
Mark B
  • 95,107
  • 10
  • 109
  • 188
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
2

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
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
  • That should not compile in any conforming compiler. C++ does not allow for implicit return types, and the signature of main returns an `int`. Many compilers still allow the obsolete .h headers, but the missing return type should be a show stopper. Then again, g++ seems to accept it by default, but will warn with -Wall or -pedantic – David Rodríguez - dribeas Mar 18 '11 at 22:44
  • There *is* a good reason for this. Though this is not correct according to standard C++, this would have been valid code pre-standardization. To prevent really old code from breaking, most compilers should still compile this with a warning unless explicitly not to. – Chinmay Kanchi Mar 18 '11 at 22:57
  • @dribeas: conforming compilers are permitted to compile non-standard code, provided that they emit a diagnostic. So it's not correct to say it "shouldn't compile". It shouldn't compile *cleanly*. – Steve Jessop Mar 19 '11 at 01:17
1

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.

orlp
  • 112,504
  • 36
  • 218
  • 315