21

As an assignment in school, we have to write a C++ program and returns different error codes in the main.

The problem is that we have to return -2 if a specific error occurs but I have no idea how to return a negative value.

For example:

int main()
{ 
    int a = -2; 
    return a;
}

In windows this gives me a return value like: 42232684 and in Linux there is: 253

Why -2 is not allowed?

And how can I manage to get -2?

princepiero
  • 1,287
  • 3
  • 15
  • 28
Michi Kampl
  • 213
  • 2
  • 4
  • 1
    How do you check the return value? – Kiril Kirov Sep 19 '13 at 09:11
  • 2
    You can't do it. Termination status is unsigned. Are you sure the assignment really wants this in the program's termination status, and not stdout? – Barmar Sep 19 '13 at 09:12
  • @Barmar - seriously? Then why is `main` `int` not `unsinged` for example? Interesting, I've never thought about this. – Kiril Kirov Sep 19 '13 at 09:13
  • The return value of int is not the same as the program's termination status. The termination status is 2 bytes, the return value is put into one byte of that. it gets converted to unsigned in the process. – Barmar Sep 19 '13 at 09:14
  • @Barmar That would explain the result the OP gets on Linux, but not on Windows. Is this a Linux thing? Or a POSIX thing? – us2012 Sep 19 '13 at 09:16
  • In windows its automatically printed out by the IDE. In linux i don't know how to display the return value but my university has a testing environment for such things. i hand in code and the system checks the return values and lets me know if they are like they should be. – Michi Kampl Sep 19 '13 at 09:17
  • On Unix, the termination status uses one byte for the signal that killed the process, and one byte for the exit code. It looks like Windows is simply converting the exit code to unsigned long. – Barmar Sep 19 '13 at 09:17
  • 3
    According to the standard, the return value of `main()` is passed to `std::exit(int)`. From there, by C++11 § 18.5, "If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.". So if you're looking for a standard-manadated return value to the host OS, you're not going to find one. – WhozCraig Sep 19 '13 at 09:18
  • 1
    On Linux you display the exit code with `echo $?`. – Barmar Sep 19 '13 at 09:18
  • @Barmar: you are right. and linux converts it to unsigned char i think. – Michi Kampl Sep 19 '13 at 09:19
  • 7
    That's damn good question, especially for user, just signed in! This is really rare case :) – Kiril Kirov Sep 19 '13 at 09:20
  • how to check the exit code? visual studio accepts the return of -2 – Maher Sep 19 '13 at 09:28
  • @MichiKampl, on Linux, after executing `g++ source.cpp && ./a.out` on the terminal you should run `echo $?` to get the return code. @Maher, run the executable from the command prompt and then run `echo %errorlevel%`. –  Sep 19 '13 at 09:29
  • [POSIX](http://pubs.opengroup.org/onlinepubs/009695399/functions/exit.html) – BoBTFish Sep 19 '13 at 09:35
  • i still got -2 after doing that! it seems that it depends on system bits 32 and 64 – Maher Sep 19 '13 at 09:42

3 Answers3

10

The problem is that what is returned to the OS is then interpreted by the OS shell as IT likes it, not as your program likes.

the main function returns an int, and return -2 is just what your program has to do.

253 is -2 in 2s complement onto 8 bits.

The problem -here- is a mismatch between the C++ specs (int main()) and the way the shell use it. But it does not depend on the program.

The assignment itself is a trap.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
4

From C++11 standard 18.5/8:

If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

Thus is it completely compliant that you get different results for different platforms, and/or compilers.

nikolas
  • 8,707
  • 9
  • 50
  • 70
  • 1
    Your first sentence is in contradiction with the quoted section from the standard. The latter states that the return value is always implementation defined, while your introductory sentence suggests that sometimes it isn't. – IInspectable Sep 19 '13 at 09:44
2

Unix, and linux are limited to 8 bit return codes, -2 is 0xfe. Which your shell will understand to be 254 when you echo $?

You're expected to give a return code between 0 and 255.

http://en.wikipedia.org/wiki/Exit_status

on POSIX-compatible exit statuses are restricted to values 0-255, the range of an unsigned 8-bit integer.

Sirch
  • 407
  • 3
  • 17