CS1102C Miniature 2

Why do we return 0 at the end of the main() function?

You might have noticed that in C and C++ programs, the main() function is written

int main(); {
    // ...
}

or

int main(int argc, char* argv[]) {
    // ...
}

which meant that at the end of the main() function, we are supposed to return an int, usually by:

return 0;

Before I continue, it is worth digressing a bit and mention that if a function prototype is written without a return type, then the expected return type is int. That is,

func();

and

int func();

have the same meaning. So it is a good idea to always use the second form, because it makes the return type specific and causes less confusion for readers. The reason why I make this digression is because it is often seen in older programs, particularly the main() function. To specify that a function returns nothing, use the void keyword instead:

void func();

When a function is declared to return something, the function really should return a value. Otherwise, the caller will get a undefined random value, which is definitely not a good thing. This is why C and C++ compilers give warnings when you forget to make the function return a value when it is declared to return something.

Back to the question: why do we return 0, or any other value, at the end of the main() function?

The simple answer: most of the time, it does not matter!

The longer answer: it tells the operating system the status of your program, such as whether your program ran into an error. By convention, when 0 is returned from main(), the program never encountered any errors. Conversely, when a non-zero value is returned, it signals that an error is encountered, for example, the program could not open a file that was specified on the command line. The actual values that are returned from the program and their meanings are program specific: they are defined by the programmer of that program.

An illustration with a C program segment that uses non-zero return values:

int main() {
    FILE* hinfile;
    FILE* houtfile;
    if (!(hinfile = fopen("infile.txt", "r"))) {
        fprintf(stderr, "Error opening infile.txt for reading!\n");
        return 1;
    }
    if (!(houtfile = fopen("outfile.txt", "w"))) {
        fprintf(stderr, "Error opening outfile.txt for writing!\n");
        return 2;
    }
    // ...
    fclose(hinfile);
    fclose(houtfile);
    return 0;
}

If you are writing a C or C++ program that calls other programs, and want to retrieve the return values, you can do the following:

int ret;
ret = WEXITSTATUS(system("prog"));  // ret will contain the return value

For shell scripts on Unix and Linux systems, the return value can be retrieved using the $? variable. For example:

#! /bin/bash
prog  # call your program
if (($? != 0)); then
    echo The program ran into an error.
fi

On MS-DOS and Windows systems, you can get the return value through the IF ERRORLEVEL command. An example MS-DOS batch (.bat) file:

@echo off
prog
if errorlevel 1 echo The program ran into an error.

However, do note that in all systems, the return value should be retrieved immediately after the program is called. This is because the $? or ERRORLEVEL value will be overwritten each time a program is called.

Back to Index