473,657 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof(char)

I have a C book that says the sizeof a char is one byte. Is this true for
C++?
Jul 22 '05 #1
5 16560
"nsgi_2004" <no****@nospam. com> wrote in message
news:X1xvc.4292 9$mm1.41139@fed 1read06...
I have a C book that says the sizeof a char is one byte. Is this true for
C++?


Yes.
Jul 22 '05 #2
"nsgi_2004" <no****@nospam. com> wrote...
I have a C book that says the sizeof a char is one byte. Is this true for
C++?


sizeof(char) is always 1, yes. 'char' and 'byte' are synonyms in
both C and C++ when sizes are concerned.

V
Jul 22 '05 #3
On Wed, 2 Jun 2004 20:37:24 -0700 in comp.lang.c++, "nsgi_2004"
<no****@nospam. com> wrote,
I have a C book that says the sizeof a char is one byte. Is this true for
C++?


Yes. Note that a byte or char must be AT LEAST 8 bits in size,
but can be any number larger.

Jul 22 '05 #4
"nsgi_2004" <no****@nospam. com> wrote in message news:<X1xvc.429 29$mm1.41139@fe d1read06>...
I have a C book that says the sizeof a char is one byte. Is this true for
C++?


Yes, because C++ redefines byte to mean the size of a char. It's not
always the 8-bit thingy you might expect. This is shared between C
and C++, It's relevant e.g. for some DSP chips inside cellphones
that have 32 bits per byte. Now, those are programmed in C not C++,
but C++ felt no need to differ from C here. If anyone writes a
C++ compiler for such things, they can still use the same 32-bit
char.

Regards,
Michiel Salters
Jul 22 '05 #5
Michiel Salters wrote:
"nsgi_2004" <no****@nospam. com> wrote in message news:<X1xvc.429 29$mm1.41139@fe d1read06>...
I have a C book that says the sizeof a char is one byte. Is this true for
C++?

Yes, because C++ redefines byte to mean the size of a char. It's not
always the 8-bit thingy you might expect. This is shared between C
and C++, It's relevant e.g. for some DSP chips inside cellphones
that have 32 bits per byte. Now, those are programmed in C not C++,
but C++ felt no need to differ from C here. If anyone writes a
C++ compiler for such things, they can still use the same 32-bit
char.

Regards,
Michiel Salters


Yep. If you want to specify an 8-bit quantity, you might want to follow
the lead of the internet RFC guys and call it an "octet". sizeof(char)
is 1. Period. Regardless of the actual number of bits in it. Just for
reference, IIRC from my original K&R, one of the early C implementations
was on a machine with 9-bit "bytes".
Jul 22 '05 #6

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));
9
4970
by: Sunner Sun | last post by:
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)
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?
0
412
by: davidb | last post by:
Hi, does someone know how to get the length of a 2 dimensional string array: here what i need: ---------------------------------------------------------------- char **getList(void){ char **myString= (char **) malloc (sizeof (char *));
1
2303
by: svlsr2000 | last post by:
Here is a small program which i saw a couple of years ago. #include<stdio.h> int main() { char x; if(sizeof(int)==sizeof('a')) printf("\n C compiler\n"); else
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
8392
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
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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
8732
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...
0
7324
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
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
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
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.