473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Does sizeof(char) always equal to 1?

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, :(

Nov 14 '05 #1
9 4932
Sunner Sun wrote:
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
mf*****@sbcglobal.net

Nov 14 '05 #2

"Sunner Sun" <su****@gmail.com> wrote
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.
Nov 14 '05 #3
On 21 May 2005 22:53:18 -0700, in comp.lang.c , "Sunner Sun"
<su****@gmail.com> wrote:
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' .

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #4
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?

Nov 14 '05 #5
Sunner Sun <su****@gmail.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"C++. C++ run. Run, ++, run."
- JIPsoft
Nov 14 '05 #6
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"

Nov 14 '05 #7
On 22 May 2005 00:43:57 -0700, in comp.lang.c , "Sunner Sun"
<su****@gmail.com> wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #8
On 22 May 2005 00:43:57 -0700, "Sunner Sun" <su****@gmail.com> wrote:
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.
Nov 14 '05 #9
On Sun, 22 May 2005 13:18:03 +0200, "Emmanuel Delahaye"
<em***@YOURBRAnoos.fr> wrote in comp.lang.c:
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...


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: José de Paula | last post by:
Is it a given that sizeof(char) always yields 1, no matter the implementation? I ask because I saw some critics against char * ptr = malloc (sizeof(char) * NUM); in favour of simply char * ptr...
9
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
12
by: Olaf \El Blanco\ | last post by:
How many bits we need to save the "number" 1 in a C text file? 00000001? 1 byte? And the "number" 12? Is also a byte? 00001100? And the "number" 300?
2
by: arnaudk | last post by:
Why is sizeof(char*) = 4? I would have expected 1... Is it because a pointer is stored as an int ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.