7 ms·
The example on the bottom can hardly be called C++ #include "stdio.h" void main() { printf( "Hello world!\n" ); getchar(); } - main() should
by Ives 15y ago
The example on the bottom can hardly be called C++
#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()
- __rkaup__ 15y agoI think the STL was too buggy to be used much back in those days. Besides, there's nothing wrong with C style C++. 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.
- personalcompute 15y agoiostream is not part of the STL, just the standard library.
- voyvf 15y agoThank you for pointing that out. It appears that the author of the article subscribes to the atrocious concept of C/C++, rather than realizing that they're two separate languages.
- SamReidHughes 15y ago- Who cares - No - No And you didn't even complain about "stdio.h".
- msbarnett 15y ago> - Who cares 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.
- dkersten 15y agoIf you call it C++, then you should care.
- SamReidHughes 15y agoNot really. People who feel the need to point out a use of void main every time they see it have nothing important to talk about.
- msbarnett 15y agoOn the other hand, people who write void main clearly have nothing to teach about C++, because that plainly isn't C++.
- SamReidHughes 15y agoApparently they do, because they're teaching it. But I guess you don't care about that, it's more important that you inform everybody about this distinction whereby you have a definition of C++ that requires more rigorous adherence to one of the standards, because it's very important that people be informed of this. We wouldn't want beginners to write programs that use "void main" and work perfectly fine. For that would be bad. No, this tutorial should be retitled. It should be retitled to "Game Programming with a language that's not exactly C++ but is acceptable to Microsoft's C++ compiler." Because that's a correct title. It would have been a perfectly fine tutorial that would introduce students to C++ programming, but right now it's horrible. 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.
- Jach 15y agoYou're right that it should be int main() to be valid C++: 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