unsigned char

L

Lars Tackmann

Does the c standard state that for all c99 implementation char as standard
is equal unsigned char ??? - or do i have to take this into account when
writing my program ???.
 
H

Hallvard B Furuseth

Lars said:
Does the c standard state that for all c99 implementation char as standard
is equal unsigned char ?

Not at all. It's equivalent to either signed char or unsigned char.
The choice is up to the implementation.

It's not _equal_ to either, though. It's a distinct type, so
char *foo;
unsigned char *bar = foo;
is an error even if char is equivalent to unsigned char.
 
I

Irrwahn Grausewitz

Lars Tackmann said:
Does the c standard state that for all c99 implementation char as standard
is equal unsigned char ??? - or do i have to take this into account when
writing my program ???.

The implementation has to pick one from {signed|unsigned} char
for "plain" char:

ISO/IEC 9899:1999 6.2.5#15
The three types char, signed char, and unsigned char are
collectively called the character types. The implementation
shall define char to have the same range, representation,
and behavior as either signed char or unsigned char.

and

ISO/IEC 9899:1999 6.3.1.1#3
The integer promotions preserve value including sign. As
discussed earlier, whether a "plain" char is treated as
signed is implementation-defined.


HTH
Regards
 
T

Thomas Stegen

Hallvard said:
Lars Tackmann wrote:




Not at all. It's equivalent to either signed char or unsigned char.
The choice is up to the implementation.

It's not _equal_ to either, though. It's a distinct type, so
char *foo;
unsigned char *bar = foo;
is an error even if char is equivalent to unsigned char.

Note that pointers to unsigned char can point to any byte in an object.
So if foo had been properly initialised the above would have been legal.

But even
int *p;
int *p2 = p;

is an error since it uses the value of an uninitialised variable.
 
J

Jeremy Yallop

Christopher said:
Is ( p==NULL ) likewise undefined?

Yes. No expression that uses the value of an uninitialized pointer
variable has defined behaviour.

Jeremy.
 
C

Christopher Benson-Manica

Jeremy Yallop said:
Yes. No expression that uses the value of an uninitialized pointer
variable has defined behaviour.

Does that hold true for non-pointer variables as well? Such as

int i;
if( i != 42 ) {
printf( "What a tragedy...\n" );
}
 
J

Jeremy Yallop

Christopher said:
Does that hold true for non-pointer variables as well?

Not in every case. Since the bit pattern of an uninitialized object
is indeterminate, whether the behaviour is defined depends on whether
all

It does, but the particular type of behaviour - undefined,
unspecified, etc. - depends on the type of the variable and possibly
on the implementation. For example, all bit patterns have valid
unsigned char values, so using an uninitialized unsigned char object
in an arithmetic expression never invokes undefined behaviour. Some
such expressions may even have well-defined behaviour; the following
assertion cannot fail.

unsigned char c;
assert(!(c - c));
Such as

int i;
if( i != 42 ) {
printf( "What a tragedy...\n" );
}

It's unspecified whether or not this code has undefined behaviour. On
an implementation where all possible bit patterns represent valid int
values the behaviour is not undefined, because the standard imposes
requirements on it: `i' has either the value 42 or some other valid
value. On an implementation with trap values for int, the code has
undefined behaviour.

Jeremy.
 
C

Christopher Benson-Manica

Jeremy Yallop said:
Not in every case. Since the bit pattern of an uninitialized object
is indeterminate, whether the behaviour is defined depends on whether
all

Lose your train of thought? Did Agent Smith pay you an unwelcome
visit? ;)
It does, but the particular type of behaviour
(etc.)

Glad to see Agent Smith let you live this time. Seriously, thanks.
 
H

Hallvard B Furuseth

Thomas said:
Note that pointers to unsigned char can point to any byte in an object.
So if foo had been properly initialised the above would have been legal.

No, assignment between different pointer types are not legal.
OTOH, using a cast is OK: unsigned char *bar = (unsigned char*) foo;
But even
int *p;
int *p2 = p;

is an error since it uses the value of an uninitialised variable.

Oops. I should have said 'char *foo = "something";'.
 
S

Simon Biber

Thomas Stegen said:
Note that pointers to unsigned char can point to any byte in
an object. So if foo had been properly initialised the above
would have been legal.

Well, no, it would still be a constraint violation because of the
incompatible types in the initialisation. The type (char *) is
never compatible with the type (unsigned char *), and nor is it
ever compatible with the type (signed char *).

To make it legal you can either use a cast, or an intermediate
variable of type (void *) as conversions to and from (void *)
do not require casts.

Example with cast:
char *foo = 0; /* Properly initialise */
unsigned char *bar = (unsigned char *)foo; /* Convert with cast */

Example without cast:
char *foo = 0; /* Properly initialise */
void *bar = foo; /* Convert to (void *) */
unsigned char *baz = bar; /* Convert from (void *) */
But even
int *p;
int *p2 = p;

is an error since it uses the value of an uninitialised variable.

Though this is not a contraint violation, it is merely undefined
behaviour, so the compiler is not required to detect it and
generate a diagnostic message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,122
Messages
2,570,717
Members
47,283
Latest member
VonnieEwan

Latest Threads

Top