I created this simple program that calculates sum of two variables 'a' and 'b', stores it in variable 'sum' and returns it to main() function,
1 2 3 4 5 6 7 8 9 10 11
#include<stdio.h>
int main()
{
int a,b,sum;
printf("\nEnter value of a = ");
scanf("%d",&a);
printf("\nEnter value of b = ");
scanf("%d",&b);
sum=a+b;
return(sum);
}
This program is running fine and returning the exit code as value of variable sum. I checked it in gdb.
But when sum > 7, the value of exit code returned as seen in gdb is "2 more than the value of variable sum". For example if sum = 8, then exit code of program is 10.
Why so? Can someone explain me this strange phenomena?
main() should only return two values predefined in cstdlib: EXIT_SUCCESS and EXIT_FAILURE.
If it doesn't work correctly for returning your sum, it's because it was never meant to be used in this way. Possibly if you change your compiler, it will behave differently. But still main() should return a small number which is an error code.
Edit: I see you're coding in C, not C++ in that case the header is stdlib.h instead of cstdlib.
Edit 2: you can also check the error code from the console without GDB
Thanks for replying nano511 and Catfish.
But never mind i got the answer. Actually the value returned by main() function is octal. I was thinking it to be decimal. ;)