Pelles C forum

C language => Beginner questions => Topic started by: ranadas on November 28, 2017, 12:34:19 PM

Title: so why do I write int main(void){} and not int main(){}?
Post by: ranadas on November 28, 2017, 12:34:19 PM
Is this a rule as specified by the latest C11?
If so then what is the reason behind such kind of definition?

I mean life still works with int main(){} , then what was the necessity to specify int main(void){}?

Is it like a rule or any reason behind it??

Is the reason being tokenization or something?
Title: Re: so why do I write int main(void){} and not int main(){}?
Post by: frankie on November 28, 2017, 01:29:12 PM
There is a formal point and a practical point.
Formally ISO/IEC 9899:2011 §5.1.2.2.1 Program startup says:
Quote
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 and argv, 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 (thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.); or in some other implementation-defined manner.

Practically a declaration with empty parenthesis is interpreted as a __cdecl function, and can create calling convention problems when writing code mixed with __stdcall, or other, calling conventions.
Title: Re: so why do I write int main(void){} and not int main(){}?
Post by: TimoVJL on November 28, 2017, 01:55:36 PM
C99 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)
QuoteISO/IEC 9899:TC2 Committee Draft — May 6, 2005 WG14/N1124
5.1.2.2.1 Program startup
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 and argv, 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; 9)
or in some other implementation-defined manner