Re: signed and unsigned char
In <c0gehn$ek0$1@c hessie.cirr.com > Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
[color=blue]
>signed char str_a[]="Hello, world!\n";
>unsigned char str_b[]="Hello, world!\n";
>
>what is the difference, if any, between the following two statements?
>
>printf( "%s", str_a );
>printf( "%s", str_b );[/color]
None.
s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type.
^^^^^^^^^^^^^^
Both str_a and str_b are arrays of character type.
[color=blue]
>If there is a difference, what is the best way to compare *str_a with
>0xFF? (On my implementation, unadorned char is signed, and so I'm
>using
>
>if( *str_a == (signed char)0xFF ) ...
>
>to quiet compiler warnings.)[/color]
It's not clear what exactly you want to achieve here. If you want to see
if the respective character value has a certain representation, the most
portable approach is to use a pointer to unsigned char:
if( *(unsigned char *)str_a == 0xFF ) ...
This works even if this pattern is a trap representation for the type
signed char.
OTOH, if you want to check that your character has a certain value,
simply compare against that value:
if( *str_a == -1 ) ...
Comparing an object with a value it cannot possibly take, as in your
example, doesn't make much sense a priori, so you have to explain your
exact intentions.
BTW, if str_a were an array of plain char, you had the following solution:
if( *str_a == '\xff' ) ...
but still not guaranteed to work if this bit pattern is a trap
representation for plain char.
As other people have already mentioned, (signed char)0xFF is useless for
your purpose, in a portability context, because the result need not be
the signed char value corresponding to that bit pattern. Casts really
are *conversion* operators and not devices for silencing the compilers.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
In <c0gehn$ek0$1@c hessie.cirr.com > Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
[color=blue]
>signed char str_a[]="Hello, world!\n";
>unsigned char str_b[]="Hello, world!\n";
>
>what is the difference, if any, between the following two statements?
>
>printf( "%s", str_a );
>printf( "%s", str_b );[/color]
None.
s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type.
^^^^^^^^^^^^^^
Both str_a and str_b are arrays of character type.
[color=blue]
>If there is a difference, what is the best way to compare *str_a with
>0xFF? (On my implementation, unadorned char is signed, and so I'm
>using
>
>if( *str_a == (signed char)0xFF ) ...
>
>to quiet compiler warnings.)[/color]
It's not clear what exactly you want to achieve here. If you want to see
if the respective character value has a certain representation, the most
portable approach is to use a pointer to unsigned char:
if( *(unsigned char *)str_a == 0xFF ) ...
This works even if this pattern is a trap representation for the type
signed char.
OTOH, if you want to check that your character has a certain value,
simply compare against that value:
if( *str_a == -1 ) ...
Comparing an object with a value it cannot possibly take, as in your
example, doesn't make much sense a priori, so you have to explain your
exact intentions.
BTW, if str_a were an array of plain char, you had the following solution:
if( *str_a == '\xff' ) ...
but still not guaranteed to work if this bit pattern is a trap
representation for plain char.
As other people have already mentioned, (signed char)0xFF is useless for
your purpose, in a portability context, because the result need not be
the signed char value corresponding to that bit pattern. Casts really
are *conversion* operators and not devices for silencing the compilers.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Comment