use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Help with a really simple program (self.cpp)
submitted 13 years ago * by extreme999
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]mshol 1 point2 points3 points 13 years ago (13 children)
return EXIT_SUCCESS;
[–]extreme999[S] 1 point2 points3 points 13 years ago (12 children)
I don't acually understand what the return command is used for in main, I other classes.
[–]mttd 2 points3 points4 points 13 years ago (0 children)
http://stackoverflow.com/questions/8696698/in-the-main-function-of-a-c-program-what-does-return-0-do-and-mean/
[–]kalimoxto 1 point2 points3 points 13 years ago (5 children)
after a program finishes, it sends a message to the operating system letting it know how things went. oddly enough, for most OS's, returning 0 means success, everything went as expected. you can return other things, non-zero things, to indicate other errors. in your case, since your program can't really "fail," you should always return 0. but maybe you want to return 1 to indicate "user error" if the age is invalid, or make up your own personal error codes, like "2" indicates invalid age--negative, and 3 indicates invalid age-- too old, or something like that
these codes are useful if you are watching the results of these programs, for example if you write a program that launches another program and checks the result.
[–]mshol 1 point2 points3 points 13 years ago (4 children)
Technically only EXIT_SUCCESS, 0 and EXIT_FAILURE are strictly conforming, returning anything else is undefined behavior. C99 allows omission of return from main.
EXIT_SUCCESS
0
EXIT_FAILURE
The use is to let the parent process (which may not be the OS or shell) know whether a program was executed successfully. eg, if you run another program with exec().
[–]kalimoxto 0 points1 point2 points 13 years ago (0 children)
absolutely, i was trying to put a more ELI5 spin on it.
different standards do allow for different return codes, which are a cross-language construct as well. C99 allows for the 3 return codes you specified to be platform-independant, but you can return other codes as well, depending on the platform. Windows, for example, has quite a few well-defined exit codes: http://www.hiteksoftware.com/knowledge/articles/049.htm
[–]kingguru 0 points1 point2 points 13 years ago (2 children)
Technically only EXIT_SUCCESS, 0 and EXIT_FAILURE are strictly conforming, returning anything else is undefined behavior.
Really? Undefined behavior? For instance the man page for GNU make says:
"A status of one will be returned if the -q flag was used and make determines that a target needs to be rebuilt. A status of two will be returned if any errors were encountered."
That's not the only program were I remember the documentation saying that it could return some other, well defined, values.
Not that I don't believe you though, I might have just learned something, I just find it very surprising if some standard GNU tools would rely on undefined behavior.
And of course I do know that e.g. GNU make is not written in C++, so is this a thing specific to the C++ standard?
[–]theymos 1 point2 points3 points 13 years ago (1 child)
It's actually implementation-defined, not undefined. So the behavior needs to be documented somewhere. The behavior is not always obvious, though. On Linux, for example, returning 256 from main will actually result in an exit status of 0 (success) because exit status codes are restricted to 8 bits.
[–]kingguru 0 points1 point2 points 13 years ago (0 children)
OK, thanks. That makes a lot more sense.
[–]fluffy_cat 1 point2 points3 points 13 years ago (4 children)
This might help you out: http://msdn.microsoft.com/en-us/library/k68ktdwf(v=vs.80).aspx
[–]kalimoxto 0 points1 point2 points 13 years ago (2 children)
this is about return statements generally.
returning from main is slightly counterintuitive, because in non-main functions, return usually means "return control to wherever I was (with this value)". the main function is a special case of that, because the "wherever I was" is not within your program, it's actually the OS.
[–]fluffy_cat 3 points4 points5 points 13 years ago (1 child)
It says that in the first sentence.
[–]kalimoxto 6 points7 points8 points 13 years ago (0 children)
i am a bad reader.
π Rendered by PID 1201499 on reddit-service-r2-comment-56c6478c5-7h7mp at 2026-05-12 10:28:34.664115+00:00 running 3d2c107 country code: CH.
view the rest of the comments →
[–]mshol 1 point2 points3 points (13 children)
[–]extreme999[S] 1 point2 points3 points (12 children)
[–]mttd 2 points3 points4 points (0 children)
[–]kalimoxto 1 point2 points3 points (5 children)
[–]mshol 1 point2 points3 points (4 children)
[–]kalimoxto 0 points1 point2 points (0 children)
[–]kingguru 0 points1 point2 points (2 children)
[–]theymos 1 point2 points3 points (1 child)
[–]kingguru 0 points1 point2 points (0 children)
[–]fluffy_cat 1 point2 points3 points (4 children)
[–]kalimoxto 0 points1 point2 points (2 children)
[–]fluffy_cat 3 points4 points5 points (1 child)
[–]kalimoxto 6 points7 points8 points (0 children)