ext_142778 ([identity profile] luyer.livejournal.com) wrote in [personal profile] theducks 2008-08-05 07:59 pm (UTC)

Firstly, %d is the format option for an int. You should be using %hhd or %hhu for a char (d being signed, u unsigned). Or typecasting opt to (int)opt.

Secondly, you should be using an int anyway: getopt returns int. You didn't #include unistd.h, so you may be implicitly defining getopt, and implicitly defining it to return char.

Thirdly, PowerPC vs i686: different byteorder. This may impact the result of either of these errors.

Fourthly, -1 is the unsigned char with the same binary representation as the signed char 255.

Finally: This code depends on undefined behavior. C can do absolutely anything where the standard does not define the behavior, and due to the highly optimized nature of most C code, often does something you don't expect.

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting