+ 2

Why not in most beginner C++ exercises there is no built-in return 0; ?

Hi, how are you, I'm new to programming, I'm starting to practice C++ since I'm starting university this year! I have seen various practice exercises, and so far I have not come across With return 0; And I look at a lot of comments saying that the exercises should be wrong since this variable is not there, and they emphasize that it is something important that should not be forgottenCould you tell me what you mean? So far what I have learned the exercises go like this: #include <iostream> using namespace std; int main ( ) { xxxxxxxxxxxxx xxxxxxxxxxxxx xxxxxxxxxxxxx } (The x's are for example only) Now, I look at comments saying that return 0; goes under int main ( ) { can someone tell me why? This is true? What does it influence and what does said variable mean, thank you very much and I hope Do not disturb, I am new but with too much interest in this and I give it due seriousness.

25th Jan 2023, 1:01 AM
Adrian Ochoa
Adrian Ochoa - avatar
2 Antworten
+ 7
Just adding to what Tibor mentioned, here's part of the C99 standard where it is listed how a program behaves if return statement is omitted from main. https://port70.net/~nsz/c/c99/n1256.html#5.1.2.2.3 "... reaching the } that terminates the main function returns a value of 0... " Since it is well-defined, it is OK to not explicitly return 0 for main.
25th Jan 2023, 2:52 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
You can find the answer in many old questions. int main() --> this function signature indicates that the result of the main function is an integer number. And by convention, return 0 means the program finished execution successfully. https://www.sololearn.com/Discuss/63368/?ref=app https://www.sololearn.com/Discuss/81360/?ref=app https://www.sololearn.com/Discuss/145188/?ref=app https://www.sololearn.com/Discuss/2667389/?ref=app
25th Jan 2023, 1:44 AM
Tibor Santa
Tibor Santa - avatar