Difference Between int main and void main in C/C++

Difference Between int main and void main in C/C++

4 mins readComment
Anshuman
Anshuman Singh
Senior Executive - Content
Updated on Mar 13, 2024 15:39 IST

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!

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

Free
2 hours
– / –
40 hours
– / –
45 hours
Free
13 hours
Free
3 hours
₹1.3 K
2 weeks
₹2.2 K
4 weeks
– / –
1 month
₹2.4 K
50 hours
₹3 K
50 hours

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
}
Copy code

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
}
Copy code

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.

Learning If Statement in C
Learning If Statement in C
The if statement in C is a control structure that evaluates a condition. If the condition is true, it runs the associated code; if false, the code is ignored. It’s...read more

Addition of Two Numbers in C
Addition of Two Numbers in C
Delve into C programming with a foundational task: adding two numbers. This guide walks you through each step, from declaring variables to getting the output, making it a perfect starting...read more

Learning Loops in C
Learning Loops in C
In C programming, a loop is a control structure that allows a set of instructions to be executed repeatedly based on a specified condition. Loops provide an efficient way to...read more

Control Statements in C | Meaning and Types
Control Statements in C | Meaning and Types
Control statements in C are used to determine the order in which the instructions within a program are executed. They are of three types: Selection, Iteration & Jump statements. Think...read more

Relational Operators in C | About and Types
Relational Operators in C | About and Types
Relational operators in C, such as ā€œless thanā€, ā€œgreater thanā€, ā€œless than or equal toā€, ā€œgreater than or equal toā€, ā€œequal toā€, and ā€œnot equal toā€, play a pivotal role...read more

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.

About the Author
author-image
Anshuman Singh
Senior Executive - Content
Anshuman Singh is an accomplished content writer with over three years of experience specializing in cybersecurity, cloud computing, networking, and software testing. Known for his clear, concise, and informative wr Read Full Bio