Home »
C programming language
return 0 from int main() in c programming
In this tutorial, we will learn about the return type of a main() function. If main() has return type int, then why should a function return an integer value to the calling function?
By IncludeHelp Last updated : April 13, 2023
What is main() function in C?
The 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.
The main() is a function, which is invoked (called) through operating system when program's execution is going to start.
What are the Variations of main() in C?
The following are the variation of main() in C language:
- void main(void);
- void main();
- int main(void);
- int main();
- int main(int argc, char **argv);
- int main(int argc, char *argv[]);
main() Function Parameters Explanation
1. int argc
Here, argc is the number of argument passing in main() function.
2. char *argv[]
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.
C program to demonstrate int main() with return 0 statement
#include <stdio.h>
int main()
{
FILE* fp;
//open any file
fp = fopen("sample.txt", "r");
if (fp == NULL) {
printf("Error in file opening!!!\n");
return -1;
}
printf("File opened successfully.\n");
//closing the file
fclose(fp);
return 0;
}
Output
Error in file opening!!!
Explanation
Since we don't have this file "sample.txt", program will print "Error in file opening!!!" and return -1 to the operating system.
Use of 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.
C program to demonstrate int main() with return EXIT_SUCCESS statement
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello, World!\n");
return EXIT_SUCCESS;
}
Output
Hello, World!