using Return 0; or not. whats the difference actually? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

using Return 0; or not. whats the difference actually?

When i use return 0; the output is just as same as when i dont use return 0; then why we should use it?

11th Jun 2018, 11:10 AM
Sakkhor Chowdhury
Sakkhor Chowdhury - avatar
4 Answers
+ 6
The standard says that it is OK, to omit the return statement for main(). http://www.stroustrup.com/bs_faq2.html#void-main
11th Jun 2018, 11:30 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
If a function is declared to return a value and fails to do so, the result is undefined behavior (in C++) When we don't declare return 0. One possible result is seeming to work. As an aside, in C, you wouldn't actually have undefined behavior -- in C, you get undefined behavior only if you try to use the return value, and the function didn't specify a value to return.
11th Jun 2018, 11:30 AM
Akash Pal
Akash Pal - avatar
+ 4
We return 0 in the main function to indicate to the environment the fact that the program worked as expected. You can return a non-zero value to indicate an error. Generally, if you omit it, the compiler will append it at the end of the main function.
11th Jun 2018, 11:30 AM
Vlad Serbu
Vlad Serbu - avatar
+ 1
14th Jun 2018, 6:43 AM
Sakkhor Chowdhury
Sakkhor Chowdhury - avatar