NO

Author Topic: so why do I write int main(void){} and not int main(){}?  (Read 2839 times)

ranadas

  • Guest
so why do I write int main(void){} and not int main(){}?
« 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?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: so why do I write int main(void){} and not int main(){}?
« Reply #1 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:
Code: [Select]
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):
Code: [Select]
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.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: so why do I write int main(void){} and not int main(){}?
« Reply #2 on: November 28, 2017, 01:55:36 PM »
C99
Quote
ISO/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
May the source be with you