Consider the following two definitions of main().
int main() { /* */ return 0; } |
and
int main(void) { /* */ return 0; } |
What is the difference?
In C++, there is no difference, both are same.
Both definitions work in C also, but the second definition with void is considered technically better as it clearly specifies that main can only be called without any parameter.
In C, if a function signature doesn’t specify any argument, it means that the function can be called with any number of parameters or without any parameters. For example, try to compile and run following two C programs (remember to save your files as .c). Note the difference between two signatures of fun().
// Program 1 (Compiles and runs fine in C, but not in C++) void fun() { } int main(void) { fun(10, "GfG", "GQ"); return 0; } |
The above program compiles and runs fine (See this), but the following program fails in compilation (see this)
// Program 2 (Fails in compilation in both C and C++) void fun(void) { } int main(void) { fun(10, "GfG", "GQ"); return 0; } |
Unlike C, in C++, both of the above programs fails in compilation. In C++, both fun() and fun(void) are same.
So the difference is, in C, int main() can be called with any number of arguments, but int main(void) can only be called without any argument. Although it doesn’t make any difference most of the times, using “int main(void)” is a recommended practice in C.
Exercise:
Predict the output of following C programs.
Question 1
#include <stdio.h> int main() { static int i = 5; if (--i){ printf("%d ", i); main(10); } } |
Question 2
#include <stdio.h> int main(void) { static int i = 5; if (--i){ printf("%d ", i); main(10); } } |
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Similarities and Difference between Java and C++
- Difference and Similarities between PHP and C
- Difference Between malloc() and calloc() with Examples
- What is the difference between single quoted and double quoted declaration of char array?
- Difference between ++*p, *p++ and *++p
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- What is the difference between "char a" and "char a[1]"?
- What is the difference between printf, sprintf and fprintf?
- Difference between getc(), getchar(), getch() and getche()
- What’s difference between “array” and “&array” for “int array[5]” ?
- What's difference between char s[] and char *s in C?
- Difference between C structures and C++ structures
- Difference between Structure and Union in C
- Difference between strlen() and sizeof() for string in C
- Difference between const char *p, char * const p and const char * const p
- Difference between %d and %i format specifier in C language
- Difference between Relational operator(==) and std::string::compare() in C++
- Difference between std::remove and vector::erase for vectors
- Difference between float and double in C/C++
- Difference between fork() and exec()

