+ 8

Why do we write "return 0;" and not anything else (for example NULL) after the int main() function. And if I wanna return exactl

29th Oct 2016, 11:30 AM
Rebeka Asryan
Rebeka Asryan - avatar
4 Réponses
+ 8
The return value of main can be checked in system, when program ended. So, is there such convention. return 0; means the program ended OK return other integer value means the program ended with any error Of course you can define other meaning of returning integer value, and describe it it your program documentation and help Return anything else than integer is not a clever idea, it can cause any mash..
29th Oct 2016, 5:40 PM
Petr Hatina
Petr Hatina - avatar
+ 2
Well it depends on the return data type of the function which has return statement in it. We usually write return 0 if he have declared main to return an integer as in int main(). That means that the program ended correctly. We could also write void main() and return no value though its not according to standards. Anyway even if you don't return anything the compiler will "add" a return 0 for you. NULL it's not allowed
2nd Nov 2016, 8:40 PM
Olsi
0
I found it very confusing, that 0 is usually returned, when a function worked correctly. Since 0 stands for false in conditions... Also MPI functions return 0 if successful for example. Weird convention.
2nd Aug 2018, 2:02 PM
Matthias
Matthias - avatar
- 2
I upvoted you petr just so you get the new badge 😁
7th Nov 2016, 9:57 AM
Eric Waweru
Eric Waweru - avatar