Home » C programming language

return 0 from int main() in c programming

What is main ()?

main() is a program's execution entry point of C, C++ or some other programming languages. It is system declared (pre declared) function which is defined by the programmer.

main() is a function, which is invoked (called) through operating system when program's execution is going to start.

There are some variations of main() function

void main(void);
void main();

int main(void);
int main();

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

Here argc is the number of argument passing in main() function, argv is the pointer to strings (arguments which are passing after argument count).

Some compilers may not support void as return type of main() function.

Here we are discussing about return type of main() function. If main() has return type int, then function should return an integer value to the calling function.

Why int main()?

There are many variations of main() function, but now a days it's a standard that we should return some value (an integer value) to the calling/parent function.

Since main() calls by the operating system at the time of program's execution, returned value reaches to the operating system which indicates that function/ program is executed successfully or not.

So we should use this variation of main() function but we really don’t know what operating system does with returned value?

Why return 0?

It is not necessary that every time you should use return 0 to return program's execution status from the main() function. But returned value indicates program's success or failure to the operating system and there is only one value that is 0 which can indicate success and other non zero values can indicate failure of execution due to many reasons.

For example - if program's execution fails due to lake of memory we can return -1, if it fails due to file opening we can return -2, if it fails due to any invalid input value we can return -3 and so on. If program's execution is success we should return 0.

Consider the following program

#include <stdio.h>
int main(){
	FILE *fp;
	//open any file
		printf("Error in file opening!!!\n");
		return -1; 
	printf("File opened successfully.\n");
	//closing the file
	return 0;


Error in file opening!!!

Since we don't have this file "sample.txt", program will print "Error in file opening!!!" and return -1 to the operating system.

EXIT_SUCCESS, EXIT_FAILURE instead of 0 or non zero value

There are two values defined in stdlib.h which indicates success and failure of the program to the operating system.

We can use EXIT_SUCCESS instead of return 0 to indicate successful execution of program and EXIT_FAILURE instead of non zero value to indicate failure of program's execution.

Consider the following program

#include <stdio.h>
#include <stdlib.h>

int main()
    printf("Hello, World!\n");

    return EXIT_SUCCESS;


Hello, World!

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.