char size confusion [duplicate]

五迷三道 提交于 2019-12-28 13:57:57

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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




回答6:


1 byte = 8 bits




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/4266771/char-size-confusion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!