This question already has an answer here:
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.
This question already has an answer here:
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.
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.
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 theCHAR_BIT
macro to get the number of bits in the compiler target'schar
data type.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.
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
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.
The number of bits in a
char
generally 8 (one byte/octet). The exact number is defined in the header<climits>
asCHAR_BIT
.