I started studying c programing.while learning some basic programs I got a doubt that why we r using "return 0" at the end of the program even if I am not writing that return 0 also I am getting correct outputs ( for basic programs). I want to know... 1) what is the need of using? 2)if we won't write what will happend?
-
1http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – Benjamin Trent Sep 26 '14 at 18:01
1 Answers
Most function that you have in a c program has a return type. and by return type i mean some data to send back to something that has called that function. this caller can be another function, or even the operating system.
now when you write a c program yo would notice it has a main() function, inside which resides the code that one has written. this main function is described something like this
int main()
or
void main()
in this declaration, you can notice the int
(for integer) and void
for, well nothing. this defines what type of data the function would return.
so at the end of every c program the return 0
is like a command saying to return the integer 0 to the caller (in this case the C runtime). there are a predefined integers which has a predefined meaning. like here 0
means executed without any error. usually returning 1 means the program executed with some error.

- 12,120
- 6
- 43
- 70
-
Also, *not* returning anything to the operating system with `void main` is actually not an option. A program will *always* return a value. – Jongware Sep 26 '14 at 21:03