theducks: (Default)
[personal profile] theducks
Dear LazyGDB..

Not knowing much about C, I find myself confused by the following, extrapolated from some code I didn't write:

Code:
#include <stdlib.h>
#include <signal.h>

int main(int argc, char **argv)
{

        char    opt;
        char    *options = "a:de:f:l:p:";

        opt = getopt(argc, argv, options);
        printf ("Opt: %d\n",opt);

}


Linux on PowerPC:
suffix:~# ./test -a fish
Opt: 97
suffix:~# ./test
Opt: 255


Linux on i686:
vitalstatistix:~# ./test -a fish
Opt: 97
vitalstatistix:~# ./test 
Opt: -1


What is the deal?

(no subject)

Date: 2008-08-05 04:19 am (UTC)
From: [identity profile] girlmitzi.livejournal.com
I don't know what this means either but for some reason I just took off all my clothes...

(no subject)

Date: 2008-08-05 04:27 am (UTC)
From: [identity profile] dannipenguin.livejournal.com
You're seeing the difference between a signed char and an unsigned char (why does C say char can have a sign? who knows). Bitwise, they are the same value: http://en.wikipedia.org/wiki/Two%27s_complement

It's worth noting that man 3 getopt (first point of call) tells you that -1 is returned when there are no more options to pass and that getopt() returns an 'int', not a 'char'.

(no subject)

Date: 2008-08-05 04:54 am (UTC)
From: [identity profile] theducks.livejournal.com
See, that's what I would have thought.. but in the code I was looking at, it was indeed a char..

(no subject)

Date: 2008-08-05 11:18 am (UTC)
From: [identity profile] zharradan.livejournal.com
premature optimisation root of evil etc

ISO 9899 section J3.4

Date: 2008-08-05 07:57 pm (UTC)
From: [identity profile] mkj.livejournal.com
char's signedness is implementation-defined. If you care, use "(un)signed char" or pass some gcc flag to force it a particular way.

J3.4
"Which of signed char or unsigned char has the same range, representation, and behavior as ‘‘plain’’ char"

(no subject)

Date: 2008-08-05 07:59 pm (UTC)
From: [identity profile] luyer.livejournal.com
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.

April 2023

S M T W T F S
      1
2345678
91011121314 15
16171819202122
23242526272829
30      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 6th, 2025 04:01 pm
Powered by Dreamwidth Studios