Is it fine to write void main() or main() in C/C++?
In C programming language:
The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main() function has no definition or legitimate usage and can occasionally deliver meaningless results or errors. However, main() denotes the main function, which accepts no parameters and returns an int data type.
In C++ programming language:
The default return type of main function in the C++ programming language is "void," which indicates that main() will not return anything.
The syntax for void main() function is as follows:
void main(){
/* statement-1 */
/* statement-2 */
}
The syntax for the int main() function, which is a complying implementation, is as follows:
int main(){
/* statement-1 */
/* statement-2 */
}
Syntax in C++ programming language:
int main(int argc, char*argv[ ]){
/*optional command line arguments argc and argv*/
/*statement-1*/
/*statement-2*/
}
The int returned by the main() function allows a program to return a value to the computer that called it. For example, the process always terminates with a result value that the kernel system gathers in Linux Operating System. The return value is discarded for systems that do not provide such a feature.
Therefore, "void main()" is neither valid in the C++ programming language nor in the C programming language.
Note: Even if the C or C++ compiler supports void main(), avoid it at all costs. It is incorrect. In the C++ programming language, the main() function does not need to include a specific return statement. In that situation, the value returned is "zero," indicating that the execution was successful.
Take a look at the following C++ programming language example:
Program Code: 1
/* CPP program to show the main() function without return statement */
#include <iostream>
using namespace std;
int main(){
cout<< “ Hello World!\n ”;
cout<< “ This code returns the int value 0\n ”;
}
Output:
Program Code: 2
/* CPP program to show the main() function with return type */
#include <iostream>
using namespace std;
main(){
/* default return type of main() function in C++ is int data type */
cout<< “Hello World!\n”;
cout<< “This code will return int value\n”;
return 0;
}
Output:
There is no error in the above code. If you write the whole code without any errors, even without a return type at the end of the main( ) block, the compiler will add a return statement with the appropriate datatype at the conclusion of the program for you.
Note: In the ISO C++ or C99 versions, you cannot remove the type from a declaration. In other words, unlike the C89 version and ARM C++, when a type is missing from a declaration, the int data type is ignored.
An example of the int main() function in the C programming language:
/* C program to show the main() function without return statement */
#include <stdio.h>
int main(){
printf(“Hello World!\n”);
printf(“This returns int value 0\n”);
}
Output:
An example of C program to show the main() function with return type
/* C program to show the main() function with return type */
#include <stdio.h>
main(){
printf(“Hello World!\n”);
printf(“This returns int value\n”);
return 0;
}
Output:
Some Differences between void main() and int main():
The main() function works similarly to other functions. It also accepts parameters and returns a value. One thing to remember is that the program begins its execution with the main() function. When the operating system or the compiler calls the function, some value is returned by the main() block, and it is sent back to the operating system. The void main() function implies that no value can be returned. The int main() method specifies that it can return data of the integer type.
When the program is simple and will not stop the execution before the last line of the code or when the code is error-free, we may utilize the void main() function.
However, if we wish to leave the application using the exit() function, we must return some int values such as "zero" or "non-zero." In that case, the void main() function will fail to work.
Conclusion:
To conclude, it is never a good option to use void main() or just main() since it violates standards. However, certain compilers may allow it. As a result, it is preferable to use int main() rather than void main().