This question already has an answer here:
-
Will a `char` always-always-always have 8 bits?
7 answers
As per I know that 1 char = 1 byte = 8 bits(32 bit system).
char c=0xffff0000; //wrong
then why char
allow just 8 bits and also every character in a file also of 8 bit length.
thanks.
No. The sizeof char is by definition 1. But this does not mean that it occupies 32-bits/8-bits always.
$3.9.1/1- "Objects declared as characters (char) shall be large
enough to store any member of the
implementation’s basic character set."
There appears to be a confusion that a byte is 8-bits. The C++ Standard does not mandate this however.
Here's how byte is defined in the Standard $1.7/1
The fundamental storage unit in the C
+ + memory model is the byte. A byte is at least large enough to contain
any member of the basic execution
character set and is composed of a
contiguous sequence of bits, the
number of which is
implementation-defined.
As is clear, a byte need not be always 8-bits.
Just because a system is classified as "32 bit" doesn't mean it uses 32-bit bytes.
A byte is often defined (in a system-dependent way) as the smallest addressable piece of memory, and for many architectures that is still 8 bits, even though the architectures (like x86 or x86-64) are capable of working with larger amounts of data in registers (32 vs 64, respectively). If you're into this thinking, you often use the word "octet" to talk about 8-bit quantities, since the meaning of "byte" changes with the architecture being discussed.
In contrast, for some people "a byte" is defined as always being 8 bits, but then the confusion in the question would probably never happen since they wouldn't expect char
on e.g. a 32-bit system to be 32 bits.
Of course, the entire idea of classifying a system as "n-bit" is oversimplifying things quite a lot.
In C, you can always #include <limits.h>
and then use the CHAR_BIT
macro to get the number of bits in the compiler target's char
data type.
a char is always a byte and always has size 1.
A byte always has at least 8 bits but can have more on some systems.
A 32-bit system refers to the size of the address-bus, in C or C++ you can think of this as the size of a pointer, not the size of a byte.
char has CHAR_BIT
bits [from #include <climits>
]
On 80x86 machines I have always seen this as 8-bits.
On a TMS320C54x and TMS320C55x DSP's I have seen it as 16-bit. This was a pain because to save memory, strings had to be packed with two ASCII characters held in each char!
Always, sizeof(char) == 1
The number of bits in a char
generally 8 (one byte/octet). The exact number is defined in the header <climits>
as CHAR_BIT
.
One byte is most certainly NOT 32 bits. A byte is always 8 bits, no matter what system you're on.
A system that is "32-bit" means that the "word" size is 32 bits. In other words, data is transferred around the system in 32-bit chunks.
In addition to points made already - note that sizeof(char)
and the size of a character are not always the same.
Multibyte character sets can take > 1 byte per character - for example, a Unicode character always takes up more than one byte (sizeof(wchar_t)
).
Microsoft docs on this topic are here. To add to the confusion, some character sets don't even use a fixed number of bytes for each character.