What does main() return in C and C++?
Last Updated :
19 Sep, 2019
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 return 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 :
C
#include <stdio.h>
void main()
{
// This code will run properly
// but in the end,
// it will demand an exit code.
printf("It works fine");
}
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 :
C
#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;
}
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 :
C
#include <stdio.h>
int main()
{
printf("GeeksforGeeks");
return "gfg";
}
Output:
It works fine
Runtime Error:
NZEC
Correct Code for C :
C
#include <stdio.h>
int main()
{
printf("GeeksforGeeks");
return 0;
}
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 :
CPP14
#include <iostream>
using namespace std;
void main()
{
cout << "GeeksforGeeks";
}
Compile Errors:
prog.cpp:4:11: error: '::main' must return 'int'
void main()
^
Correct Code for C++ :
CPP14
#include <iostream>
using namespace std;
int main()
{
cout << "GeeksforGeeks";
return 0;
}
Example #4 :
CPP14
#include <iostream>
using namespace std;
char main()
{
cout << "GeeksforGeeks";
return "gfg";
}
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++ :
CPP14
#include <iostream>
using namespace std;
int main()
{
cout << "GeeksforGeeks";
return 0;
}
Similar Reads
What is return type of getchar(), fgetc() and getc() ? In C, getchar(), fgetc(), and getc() all are the functions used for reading characters from input buffer. This buffer is standard input buffer for getchar() and can be any specified file for getc() and fgetc(). In this article, we will learn about the return type of these functions and why it matter
3 min read
Return Statement vs Exit() in main() in C++ The return statement in C++ is a keyword used to return the program control from the called function to the calling function. On the other hand, the exit() function in C is a standard library function of <stdlib.h> that is used to terminate the process explicitly. The operation of the two may
3 min read
Return values of printf() and scanf() in C/C++ What values do the printf() and scanf() functions return ? printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding error Example 1: The printf() function in the code written below returns 6. As 'CODING' contains 6 characters. CPP // C/C++ program
2 min read
Executing main() in C/C++ - behind the scene How to write a C program to print "Hello world" without main() function? At first, it seems impractical to execute a program without a main() function because the main() function is the entry point of any program. Let us first understand what happens under the hood while executing a C program in Lin
4 min read
Difference between return and printf in C In C programming, return and print serve fundamentally different purposes and they are used in distinct contexts to achieve specific tasks. Let's see each of them and see what are their functions and differences. 1. Return Statement In C, return is a statement used within a function to terminate the
2 min read