×

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function

In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution.

In general, a value of '0' indicates that the program was successfully executed.

Syntax of “int main( ) function”

int main() {
	/* statement-1*/
	/* statement-2*/
	return 0;
}

int main(void) function

int main(void) indicates that the function accepts “NO arguments.”

Difference between “int main( ) and int main(void)

In C++ programming language, there is no variation; both int main( ) and int main(void) are the same.

Both int main( ) and int main(void) operate effectively in the C programming language, but the int main(void) function is regarded as more technically effective since it explicitly indicates that the main( ) function can be called even without parameters.

When a function signature in the C programming language does not provide an argument, it indicates that the function may be called with any number of parameter values or without any parameter values.

Example of int main( ) function in C programming language

Here a function declared as ‘letters( )’ can accept any number of parameters.

/* Program Code Compiles and runs fine in C language */
#include <stdio.h>
void letters( )
{}
int main( ){
	letters( );
	letters(‘a’);
	letters(“abc”);
	letters(‘ab’,“abcd”);
	printf(“abcd\n”);
	return 0;
}	

Output:

Difference between “int main( ) and int main(void)” in C/C++?

In the above program code, calling letters( ), letters('a'), and letters("abc") will not give any error because any number of arguments can be sent to the function without any parameters.

Example for int main( ) function in C++ programming language:

Here a function is declared as "letters( )."

/* Program Code Fails in compilation in C++ language */
#include <iostream>
using namespace std;
void letters()
{}
int main(){
    letters( );
    letters('a');
    letters("abcd");
    printf("abcd\n");
    return 0;
}

Output:

Difference between “int main( ) and int main(void)” in C/C++?

In the above program code, calling letters( ), letters('a'), letters("abcd") will give us some errors because we can't pass any number of arguments to the function "letters( )."

Example for int main(void) function in C programming language

Here a function is declared as "letters(void)." However, using letters(void) limits the function's ability to accept any argument and results in an error.

/* Program Code Fails in compilation in C language */
#include <stdio.h>
void letters(void)
{}
int main( ){
	letters( );
	letters(‘a’);
	letters(“abc”);
	letters(‘ab’,“abcd”);
	printf(“abcd\n”);
	return 0;
}

Output:

Difference between “int main( ) and int main(void)” in C/C++?

The preceding program code will throw an error since we used 'letters(void),' which implies we can't send any arguments to the method 'foo' as we could with 'letters( ).'

Example for int main( ) function in C++ programming language:

Here a function declared as "letters( )."

/* Program Code Fails in compilation in C++ language */
#include <iostream>
using namespace std;
void letters(void)
{}
int main(){
    letters( );
    letters('a');
    letters("abcd");
    printf("abcd\n");
    return 0;
}

In the preceding program code, calling letters( ), letters('a'), letters("abcd") will give us some errors because we can't pass any number of arguments to the function.

“letters(void).” in C++ programming language.

Output:

Difference between “int main( ) and int main(void)” in C/C++?

Some more differences between int main( ), main( ), void main( )

Just like any other function, main( ) is also a function with the additional property that the program code execution always begins with the 'main.' Its return types are 'int' and 'void.'

1. int main - 'int main' indicates that our function must return an integer at the completion of execution, which we do by returning ‘0’ integer at the end of the program. The standard for "successful program execution" is 0.

2. main - The undefined return type in the C'89 version is 'int' by default. In C'89, ‘main( )’ is identical to ‘int main.' However, in the C'99 version, this is not permitted, and so ‘int main( )’ must be used.

3. void main - The American National Standards Institute (ANSI) standard states "no" to the "void main( )" therefore, using it is incorrect. If this is the case, the 'void main( )' should be avoided.

As a result, the preferable method is “int main( )” method.

Conclusions

In C++ programming language, letters(void) and letters() are equivalent, but not in C language. The same is true for the 'main' function. If ‘main’ function does not accept any arguments, the preferable form is “int main(void).”


Related Topics

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

Check for Balanced Brackets in an Expression (well-formedness) using Stack

Write a program that check the correctness of the pairs and ordering of the characters “{“, “}”, “(“, “)”, “[“, “]” in the expression string exp. Example: Checking for balanced parenthesis is one of...

2 minutes read.

C++ Forward Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

C++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

4 minutes read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

Quick Sort in C++

Quick sort is an efficient, in-place, comparison-based sorting algorithm that uses a divide-and-conquer strategy to sort an array or list of elements. First a pivot element is selected from the...

4 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

C++ Virtual Function

A virtual function is such function which is declared inside the base class and redefined by the derive class. C++ uses a virtual keyword to make a function as a virtual function. The virtual...

1 minute read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

Returning Multiple Values from a Function using Tuple and Pair in C++

We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly...

4 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.