What does main() return in C and C++?

What does main() return in C and C++?

Answer: Every C and C++ program has a function called the main() function, which works as an entry point of the program. It is the point of origin and conclusion of execution.

In both C and C++, the main() function is the entry point of the program. A function returning int may be defined this way according to the C (ISO C99+) and C++ (ISO C++98+) standards. The return value of main() is an integer that tells the operating system whether the program exited successfully or encountered an error. In this article, we will learn about the main() function usage and how it returns in C/C++ languages.

Table of Contents

What is main() in C and C++?

It is the entry point of the program where everything begins and ends

  • return 0; -> Indicates successful run.
  • return 1; or return any non-zero number -> Signals an error or unusual exit

Structure of main()

int main() {
    // Program execution
    return value;
}

The return type of the main() has to be an int type. A void return type is non-standard and may not work across all compilers. The main() function returns a status code to the operating system that indicates whether the program ran successfully or failed. return 0 indicates success (the program finished successfully), while return 1 (or any value other than zero) signals failure or error.

If the main() does not explicitly return a value in C++, the compiler implicitly inserts return 0 at the end of main() to indicate success. In C, not using a return may result in undefined behavior in some compilers.

Simple Code in C

C

Output:

Simple Code in C Output

The above program returns 0, which means the code is running successfully.

Simple Code in C++

Cpp

Output:

Simple Code in C++ output

The above code returns 0 by ensuring the successful execution.

Returning an Error Code

As we discussed earlier, if the program returns an error code, it is telling you that something is wrong with the code. When a program terminates, it returns an exit status to the operating system. Returning a non-zero value (like return 1;) denotes that there was an error or issue with the program.

Returning an Error Code in C

C

Output:

Returning an Error Code in C Output

Notice here, return 1; indicates the program didn’t run successfully, which is an error to the operating system (OS).

Returning an Error Code in C++

Cpp

Output:

Returning an Error Code in C++ Output

The return statement 1 in C++ is an indication to the operating system of an error or failure, similar to requiring the system to know that the program did not terminate successfully.

Implicit Return in C++

In C++, if you do not explicitly give a return statement in your main() function, the compiler makes a return 0 as the last line in the main() function. So, in C++, the program does return 0, which indicates successful execution, even if the programmer forgot to write the return statement.

Why Does C++ Allow This?

In C++, if you don’t explicitly return a value from main(), the compiler automatically assumes a return 0, indicating successful program execution. This means you can ignore the return statement at the end of the main() without affecting the program’s behavior. This default behavior is specified in the ISO C++98 standard and remains consistent in later versions.

Note: This implicit return is a feature of C++ only and an error in C, which will produce undefined behavior and may even output warnings/errors to the compiler.

Example:

Cpp

Output:

Why Does C++ Allow This Output

This C++ example does not return 0 from main(), but the compiler will add return 0; to the end of main() for us to indicate your program ran without error.

Why Is This Not Allowed in C?

In C, the return type of main() must return an integer. Of course, in C, a function that doesn’t explicitly end with a return statement will have an undefined behavior – it is not mandatory, but there is a huge risk of unpredictable results on different compilers. Thus, the return 0 in main() is written explicitly to indicate successful execution.

Example:

C

Output:

Why Is This Not Allowed in C Output

In some compilers, compiling this code in C, you might get a warning or error saying that main() should return int explicitly.

Conclusion

In both C and C++ programming, the main() function is the entry point of the program. The return type of the main() has to be an int type, as it checks whether the program ran successfully or failed. The return 0 indicates the program finished successfully, while the return 1 or any value other than zero signals failure or error. To ensure the clarity return 0 from main() is always necessary.

FAQs on What should the main() return in C and C++

1. What is the return type in C and C++?

The return type in C and C++ must be an int type, as it returns the exit status to the operating system.

2. What happens if main() function doesn’t return anything in C++?

In C++, if the main() function doesn’t return any value, the compiler automatically generates the return 0 by indicating the successful execution.

3. Is void main() acceptable in C/C++?

No, the void main() is not acceptable in C/C++ because it is non-standard and may not work in all compilers.

4. What does return 1 indicate in the main() function?

The return 1 or any value other than zero signals failure or error.

5. Can main() have arguments in C/C++?

Yes, main() can take arguments like (int main(int argc, char* argv[])

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner