#include "stdio.h"
void main()
{
printf( "Hello world!\n" );
getchar();
}
- main() should return int, not void- std::cout should be used instead of printf
- std::cin should be used instead of getchar()
#include "stdio.h"
void main()
{
printf( "Hello world!\n" );
getchar();
}
- main() should return int, not void- std::cout should be used instead of printf
- std::cin should be used instead of getchar()
Edit: I must be tired: this is a tutorial on C++, and thus should use C++ style. Also, despite the old-style windows, this article is quite new.
2 An implementation shall not predefine the main
function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise
its type is implementation-defined.
But note: 5 A return statement in main has the effect of leaving
the main function (destroying any objects with auto-
matic storage duration) and calling exit with the
return value as the argument. If control reaches the
end of main without encountering a return statement,
the effect is that of executing
return 0;
The other two points I disagree with. =P- No
- No
And you didn't even complain about "stdio.h".
Compilers care, or should. "void main" is illegal per the spec, and if your compiler happens to do anything useful with that, it's purely out of luck.
I was so glad to learn that people here are aware that C++ says main should return int. Because I was sitting here, wondering if somebody on Hacker News knows that main should return int. And then Ives came along to say, "I do!" That resolved my question. But then as a special treat, somebody else came along to point out that the iostreams library is not part of the STL. Or is it? I sure wouldn't want to forget the distinction between The SL and The STL. For they are different things. I think.
Confession time: One time, I learned that I had once learned some C++ from a tutorial that had used "void main." I felt doomed. I had just gotten over the phase where I thought it was okay not to correct somebody when not every part of every clause of every sentence they said was correct. But then I found that I liked using void main. And then before you knew it, I started writing <stdlib.h> instead of <cstdlib>, and then I used C99 header files, and then I used variable length arrays, and I used vector::data, and I liked it. The next thing you know I found myself running two threads at the same time, wouldn't you believe it? Now I know that what I was using was not C++, but that what I used was an abomination.