×

Is it fine to write void main() or main() in C/C++?

In C programming language:

The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main() function has no definition or legitimate usage and can occasionally deliver meaningless results or errors. However, main() denotes the main function, which accepts no parameters and returns an int data type.

In C++ programming language:

The default return type of main function in the C++ programming language is "void," which indicates that main() will not return anything.

The syntax for void main() function is as follows:

void main(){
	/* statement-1 */
	/* statement-2 */
}

The syntax for the int main() function, which is a complying implementation, is as follows:

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

Syntax in C++ programming language:

int main(int argc, char*argv[ ]){ 
/*optional command line arguments argc and argv*/
	/*statement-1*/
	/*statement-2*/
}

The int returned by the main() function allows a program to return a value to the computer that called it. For example, the process always terminates with a result value that the kernel system gathers in Linux Operating System. The return value is discarded for systems that do not provide such a feature.

Therefore, "void main()" is neither valid in the C++ programming language nor in the C programming language.

Note: Even if the C or C++ compiler supports void main(), avoid it at all costs. It is incorrect. In the C++ programming language, the main() function does not need to include a specific return statement. In that situation, the value returned is "zero,"  indicating that the execution was successful.

Take a look at the following C++ programming language example:

Program Code: 1

/* CPP program to show the main() function without return statement */
#include <iostream>
using namespace std;
int main(){
	cout<< “ Hello World!\n ”;
	cout<< “ This code returns the int value 0\n ”;
}

Output:

Is it fine to write void main() or main() in C/C++?

Program Code: 2

/* CPP program to show the main() function with return type */
#include <iostream>
using namespace std;
main(){  
/* default return type of main() function in C++ is int data type */
cout<< “Hello World!\n”;
cout<< “This code will return int value\n”;
return 0;
}	

Output:

Is it fine to write void main() or main() in C/C++?

There is no error in the above code. If you write the whole code without any errors, even without a return type at the end of the main( ) block, the compiler will add a return statement with the appropriate datatype at the conclusion of the program for you.

Note: In the ISO C++ or C99 versions, you cannot remove the type from a declaration. In other words, unlike the C89 version and ARM C++, when a type is missing from a declaration, the int data type is ignored.

An example of the int main() function in the C programming language:

/* C program to show the main() function without return statement */
#include <stdio.h>
int main(){
	printf(“Hello World!\n”);
	printf(“This returns int value 0\n”);
}

Output:

Is it fine to write void main() or main() in C/C++?

An example of C program to show the main() function with return type

/* C program to show the main() function with return type */
#include <stdio.h>
main(){
	printf(“Hello World!\n”);
	printf(“This returns int value\n”);
return 0;
}

Output:

Is it fine to write void main() or main() in C/C++?

Some Differences between void main() and int main():

The main() function works similarly to other functions. It also accepts parameters and returns a value. One thing to remember is that the program begins its execution with the main() function. When the operating system or the compiler calls the function, some value is returned by the main() block, and it is sent back to the operating system. The void main() function implies that no value can be returned. The int main() method specifies that it can return data of the integer type.

When the program is simple and will not stop the execution before the last line of the code or when the code is error-free, we may utilize the void main() function.

However, if we wish to leave the application using the exit() function, we must return some int values such as "zero" or "non-zero." In that case, the void main() function will fail to work.

Conclusion:

To conclude, it is never a good option to use void main() or just main() since it violates standards. However, certain compilers may allow it. As a result, it is preferable to use int main() rather than void main().


Related Topics

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

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

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 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++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Bit Manipulation in C++

The high-level language in which we communicate is not understood by the computer. As a result, there existed a standard mechanism for understanding any instruction sent to the computer. At...

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

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

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

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

Multilevel Inheritance

C++ Multilevel Inheritance Multilevel inheritance is such an inheritance in which a derived class is created from another derived class. C++ Multilevel Inheritance Example In this example, a base class Student is inherited in...

2 minutes read.