Difference Between int main and void main in C/C++
Have you ever wondered about the difference between int main and void main in C/C++? While int main is standard-compliant, returning an integer to indicate the program's execution status, void main does not return a value and is not standard in C/C++, potentially leading to undefined behaviour. Let's understand more!
In C and C++, int main and void main are two possible signatures for the main function, which is the entry point for a program. However, their use and standard compliance differ significantly between the two languages. In this blog, we will explore these differences in detail!
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
Difference Between int main and void main in C/C++
Below is a table differentiating between int main and void main in C/C++
|   Feature  |  
       int main  |  
       void main  |  
    
|   Standard Compliance  |  
       Complies with the ISO C and C++ standards.  |  
       Not standard in ISO C or C++.  |  
    
|   Return Type  |  
       Returns an integer to the operating system.  |  
       Does not return a value.  |  
    
|   Usage  |  
       Indicates the success or failure of program execution.  |  
       Does not indicate program success or failure directly.  |  
    
|   Portability  |  
       Highly portable across different platforms and compilers.  |  
       It may not be portable and accepted by some compilers with warnings or errors.  |  
    
|   Best Practice  |  
       Recommended and considered best practice in C/C++.  |  
       Generally discouraged in favour of int main.  |  
    
|   Typical Return Values  |  
       0 or EXIT_SUCCESS for success, EXIT_FAILURE for failure.  |  
       N/A  |  
    
|   Compatibility  |  
       Compatible with all standard-compliant compilers.  |  
       Compatibility depends on the compiler and its settings.  |  
    
What is int main?
In int main, the int signifies that the function returns an integer to the operating system upon completion. This return value is often used to indicate whether the program executed successfully or if there was an error. A return value of 0 or EXIT_SUCCESS typically signifies successful completion, while any non-zero value (often EXIT_FAILURE) indicates an error.
For Example,
#include <stdio.h>
int main() {    printf("Hello, world!");    return 0; // Indicates successful execution}
Output
Hello, world!
This example prints "Hello, world!" to the console and then returns 0 to the operating system, indicating that the program executed successfully.
What is void main?
On the other hand, void main specifies that the main function does not return a value. While some compilers might accept void main for certain types of applications (especially embedded systems where returning a value to an operating system is not required), it is not standard in ISO C or C++, and its use is generally discouraged.
For Example,
#include <stdio.h>
void main() {    printf("Hello, world!");    // No return statement needed or allowed}
Output
Hello, world!
This example also prints "Hello, world!" to the console. However, it does not return a value to the operating system. Using void main can lead to undefined behaviour in standard C and C++ environments because the language standards expect an integer return type for the main function.
Thus, It's recommended to use int main instead of void main in C and C++ programs for compatibility with the language standards and to ensure predictable behaviour across different platforms and compilers. The int return type provides a mechanism to communicate the exit status of the program to the operating system, which can be useful for debugging and for scripts or other programs that might call the program and need to check its result.
Explore these C/C++ courses to dive into the details and learn more!
FAQs
What is the standard return type of the main() function in C and C++?
According to the C and C++ standards, the return type of main() should be int. This indicates the program's execution status to the environment it runs in, with 0 typically signifying successful completion.
Why is int main() preferred over void main() in C and C++?
int main() is preferred because it conforms to the standards set by the ANSI C and ISO C++ specifications, which require main() to return an integer. Using void main() is non-standard and should be avoided as it does not return an exit status.
Can void main() be used in any situation?
While some compilers may allow void main(), it is not standard practice and its use is not portable. The C and C++ standards require main() to return an integer to the operating system, so using void main() can result in undefined behavior.
What is the significance of the return value of int main()?
The return value of int main() is used by the operating system to determine the exit status of the program. A return of 0 or EXIT_SUCCESS typically means the program ended successfully, while EXIT_FAILURE (often 1) indicates that the program encountered an error.
What happens if int main() does not explicitly return a value?
In C++, if int main() reaches the end of the function without encountering a return statement, the compiler implicitly inserts return 0;
In C, particularly in C99 and later, the same rule applies. However, it's always good practice to include an explicit return 0; for clarity and to ensure compatibility with all standards and compilers.

 
    



