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

C

According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of written statement but that doesn’t mean that C code doesn’t require 0 as exit code. Let’s see one example to clear our thinking about need of return 0 statement in our code.

Example #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
void main()
{
  
    // This code will run properly
    // but in the end,
    // it will demand an exit code.
    printf("It works fine");
}

chevron_right


Output:

It works fine

Runtime Error:

NZEC

As we can see in the output the compiler throws a runtime error NZEC, Which means that Non Zero Exit Code. That means that our main program exited with non zero exiting code so if we want to be a developer than we make these small things in our mind.

Correct Code for C :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
  
    // This code will run properly
    // but in the end,
    // it will demand an exit code.
    printf("This is correct output");
    return 0;
}

chevron_right


Output:

This is correct output

Note: Returning value other than zero will throw the same runtime error. So make sure our code return only 0.

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
  
    printf("GeeksforGeeks");
    return "gfg";
}

chevron_right


Output:

It works fine

Runtime Error:

NZEC

Correct Code for C :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
  
    printf("GeeksforGeeks");
    return 0;
}

chevron_right


Output:

GeeksforGeeks

C++

In case of C++, We are not able to use void keyword with our main() function according to coding namespace standards that’s why we only intend to use int keyword only with main function in C++. Let’s see some examples to justify these statements.

Example #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
void main()
{
    cout << "GeeksforGeeks";
}

chevron_right


Compile Errors:

prog.cpp:4:11: error: '::main' must return 'int'
 void main()
           ^

Correct Code for C++ :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int main()
{
    cout << "GeeksforGeeks";
    return 0;
}

chevron_right


Output:

GeeksforGeeks

Example #4 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
char main()
{
    cout << "GeeksforGeeks";
    return "gfg";
}

chevron_right


Compile Errors:

prog.cpp:4:11: error: '::main' must return 'int'
 char main()
           ^
prog.cpp: In function 'int main()':
prog.cpp:7:9: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
  return "gfg";
         ^

Correct Code for C++ :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int main()
{
    cout << "GeeksforGeeks";
    return 0;
}

chevron_right


Output:

GeeksforGeeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.