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++
#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++
#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 Algorithms, Competitive Programming, Basics of C, Basics of Java, Computer Networks, etc. along with some Contests and Interview Experiences only on Coding Ninjas Studio.
Happy Learning Ninja!