Consider the following two definitions of main().
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().
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.
Predict the output of following C programs.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Difference between C and C#
- Difference between C and C++
- Difference between GCC and G++
- Difference between ++*p, *p++ and *++p
- Difference between Python and C++
- Difference between scanf() and gets() in C
- Difference between while(1) and while(0) in C language
- Difference and Similarities between PHP and C
- Difference between C and Python
- Difference between Keyword and Identifier
- Difference between Private and Protected in C++ with Example
- Difference between Inheritance and Polymorphism
- Difference between Abstraction and Encapsulation in C++
- Difference Between Constructor and Destructor in C++
- Similarities and Difference between Java and C++
- Difference between Inline and Macro in C++
- Difference between namespace and class
- Difference between Java and C language
- Difference between Structure and Array in C
- Difference between strncmp() and strcmp in C/C++