×

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

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

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++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 minutes read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

Hierarchical Inheritance

C++ Hierarchical Inheritance Hierarchical inheritance inherits the property of one base class in more than one derived class.   C++ Hierarchical Inheritance Example #include <iostream>   using namespace std;   class Person {       char gender[10];       int age;   public:       void getPerson()       {           cout << "Age: "; cin >> age;           cout << "Gender: "; cin >> gender;       }       void dispPerson()       {           cout << "Age: " << age << endl;           cout << "Gender: " << gender << endl;       }   };   class Employee : public Person {       float salary;   public:       void getEmployee()       {           Person::getPerson();           cout << "Salary: Rs."; cin >> salary;       }       void dispEmployee()       {           Person::dispPerson();           cout << "Salary: Rs." << salary << endl;       }   };   class Student : public Person {       char level[20];   public:       void getStudent()       {           Person::getPerson();           cout << "Class: "; cin >> level;       }       void dispStudent()       {           Person::dispPerson();           cout << "Level: " << level << endl;       }   };   int main()   {       Person per;       Employee emp;       Student stu;       cout << "Student data" << endl;       cout << "Enter data" << endl;       stu.getStudent();       cout << endl << "Displaying data" << endl;       stu.dispPerson();       cout << endl << "Staff Data" << endl;       cout << "Enter data" << endl;       emp.getEmployee();       cout << endl << "Displaying data" << endl;       emp.dispPerson();   } Output: Student data Enter data Age: 10 Gender: f Class: 5 Displaying data Age: 10 Gender: f Employee data Enter data Age:...

1 minute read.

Stack in C++

Stack: The stack is a very popular data structure. It is the form of data structure that follows a particular order called FIFO(First-In-First-Out). In simple words, a stack is an Abstract...

4 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

Inheritance in C++ vs Java

Just like we inherit traits from our parents, object-oriented programming has a concept called inheritance. In terms of object-oriented programming, a class's traits and behaviours, or its data and methods,...

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

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

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

3 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

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

Implementing the sets without C++ STL containers

Many practical features and tools in C++ support us in programming competitions. One of these parts is a set from the Standard Template Library (STL), which offers an effective way...

6 minutes read.