char size confusion [duplicate]

2019-01-18 07:43发布

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.

标签: c++ char byte bit
8条回答
冷血范
2楼-- · 2019-01-18 08:25

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.

查看更多
Viruses.
3楼-- · 2019-01-18 08:29

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-18 08:33

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.

查看更多
Melony?
5楼-- · 2019-01-18 08:34

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 star\"
6楼-- · 2019-01-18 08:39

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.

查看更多
▲ chillily
7楼-- · 2019-01-18 08:42

The number of bits in a char generally 8 (one byte/octet). The exact number is defined in the header <climits> as CHAR_BIT.

查看更多
登录 后发表回答