What Should main() Return in C and C++



In C/C++, the main() function is used in the beginning of every program. This returns the program execution status in the operating system.

Syntax

Following is the syntax of the main() function using C/C++:

int main(void);
int main(int argc, char *argv[]);

Example

Below, the C/C++ example shows the usage of the main() return value.

C C++
#include <stdio.h>

int main() {
printf("Hello, C World!");
return 0;
}

Key Points of C

  • Return 0: This is standard for successful execution
  • Non-zero: It indicates error
  • Implicit return: C99 allows omitting return
#include <iostream>

int main() {
std::cout << "Hello, C++ World!";
return EXIT_SUCCESS;
}

Key Points of C++

  • EXIT_SUCCESS: Preferred over 0
  • EXIT_FAILURE: For error conditions
  • void main(): Invalid in C++

Common Return Values

Here, we provided the tabular data to understand the meaning of each macros that used in the C/C++ programs:

Value Macro Meaning
0 EXIT_SUCCESS Program succeeded
1 EXIT_FAILURE General failure
2 - Command-line error

Points to remember

  • C89: This required the explicit return.
  • void main(): This is Non-standard for both C and C++.
Updated on: 2025-04-14T18:00:03+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements