473,659 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4970
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*****@sbcglob al.net

Nov 14 '05 #2

"Sunner Sun" <su****@gmail.c om> 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.c om> 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.c om> 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.hel sinki.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.c om> 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.c om> 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***@YOURBRAn oos.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.l earn.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
9465
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 = malloc (NUM); -- Quidquid latine dictum sit altum viditur
9
3009
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 identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
3
3286
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
426
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
2596
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
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.