68
votes

In a C program if we want to give some input from terminal then we can give it by:

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

In the same way, if we want to get return value of main() function then how can we get it?

In each main() we write return 1 or return 0; how can I know what my main() has returned at terminal?

Edit:1

I get it that by echo $? we can get the return value of main() but it only allows me to return a value less then 125 (in Linux) successfully. A return value more than that cannot be be successfully received by the $ variable so

why is int the return type of main()? Why not keep it short int?

Edit2

From where can I find out the meaning of the error code if main() returns a value greater than 125?

6
There's no real advantage to choosing short over int unless you have a lot of them in the same place (array or struct). It's not faster to have a short and on modern ABIs you often don't even save space by choosing it. In some places short is slower because you have to do sign extension when loading it.Dietrich Epp
surprised by one downvote..!!!Jeegar Patel
@Jeegar Patel....you must understand that some people are always hatersMayukh Sarkar

6 Answers

53
votes

Most shells store the exit code of the previous run command in $? so you can store or display it.

$ ./a.out
$ echo $?     # note - after this command $? contains the exit code of echo!

or

$ ./a.out
$ exit_code=$?    # save the exit code in another shell variable.

Note that under linux, although you return an int, generally only values less than 126 are safe to use. Higher values are reserved to record other errors that might occur when attempting to run a command or to record which signal, if any, terminated your program.

40
votes

Your shell probably has a special variable $?, which holds the last program returned value. So, soon after your program finishes, you can run:

echo $?

to see the returned value.

32
votes

In DOS/Windows you can use errorlevel within a batch file

executable optional arguments
if errorlevel 4 goto LABEL4
if errorlevel 3 goto LABEL3
if errorlevel 2 goto LABEL2
if errorlevel 1 goto LABEL1
:SUCCESS
echo SUCCESS; errorlevel 0
goto :eof
:LABEL1
echo FAILURE; errorlevel 1
goto :eof
:LABEL2
echo FAILURE; errorlevel 2
goto :eof
REM ...

Just remember to check from the greatest to the lowest because if errorlevel 42 really means "if errorlevel is 42 or greater"

5
votes

Summarizing comments and bits and pieces so they're in one place.

A C program always has an exit code, which the program may decide for itself if it terminates normally, by returning a value from the main function or by calling the exit function. If the program terminates abnormally, for example by a segmentation fault, the operating system decides the exit code.

In Unix (Posix), the exit code is an 8-bit value: 0-255. It is combined with some other metadata to a status: the other metadata includes information about whether the program terminated normally or not, if it was terminated because of a signal, and if so, which signal. For details, see the wait(2) manual page.

In Unix, at the shell, the status of the previous command is accessible as the $? special variable. Because the exit code is only 8 bits, and it's treated as an unsigned integer, if you return a negative value, it gets turned into a positive one: -1 becomes 255. Likewise, if you return a value greater than 255 only the least significant 8 bits are used: 256 becomes 0.

The return type of main is int, rather than short or char, because there's no particular benefit in making it a smaller type, particularly at this point in history, decades after it was decided. Changing it now would only cause unnecessary complications.

If you want to execute a program from C, the standard library provides the system function, which handily returns the status of the program. (Note that system runs commands via the shell, and you need to be very careful about escaping everything correctly if you give the command any externally provided filenames or other things on the command line.)

For more flexibility, you can execute other programs using the system calls fork, execl (or one of its variants, see the exec(3) manual page), and wait (already mentioned above). This is powerful and flexible, but it's also easy to make mistakes, so be sure to read the documentation and check out some example programs first. (On the other hand, it's very much fun to learn this stuff.)

3
votes

You can get the exit values with the command basic linux command echo $? The error codes are standard and the details are explained in this link

The general codes are

**

0- success

1- general errors

126- permission issue

127-Illegal command

128-Invalid arguments and fatal errors

255-Out of range**

1
votes

in windows command line to read return value from main use echo %errorlevel%

(Code) return.c

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
    int i;

    printf("Enter a number");
    scanf("%d",&i);

   if(i==2)
        exit(1);
   if(i==3)
        exit(2);
    
   return 0;
}

OUTPUT

OUTPUT of above program return.c