×

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

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

SDL library in C++ with Examples

SDL stands for Simple DirectMedia Layer. Using OpenGL and Direct3D, it is a cross-platform development library created to give users low-level access to audio, keyboard, mouse, joystick, and graphics hardware....

3 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 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.

Nullptr in C++

What is Nullptr in C++? A null pointer value is represented by the term nullptr. Use a null pointer value to indicate that a native pointer type, inner pointer, or object...

3 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions. What are Keywords in C++? In C++, a keyword is a reserved word that has a predefined...

4 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.