char and unsigned char

M

Magix

Hi,

char is 8 bit from -127 to 127
unsigned char is 8 bit from 0-255

from ASCII table, there is a list of char from 0-255. How char can be in
negative value ?
If I declare "char x", then can x hold char from 128-255 ?

If want to hold value from 0-127 only?

Thanks.
 
J

j

Magix said:
Hi,

char is 8 bit from -127 to 127
unsigned char is 8 bit from 0-255

char is CHAR_BIT bits, where CHAR_BIT can be more than eight.
from ASCII table, there is a list of char from 0-255. How char can be in
negative value ?

It is up to the implementation to decide whether
``char'' is ``unsigned'' or ``signed''.
If I declare "char x", then can x hold char from 128-255 ?

Maybe, if ``char'' is implemented as ``unsigned char'' with your
particular implementation.
If want to hold value from 0-127 only?

Then use unsigned char.
 
J

j

j said:
char is CHAR_BIT bits, where CHAR_BIT can be more than eight.


It is up to the implementation to decide whether
``char'' is ``unsigned'' or ``signed''.


Maybe, if ``char'' is implemented as ``unsigned char'' with your
particular implementation.


Then use unsigned char.

I should also say that the standard does not dictate a range
with respect to the values of an unsigned char, or signed char.

It dictates an upper bound though, which is CHAR_BIT bits.
Your implementation is free to implement a trivial mapping
scheme so that char foo = 88; can very well be 600+foo.
 
J

Jonathan Adams

Hi,

char is 8 bit from -127 to 127

No -- char on 8-bit 2's complement machines is either -128 to 127 or
0-255, depending on the C implementation.
unsigned char is 8 bit from 0-255

from ASCII table, there is a list of char from 0-255. How char can be in
negative value ?

<slightly OT>
You can view any 8-bit number as either an "unsigned" number from
0->255, or as a "signed" number from 0->127 and -128 -> -1. Most every
computer chip nowadays uses the "2's complement" representation for
negative numbers. The algorithm for inverting (i.e. multiplying by -1) a
value 'x' in the two's complement representation is:

flip all of the bits of x
add 1

or, in C notation:

(~x + 1) == -x

In this representation, unsigned "128" == signed "-128", and
If I declare "char x", then can x hold char from 128-255 ?

Depends on your platform -- char x could either be signed or unsigned.
If want to hold value from 0-127 only?

Make sure you don't put anything larger than 127 into it.

- jonathan
 
M

Martin Ambuhl

Magix said:
Hi,

char is 8 bit from -127 to 127
Wrong. signed char has a range of at least -127 to +127.
char may be either signed char or unsigned char.
No variety of char is constrained to be 8 bits.
unsigned char is 8 bit from 0-255
Wrong. unsigned char has a range of at least 0 to 255 and is not
constrained to be 8 bits.
from ASCII table, there is a list of char from 0-255.

Wrong. ASCII extends only from 0 to 127. There are a wide variety of
so-called extensions into the range 128-255, but they greatly differ
from one another and are none of them ASCII.
How char can be in
negative value ?
If I declare "char x", then can x hold char from 128-255 ?

If you declare "unsigned char x" then x can have a range from 128-255,
but those codes do not represent ASCII encodings.
If want to hold value from 0-127 only?

Since the ranges for both signed and unsigned chars, and hence unadorned
chars, include every value in 0-127, use whatever kind of char you want.
If you intend to do bit arithmetic (masking, anding, etc.) use
unsigned char.
 
P

pete

The standard dictates minimum ranges of types, and 0-127 inclusive,
is within the minimum guaranteed range of char.
I should also say that the standard does not dictate a range
with respect to the values of an unsigned char, or signed char.

I wouldn't use a smaller integer type than int,
unless there was a special reason,
like if I was going to store many of them in an array.
 
M

Mike Wahler

j said:
Then use unsigned char.

And if you need to specifically disallow values > 127,
do so in your code (use 'if', or mask the possible offending bits
away).

-Mike
 
O

Old Wolf

j said:
I should also say that the standard does not dictate a range
with respect to the values of an unsigned char, or signed char.

Actually it does, if you look in the specification for <limits.h>

SCHAR_MIN must be at most -127
SCHAR_MAX must be at least 127
UCHAR_MAX must be at least 255
It dictates an upper bound though, which is CHAR_BIT bits.
Your implementation is free to implement a trivial mapping
scheme so that char foo = 88; can very well be 600+foo.

No it isn't. The value must be 88. I think you are trying
to say that the representation can be something other than
pure binary. In C99 this is wrong, see 6.2.6.1/3 and 6.2.6.2/2.
I don't know if C90 has this same restriction (but would be
surprised if there were any implementations that broke this rule).
 
J

Jack Klein

No -- char on 8-bit 2's complement machines is either -128 to 127 or
0-255, depending on the C implementation.

No, signed char on 8 bit 2's complement machines is only required to
hold the range of values -127 to 127 inclusive. It is not required to
hold the value -128. The current C standard specifically states that
on object of a signed integer type on two's complement platforms that
has the sign bit 1 and all value bits 0 may be a trap representation.

Specifically see section 6.2.6.2 paragraphs 1 and 2.
 
J

Jonathan Adams

Jack Klein said:
No, signed char on 8 bit 2's complement machines is only required to
hold the range of values -127 to 127 inclusive. It is not required to
hold the value -128. The current C standard specifically states that
on object of a signed integer type on two's complement platforms that
has the sign bit 1 and all value bits 0 may be a trap representation.

Specifically see section 6.2.6.2 paragraphs 1 and 2.

Ah, my mistake. Apologies,

- jonathan
 

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
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top