Reputation: 129
void main()
{
printf("hi\n");
return 0;
}
Why does the compiler give no error when I'm returning a value from the function main with return type void?
Upvotes: 0
Views: 3242
Reputation: 2266
Either you are, or your compiler is, doing something wrong. You can't return a value from a void function. The compiler should emit a warning, at the least.
Upvotes: 0
Reputation: 106142
No. It can't. You are doing wrong. You can't return anything from a function whose return type is void
. Your compiler should give a warning:
[Warning] 'return' with a value, in function returning void [enabled by default]
void main
is obsolete now. The standard says about the definition of main
.
1 The function called at program startup is named
main
. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used, as they are local to the function in which they are declared):int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.
Upvotes: 1
Reputation: 7611
Main is special.
Strictly speaking it should be
int main(int, char**);
If you deviate from that (such as by using void
), the compiler will likely throw a warning (if you have warnings turned on) but produce valid code anyway.
EDIT: Apparently int main()
is also valid.
Upvotes: 0