Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is int main() { } (without "void") valid and portable in ISO C?

Tags:

People also ask

Is int main valid in C?

Is int main; a valid C program? Yes. A freestanding implementation is allowed to accept such program. main doesn't have to have any special meaning in a freestanding environment.

Is int main void valid?

No. It's non-standard. The standard prototype of main is int main() with the optional command line arguments argc and argv . The int returned by main() is a way for a program to return a value to the system that invokes it.

Can int main () be void main ()?

The void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data. When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main().

Why is main int not void?

The short answer, is because the C++ standard requires main() to return int . As you probably know, the return value from the main() function is used by the runtime library as the exit code for the process.


The C standard specifies two forms of definition for main for a hosted implementation:

int main(void) { /* ... */ } 

and

int main(int argc, char *argv[]) { /* ... */ } 

It may be defined in ways that are "equivalent" to the above (for example, you can change the parameter names, replace int by a typedef name defined as int, or write char *argv[] as char **argv).

It may also be defined "in some other implementation-defined manner" -- which means that things like int main(int argc, char *argv[], char *envp) are valid if the implementation documents them.

The "in some other implementation-defined manner" clause was not in the 1989/1990 standard; it was added by the 1999 standard (but the earlier standard permitted extensions, so an implementation could still permit other forms).

My question is this: Given the current (2011) ISO C standard, is a definition of the form

int main() { /* ... */ } 

valid and portable for all hosted implementations?

(Note that I am not addressing either void main or the use of int main() without parentheses in C++. This is just about the distinction between int main(void) and int main() in ISO C.)