Table of contents
1.
Introduction
2.
Differences between int main() and void main()
3.
 
4.
 
5.
 
6.
What is int main()?
6.1.
What are argc and argv arguments?
6.2.
Using int main() Function
6.3.
C++
7.
What is int main(void)?
7.1.
Why is the void main() non-standard and not recommended?
7.2.
Using void main() Function
7.3.
C++
8.
Frequently Asked Questions
8.1.
What is the difference between int main() and void main()?
8.2.
Which is better to use int main() or void main()?
8.3.
What is int main () used for?
8.4.
Why does C++ have int main?
8.5.
What is void main () in C?
8.6.
What is int main() in C?
9.
Conclusion
Last Updated: Sep 2, 2024
Medium

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

Author Gaurav Singh
0 upvote

Introduction

The key difference between “int main()” and “void main()” is the “int main()” function as it gives us a return value in the form of an integer which lets the user know if the program has run successfully or not. At the same time, the “void main()” function does not return value. which may not comply with the C++ standard.

Difference Between Int Main And Void Main

In this blog, we will be discussing the difference between int main() and void main(). Apart from that, we will understand the significance of both of them in CPP compilers and their uses for different purposes with some examples.

Also see, Abstract Data Types in C++

Differences between int main() and void main()

The key difference between “int main()” and “void main()” in C++ is their return type. “int main()” returns an integer value to indicate the status of program execution, while “void main()” doesn't return any value, 

Key Point

int main()

void main()

Return Type

int

void

Return Value

It returns an int value

It does not return a value

Usage

Standard and portable

Non-standard and risky

Arguments

Can take argc and argv arguments

Same as int main(), can also take void as an argument

Usage in code

Can be used to indicate errors

Typically not used for errors

Convention

Standard convention in C and C++

Non-standard and discouraged

Required by standard

Required by C and C++ standards

Not required by any standard

Potential issues

Omitting the return value can cause warnings or errors

Using void main() can cause compatibility issues and is not supported in some compilers

Compatibility

Compatible with most compilers

Not supported in some compilers

Required by standard

Required by C and C++ standards

Not required by any standard

 

 

 

 

Also read -  File Handling in CPP

What is int main()?

The int main() defines the entry or the starting point of the C/C++ program code. Here int is a keyword that tells us about the integer data type. So when one runs the file on their local computer, the operating system executes the main () function. Any code that is written to solve a particular question goes into this main() function which the OS system when calls it, it runs the code.

Mainly we use this way of defining the function in the C/C++ program. The “int” in the int main() suggests that the “main()” function would return the integer type as an output to the operating system when the program is terminated. When the integer value is returned, it means that the program has been completed. If the return value is 0, it indicates that the program has terminated successfully, while any value other than 0 indicates an error.

What are argc and argv arguments?

Any primary function can be called using command line prompting. The main process takes two parameters, out of which one is the argc argument, which is an integer type, and the other is the argv parameter, which is a character type. The first parameter, argv, also called argument count, is an integer that tells us about the number of arguments that were entered in the command line before the program started. The second parameter, argv, also called the argument vector, is an array of pointers that point to the character's objects.

Using int main() Function

  • C++

C++

#include <stdio.h>


int main() {
   printf("Hello, World!\n");
   return 0;  // indicates successful termination of the program
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Hello, World!
You can also try this code with Online C++ Compiler
Run Code

The above code in C language tells us about implementing the int main() function to print the “Hello World” string. The return 0 statement indicates that the program has ended successfully in the main() function.

What is int main(void)?

The void main() function returns no return value when the program is successfully terminated. There is an empty data type. It is used when one doesn’t want to return any value to the main function. In CPP, void main() is one of the ways to define the entry point but in a non-standard way.

Why is the void main() non-standard and not recommended?

Most OS return a value to the user or the calling process to understand if the program was successful. This is useful in scripting, also. To identify if the function was executed and if any error occurred. Thus, it is not used much, is non-standard, and specifically not recommended. One more reason why it is not recommended to use is that it can cause compatibility issues with various other compilers. Also, it is not recommended because it is not in the standard CPP language.

Using void main() Function

  • C++

C++

#include <stdio.h>  
#include <conio.h> 
void main() 

printf (" Coding Ninjas");
}
You can also try this code with Online C++ Compiler
Run Code

Output

Coding Ninjas

Frequently Asked Questions

What is the difference between int main() and void main()?

It is one of the ways to initialize the function where the return type is int when the program is successfully terminated. If the return value is 0, then the program is successfully terminated, while other values indicate there was some error. It is used with the main function, although it does not return any value to the OS when the program is terminated. We use this when one doesn’t want any return values at the end of the program.

Which is better to use int main() or void main()?

The int main() function is much better than the void main() function as it gives us a return value in the form of an integer which lets the user know if the program has run successfully or not. At the same time, the void function does not return anything.

What is int main () used for?

int main() is the entry point of C and C++ programs. It's where program execution begins, and it returns an integer value to indicate the status of program termination to the operating system.

Why does C++ have int main?

C++ uses int main() as the standard function signature for the entry point of a program to ensure compatibility with C. It returns an integer to indicate the status of program execution.

What is void main () in C?

In C, void main() is a non-standard way to declare the entry point of a program. It indicates that the function returns nothing (void), which is not compliant with the C standard.

What is int main() in C?

int main() in C is the standard entry point of a program. It returns an integer value to indicate the status of program execution to the operating system, adhering to the C standard.

Conclusion

This blog discussed the int main() function and void main(). We separately saw what are int main() and void main() as both are used for the same purpose, but int main() has better functionality when it comes to successfully executing programming. The data type in the int main() function return is an integer, while the void main() function returns a void value. The two parameters that can be used along with the int main() function are the argc and argv parameters. We also saw why void main() function is not recommended and non-standard. And finally we understood the difference between int main() and void main(). 
 

More information on Strings arguments in Java, void function, and main function in CPP can be found in the below blogs on Coding Ninjas:

Hope this article helped you in your learning journey.

Check out some of the amazing Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingBasics of CBasics of JavaComputer Networks, etc. along with some Contests and Interview Experiences only on Coding Ninjas Studio

Happy Learning Ninja!

Live masterclass