2

Look at this 2 simples programs in C:

#include <stdio.h>
int main(int argc, char *argv[]) {
    return -1;
}

#include <stdio.h>
int main(int argc, char *argv[]) {
    return 1337;
}

Now look this very basic python script:

>>> import subprocess
>>> r=subprocess.call(['./a.out'])

I do not understand why but the python script r value contains:

  • 255 for the first C program. It should be -1
  • 57 for the second C program. It should be 1337

Have i something wrong ?

Thanks

4

1 Answer 1

1

Python has nothing to do with it. This is system dependent.

On Unix/Linux systems, the return code is stored on 1 byte, and is unsigned, so truncation occurs outside 0-255 range.

So -1 becomes 255, 1337 becomes 57 (can be checked by applying modulus 256).

note that in Windows, return codes can be higher than 255 (was able to pass 100000000, but still not negative)

The conclusion is: don't rely on return codes too much to pass information. Print something on the console instead.

Related: https://unix.stackexchange.com/questions/37915/why-do-i-get-error-255-when-returning-1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.