Possible Duplicate:
What is the valid range for program return value in Linux/bash?
Here is a simple c program:
#include <stdio.h>
int main(){
int a=0, b=100;
while(a<=b){
printf("%d\n",a);
a+=10;
}
return a;
}
Checking the return value on linux gives 110 which is expected as the last expression evaluated in the program is assignment of 110 to a;
But when I replace b=100 with something greater than 256 say b=260; the return value is 14 and not 270 which was expected.
Why does returned value wrap around 256 when main is supposed to return integer whose range is far more than 256?
Unfortunately, its the case in Unix based systems. That you cannot return a value >255 from
maineven though you specify the return type asint. Read here.Windows is more permissive in the sense that it uses 32-bit signed integers as exit codes
Also, you cant assume that the return value shall always be the value of the last expression evaluated. If there is no explicit
returnstatement, how the return value is inferred is architecture dependent. For example, on ARM systems, its the value inR0( orR0+R1sometimes) register and onx86systems its theEAXregister.