1

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?

Community
  • 1
  • 1
drake01
  • 113
  • 5
  • See also http://stackoverflow.com/questions/8082953/what-is-the-valid-range-for-program-return-value-in-linux-bash – John Carter Jun 04 '12 at 08:54
  • therefromhere , Thanks for the link.. It is indeed same question. :-) – drake01 Jun 04 '12 at 09:07
  • Since you have no explicit return statement, the actual value is undefined. In most cases it will be the value in some specific result register or accumulator, but there are no guarantees of that, it would be equally valid to always output zero for example. The question would have been just as valid with an explicit return statement and less ambiguous. It is also not valid in C *not* to have an explicit return statement for a non-void main, and in C++ it is not valid to have an implicit return type in any case, so this is not valid code in either language. – Clifford Jun 04 '12 at 09:41
  • @Clifford: Since C99, reaching the terminating `}` in `main`, if that has a return type compatible with `int`, returns 0. If the return type is not compatible with `int`, the returned status is unspecified. So many people have written `main` without return that it's now legal to not have one in `main`. – Daniel Fischer Jun 04 '12 at 11:33
  • @Daniel: I stand corrected, but the code would have demonstrated the issue just as well by not relying on this. – Clifford Jun 04 '12 at 20:10
  • @Clifford Absolutely, and it is _much_ cleaner. IMO, that's one of the few changes for the worse from C89 to C99. – Daniel Fischer Jun 04 '12 at 20:16
  • @Clifford I hope you understand this basic thing: When someone posts a question on SO, s/he is ignorant about the solution. Someone who knows the answer already (or knows it after someone else posts the solution ) [You in this case], can surely comment about the way in which the question should have been framed. I tried to ask in a manner I thought was best at the moment. Although after knowing the answer, it became obvious to me and I corrected the code. So, You should stop being rude to people. – drake01 Jun 04 '12 at 21:22
  • I don't think Clifford was, or meant to be, rude. The point is that pre-C99, your `main` without explicit return type was implicitly declared to return an `int`, but - iirc - not having an explicit return caused undefined behaviour. Since C99, `main` is an exception in that it has an implicit `return 0;` if there's no explicit return met (if the return type is compatible with `int`). However, the 'implicit int' rule was abolished with C99, so not having declared the return type made it a malformed programme. Doesn't make your question bad or invalid, but it's better to have conforming code. – Daniel Fischer Jun 04 '12 at 21:44
  • At least one respondent addresses the quality of the code rather than the question itself, adding "noise". I merely sought to help you improve the question to avoid that. What you should understand about SO is that this is not a traditional forum, and the ability for anyone to comment on questions and edit posts and close questions is intended to ensure the high quality information is available for all. – Clifford Jun 05 '12 at 04:46

3 Answers3

4

Unfortunately, its the case in Unix based systems. That you cannot return a value >255 from main even though you specify the return type as int. 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 return statement, how the return value is inferred is architecture dependent. For example, on ARM systems, its the value in R0 ( or R0+R1 sometimes) register and on x86 systems its the EAX register.

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • Pranav Manjunath Now that is something *new*, I now know about un*x machines. Thanks mate. :) Things again seem logical now... – drake01 Jun 04 '12 at 09:03
3

It's not just that you haven't specified a return type, you're missing the return statement as well. You don't return anything, so what the program returns is totally random, whatever happened to be in the relevant register/memory location at the time the function exited.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • Replacing main by int main and adding return a; as the last line before the program ends shows the same behavior. It returns 14 and not 270 on my machine. Explain? – drake01 Jun 04 '12 at 09:00
  • @drake: Of course it does, and that is what you should have posted! (not too late to edit it). If only to avoid "noise" from people addressing your strictly invalid code rather than providing the answer you were looking for. – Clifford Jun 04 '12 at 09:44
  • Though a valid point, this is a comment not an answer. – Clifford Jun 04 '12 at 09:46
2

From POSIX page:

The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, [CX] or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available to a waiting parent process.

http://pubs.opengroup.org/onlinepubs/000095399/functions/exit.html

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Thanks for the correct answer ouah. But I already got the correct answer and accepted it. Thanks once again :-) – drake01 Jun 04 '12 at 09:16