difference between int main() and int main(void)
Difference between int main()
and int main(void)
in C
In C programming, the main
function is the entry point of a program. It is where the execution of the program begins. The main
function can be declared in two different ways: int main()
and int main(void)
.
The difference between int main()
and int main(void)
lies in the function parameter list.
int main()
: This declaration of themain
function does not specify any parameters. It indicates that themain
function can accept any number of arguments or no arguments at all. This is an older style of declaring themain
function and is still supported in modern C compilers. However, it is considered a bad practice because it does not convey the intention of the programmer clearly. It is recommended to useint main(void)
instead.int main(void)
: This declaration of themain
function explicitly specifies that it does not accept any arguments. It indicates that themain
function does not expect any command-line arguments to be passed to the program. This is the preferred and recommended way of declaring themain
function in modern C programming.
It is important to note that both int main()
and int main(void)
have the same return type of int
, which indicates the exit status of the program.
Here is an example of the int main(void)
declaration:
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}
In this example, the main
function does not accept any arguments and simply prints "Hello, world!" to the console. The program then returns 0, indicating successful execution.