0
int main(){ return -1; }

I compiled the above c program with gcc. To find the return value of the program I used echo $?. Output of echo $? was 255.Currently i am using terminator

how can i get negative values.

Tim
  • 41,901
  • 18
  • 127
  • 145
Vivek
  • 38
  • 4
  • 2
    The only values you can return from `main` portably are `EXIT_SUCCESS` and `EXIT_FAILURE`. For anything else consult your OS. – Kerrek SB May 26 '14 at 19:12
  • It should also be mentioned that `EXIT_SUCCESS` is equivalent to `0`. – ooga May 26 '14 at 19:14
  • Negative returns from main are not supported. See [**glibc**](http://www.gnu.org/software/libc/manual/html_node/Exit-Status.html) "This is a value between 0 and 255 that the exiting process passes as an argument to exit." – David C. Rankin May 26 '14 at 19:24
  • On POSIX systems (UNIX, Linux, etc.), the value is determined by the `wait` system call. Other systems might have other semantics. – Keith Thompson May 26 '14 at 19:33
  • @ooga: The C standard doesn't say that `EXIT_SUCCESS == 0`; it merely says that both `0` and `EXIT_SUCCESS` denote success. But I've never heard of a system where `EXIT_SUCCESS != 0`. (I have seen systems where `EXIT_FAILURE != 1`.) – Keith Thompson May 26 '14 at 19:34
  • @KeithThompson I know. That's why I said "equivalent", not "equal". OpenVMS is a system where success is not equal to 0, but returning 0 from a C program will still return the success value (which I believe is usually 1, but is technically any odd value ... strange). – ooga May 26 '14 at 19:38
  • @ooga: Just to be pedantic (who, me?), `0` and `EXIT_SUCCESS` are not necessarily equivalent. They could denote different kinds of success. (Yes, OpenVMS was the system I was thinking of.) – Keith Thompson May 26 '14 at 19:51
  • @KeithThompson Good point, but I still believe that in a C program running on OpenVMS, returning `0` would be "equivalent" to returning `EXIT_SUCCESS`. If you wanted some other kind of success, you'd have to return an odd value other than 1. – ooga May 26 '14 at 20:13
  • @ooga: I believe that's correct, and I didn't say otherwise. What I meant was that the C standard doesn't require `0` and `EXIT_SUCCESS` to be equivalent. – Keith Thompson May 26 '14 at 20:16
  • @KeithThompson I believe that the standard *does* say that they are equivalent, as per 7.20.4.3: " If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned." That's exactly what I mean by saying that they are "equivalent", i.e., the implementation will return the exact same value to the OS if either EXIT_SUCCESS or 0 is returned from a C program. – ooga May 26 '14 at 21:04

2 Answers2

0

The return code of a process is stored in a single unsigned byte.

You can't do that.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

That is technically the return you wrote. The unsigned value of a byte with the signed value of -1 is 255.

CellPunk
  • 11
  • 1
  • that was obvious,I want to know the reason why not give out negative vaues as is – Vivek May 26 '14 at 19:17
  • If you think it's obvious, then there's a fundamental misunderstanding here. -1 is, in binary, 11111111. 255 is 11111111 as well. – CellPunk May 26 '14 at 22:20