How can I get what my main function has returned in Linux?
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 we know what my main() has returned at terminal?
#include<stdio.h> int main() { printf("Share Programing Tips"); return 32; } |
Now save this program as test.c file and in terminal run following command
$ gcc test.c $ ./a.out $ echo $? // this will show the return value of main |
Most shells store the exit code of the previous run command in $?
so you can store or display it.
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.
You can use this return code in linux script file.
You may like to read this also....
Post comment
Search in this website
our sponsors
latest comments
- sagar on List of all standard version of c language
- Mohit Dhukia on How to access/unblock songs.pk in india?
- shinto peter on How to configure mail from localhost ( wamp ) using PHP?
- tammylleanne on Implementation limitation of c programming language
- Deepak on How to access/unblock songs.pk in india?
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language