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?
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.