How can I get what my main function has returned?

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.


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.

Tags:

Linux

C

Gcc