Reputation: 9393
I have heard people saying the return type of main
is strictly int
int main(){
printf("hello world");
return 0; // returning 0 to main indicates successful execution
}
Instead I tried simply return
.
int main(){
printf("hello world");
return; //
}
It does not raise any warning or error. I wonder does it return 0
or some undefined value like in JavaScript?
I was reading this question return false the same as return? that says return
and return false
both are different in JavaScript. Is it the same in C?
Upvotes: 0
Views: 3204
Reputation: 41503
(NOTE: This answer is incorrect. See Jens' answer.)
The C standard specifies that, in the main()
function, an empty return, or falling off the end, is the same as return 0
. For all other functions, it's not gonna work.
Upvotes: 0
Reputation: 78943
The C standard explicitly states as a constraint:
A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.
and main
is no exception from that. So no this is not allowed by the standard.
What is allowed by the standard as an exception for main
compared to other functions is that control may drop out of main
at the end without having an explicit return
statement. But these two things are not the same.
Upvotes: 7