Does sizeof(char) always equal to 1?

S

Sunner Sun

Hi, all

FAQ of comp.lang.c said "In C, type char is defined as occupying one
byte, so it is usually 8 bits".
(http://www.eskimo.com/~scs/cclass/notes/sx2a.html)

But I found in C99 that "An object declared as type char is large
enough to store any member of the basic execution character set."(6.2.5
Types)

C99 means that if the basic execution character set is UNICODE, then
sizeof(char) should be 2. Is it right?

I'm mazed, :(
 
M

Mark F. Haigh

Sunner said:
Hi, all

FAQ of comp.lang.c said "In C, type char is defined as occupying one
byte, so it is usually 8 bits".
(http://www.eskimo.com/~scs/cclass/notes/sx2a.html)

But I found in C99 that "An object declared as type char is large
enough to store any member of the basic execution character set."(6.2.5
Types)

C99 means that if the basic execution character set is UNICODE, then
sizeof(char) should be 2. Is it right?

An implementation is free to make the number of bits in a character
whatever it wants, including 16. See the CHAR_BIT macro. Note that
sizeof(char) is always 1 no matter how many bits a character occupies.

However, most systems use UTF-8 or wide characters for Unicode, and use
a CHAR_BIT of 8 (which IIRC is required for POSIX compliance).



Mark F. Haigh
(e-mail address removed)
 
M

Malcolm

Sunner Sun said:
C99 means that if the basic execution character set is UNICODE, then
sizeof(char) should be 2. Is it right?

I'm mazed, :(
K and R decided to use the word "char" instead of "byte" for the basic unit
of storage.
In retrospect that was a mistake, but now we're stuck with it.
 
M

Mark McIntyre

Hi, all

FAQ of comp.lang.c said "In C, type char is defined as occupying one
byte, so it is usually 8 bits".
(http://www.eskimo.com/~scs/cclass/notes/sx2a.html)

But I found in C99 that "An object declared as type char is large
enough to store any member of the basic execution character set."(6.2.5
Types)

C99 means that if the basic execution character set is UNICODE, then
sizeof(char) should be 2. Is it right?

No. Sizeof(char) is by definition 1. If that happens to be 16 bits,
then so be it.

By the way, as far as C is concerned, the size of a byte is the same
as the size of a char.

Also by the way, note that 'byte' != 'octet' .
 
S

Sunner Sun

Thank you for all your replies.

Then, if CHAR_BIT is 16, and I want to define a variable which is 8-bit
byte. How can I do?
 
J

Joona I Palaste

Sunner Sun said:
Thank you for all your replies.
Then, if CHAR_BIT is 16, and I want to define a variable which is 8-bit
byte. How can I do?

You can't. It is impossible to define a variable whose size is less than
that of char. In structs, it's possible to define members as
"bitfields", but the whole struct will take up at least the size of char
anyway.
 
E

Emmanuel Delahaye

Sunner Sun wrote on 22/05/05 :
Thank you for all your replies.

Then, if CHAR_BIT is 16, and I want to define a variable which is 8-bit
byte. How can I do?

You can't. The C language doesn't require that such a type exists. A
char must be at least 8-bit width, not exactly 8-bit. In C99, the exact
witdh types are not required. The standard only says that if the
architecture supports them, they can exist and are named uint8_t and
the like...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
M

Mark McIntyre

Thank you for all your replies.

Then, if CHAR_BIT is 16, and I want to define a variable which is 8-bit
byte. How can I do?

You can't, not using Standard C. There may be a platform-specific
workaround which lets you address nybbles but you'd have to read the
hardware specs to see.
 
P

Paul Mesken

Thank you for all your replies.

Then, if CHAR_BIT is 16, and I want to define a variable which is 8-bit
byte. How can I do?

I would be most surprised if char is greater in size than the smallest
addressable unit of the system (SAL). Although I don't recall the
Standard saying anything about char being the SAL.

If char is 16 bits then that is probably the SAL of the underlying
system (although I do not know such a system) and it will be
impossible to have a variable smaller than that simply because the
system itself doesn't have smaller units than 16 bits in memory.
 
J

Jack Klein

Sunner Sun wrote on 22/05/05 :

You can't. The C language doesn't require that such a type exists. A
char must be at least 8-bit width, not exactly 8-bit. In C99, the exact
witdh types are not required. The standard only says that if the
architecture supports them, they can exist and are named uint8_t and
the like...

Slight correction:

The C99 standard states that if an implementation has exact width 8,
16, 32, and 64 bit types with no padding bits, and if they use 2's
complement representation for negative values in the signed variants
of these types, they MUST define the exact width types.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top