+ 1

Return 0;

Can someone please provide an in-depth analysis of what exactly happens when the program has to "return 0", i know it symbolizes that a program has ran successfully hence it has to return 0, but my curiosity is in where is it being returned to if not displaying on the screen

12th Dec 2016, 6:14 PM
Lwandiso Ntlanga
Lwandiso Ntlanga - avatar
4 Answers
+ 5
Technically, in C or C++ main function has to return a value because it is declared as "int main" which means "main function should return integer data type" if main is declared like "void main", then there's no need of return 0. Some compilers even accept and compile the code even if u dont write return 0. varies from compiler to compiler. The return value of main is what your program returns to the OS. The OS expects a program to return 0 if everything went right, non-0 when the program got some errors. If you don't know what to return from main, return 0. Having void main or something else is non-standard. For more in-Depth info follow this link http://en.cppreference.com/w/cpp/language/return
12th Dec 2016, 5:24 PM
Vipul Walia
Vipul Walia - avatar
+ 4
a return value is never returned to the screen. you need cout to do that. a return value is a value that can be used just like any other value. now that that is out of the way the return 0 statement is returned bc the return type of your main function is int. the program is the declaration of the main function. the compiler or ide calls the main function just like any other function so it must have a return statement of the correct type or it produces an error
12th Dec 2016, 5:39 PM
NICKALL [EP]
NICKALL [EP] - avatar
+ 2
Thank you!! very much for the response :) I clearly understand it now
12th Dec 2016, 6:19 PM
Lwandiso Ntlanga
Lwandiso Ntlanga - avatar
- 1
remember the fact that ur programs are just main function , so they behave like function too. i.e. they can have arguments , hence (string []args) they can have a return type , hence int main() and return 0 suppose u have many programs dependent on each other, on such cases return values are tailored accordingly. but in beginning phase it's just good habit to have a return with 0, for staying in safe side with various compiler s
12th Dec 2016, 5:47 PM
Morpheus
Morpheus - avatar