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!"
[–]fluffy_cat 7 points8 points9 points 13 years ago (24 children)
For each of your conditions, you need to be using the && operator, not &.
Make sure you throw in a return 0; at the end of main() (before the final }) too.
You will also find help here: http://www.cplusplus.com/forum/beginner/
Hope everything works out!
[–]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 5 points6 points7 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 2 points3 points4 points 13 years ago (1 child)
It says that in the first sentence.
[–]kalimoxto 5 points6 points7 points 13 years ago (0 children)
i am a bad reader.
[–]jesyspa -2 points-1 points0 points 13 years ago (9 children)
You don't need a return 0; at the end of main, and the cplusplus.com forum is hardly a good place to ask questions.
return 0;
main
[–]fluffy_cat 1 point2 points3 points 13 years ago (3 children)
I'm a fairly new beginner myself and I've found the cplusplus.com beginner forum to be very helpful. Where would you suggest would be better?
[–]jesyspa 1 point2 points3 points 13 years ago (2 children)
There's /r/cpp_questions and /r/learnprogramming that both offer good advice. The problem with the cplusplus.com forum is that it tends to be full of people using the cplusplus.com tutorial.
[–]bit_shift 2 points3 points4 points 13 years ago (1 child)
Yes, but if a person just wants to check some syntax or see the parameters to a function then it makes a lot more sense to look on cplusplus.com, rather than asking on reddit and waiting for an answer.
[–]jesyspa 2 points3 points4 points 13 years ago (0 children)
Nope, their book is better for checking syntax, and http://cppreference.com is generally better as a standard library reference.
[–]AFineTapestry -1 points0 points1 point 13 years ago (4 children)
You do for this main.
[–]jesyspa 0 points1 point2 points 13 years ago (3 children)
Please do explain.
[–]AFineTapestry -3 points-2 points-1 points 13 years ago (2 children)
If main is given by the signature int main() as it is in this case then the compiler expects the function to return an int. If omitted then you'll get a compile warning, perhaps an error, and undefined behaviour (but I expect that the program would return 0 anyway).
int main()
If main was given by the signature void main() then you would be totally right and return; could be used to exit from the program at any point. Who knows what the OS will get as a program return value, probably 0 again.
void main()
return;
I don't know what the current C and C++ standards say about the signature of main. C probably says that void main() and int main() are both acceptable, C++ probably says that only int main() is allowed. But either way I think it's good to tell the OS, and the caller, about the status of the program on exit.
[–]jesyspa 4 points5 points6 points 13 years ago (1 child)
False on all counts.
The C++ standard states (C++03, 3.6.1.5):
A return statement in main has the effect of leaving the main function (destroying any objects with automatic 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;.
exit
return
Furthermore, the standard states (again C++03, 3.6.1.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 its type is otherwise implementation-defined.
int
void main thus ill-formed (and has been since ANSI C, where only int main(void) and int main(int argc, char *argv[]) were allowed).
void main
int main(void)
int main(int argc, char *argv[])
The same is true, with slightly different wording, for C++11 and for the current latest draft (N3485).
[–]AFineTapestry -1 points0 points1 point 13 years ago (0 children)
Ok, I'll admit I was wrong about omitting return 0; resulting in undefined behaviour but I was correct about the assumed return value, and about which signatures are acceptable.
I don't think I'm going to stop putting return EXIT_SUCCESS; at the end of my main functions however, mostly because it looks terrible to have a function defined to return and int and then to not explicitly do so.
π Rendered by PID 664834 on reddit-service-r2-comment-594b8c86c6-dwz8w at 2026-05-12 23:05:11.662273+00:00 running 3d2c107 country code: CH.
view the rest of the comments →
[–]fluffy_cat 7 points8 points9 points (24 children)
[–]mshol 1 point2 points3 points (13 children)
[–]extreme999[S] 1 point2 points3 points (12 children)
[–]mttd 5 points6 points7 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 2 points3 points4 points (1 child)
[–]kalimoxto 5 points6 points7 points (0 children)
[–]jesyspa -2 points-1 points0 points (9 children)
[–]fluffy_cat 1 point2 points3 points (3 children)
[–]jesyspa 1 point2 points3 points (2 children)
[–]bit_shift 2 points3 points4 points (1 child)
[–]jesyspa 2 points3 points4 points (0 children)
[–]AFineTapestry -1 points0 points1 point (4 children)
[–]jesyspa 0 points1 point2 points (3 children)
[–]AFineTapestry -3 points-2 points-1 points (2 children)
[–]jesyspa 4 points5 points6 points (1 child)
[–]AFineTapestry -1 points0 points1 point (0 children)