
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differentiate Between int main and int main(void) Function in C
The int main Function
The int main function indicates that the program returns an integer value, typically 0, at the end of its execution. A return value of 0 signifies that the program has been executed successfully.
The syntax of int main is as follows ?
int main(){ --- --- return 0; }
Example
Below is a C program for the int main() function without arguments. It uses recursion to decrement a static variable a from 10 to 0, printing its value after each decrement. The main function calls itself until a reaches 0, then returns 0.
#include <stdio.h> int main() { static int a = 10; if (a--) { printf("after decrement a =%d
", a); main(10); } return 0; }
When the above program is executed, it produces the following result ?
after decrement a =9 after decrement a =8 after decrement a =7 after decrement a =6 after decrement a =5 after decrement a =4 after decrement a =3 after decrement a =2 after decrement a =1 after decrement a =0
The int main(void) Function
The int main(void) function indicates that if takes no arguments. If we exclude void in the parentheses, the function can accept any number of arguments.
The syntax of int main(void) is as follows ?
int main(void){ --- --- return 0; }
Actually, both seem similar, but int main(void) is technically better as it clearly indicates that main can only be called without any parameters.
Generally, in C language, if a function signature does not specify any arguments, the function can be called with any number of parameters or without any parameters.
Let's apply the same logic to implement the code for both functions. The only difference lies in their syntax.
Example
This C program uses recursion to decrement a static variable a from 10 to 0, printing its value after each decrement. The main function calls itself until a reaches 0, then returns 0.
#include <stdio.h> int main() { static int a = 10; if (a--) { printf("after decrement a =%d
", a); main(10); } return 0; }
When the above program is executed, it produces the following result ?
after decrement a =9 after decrement a =8 after decrement a =7 after decrement a =6 after decrement a =5 after decrement a =4 after decrement a =3 after decrement a =2 after decrement a =1 after decrement a =0
If we write the same code for int main() and int main(void) we will get an error. This happens because void indicates that the function takes no parameters.
So, try to remove argument 10 in the main in the above example and compile. Hence, after rectification the above code will be as follows ?
Example
This C program uses recursion to decrement a static variable a from 10 to 0, printing its value after each decrement. The main function calls itself until a reaches 0, then returns 0.
#include <stdio.h> int main() { static int a = 10; if (a--) { printf("after decrement a =%d
", a); main(); } return 0; }
When the above program is executed, it produces the following result ?
after decrement a =9 after decrement a =8 after decrement a =7 after decrement a =6 after decrement a =5 after decrement a =4 after decrement a =3 after decrement a =2 after decrement a =1 after decrement a =0