What should main() return in C/C++?


The return value of main() function shows how the program exited. The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value.

In C++ language, the main() function can be left without return value. By default, it will return zero.

Here is the syntax of main() function in C language,

int main() {
   ….
   return 0;
}

Here is an example of main() function in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   int a = 10;
   char b = 'S';
   float c = 2.88;
   a = a+b;
   printf("Implicit conversion from character to integer : %d\n",a);
   c = c+a;
   printf("Implicit conversion from integer to float : %f\n",c);
   return 0;
}

Output

Implicit conversion from character to integer : 93
Implicit conversion from integer to float : 95.879997

In the above program, The main function has business logic. There are three variables a, b and c where a holds sun of a and b. the variable c holds sum of c and a. The main function is returning 0.

a = a+b;
printf("Implicit conversion from character to integer : %d\n",a);
c = c+a;
printf("Implicit conversion from integer to float : %f\n",c);
return 0;

Updated on: 26-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements