What is the difference between char and unsigned char?

不羁岁月 提交于 2019-12-23 17:43:16

问题


(Edited change C/C++ to C)

Please help me to find out a clean clarification on char and unsigned char in C. Specially when we transfer data between embedded devices and general PCs (The difference between buffer of unsigned char and plain char).


回答1:


You're asking about two different languages but, in this respect, the answer is (more or less) the same for both. You really should decide which language you're using though.

Differences:

  • they are distinct types
  • it's implementation-defined whether char is signed or unsigned

Similarities:

  • they are both integer types
  • they are the same size (one byte, at least 8 bits)

If you're simply using them to transfer raw byte values, with no arithmetic, then there's no practical difference.




回答2:


The type char is special. It is not an unsigned char or a signed char. These are three distinct types (while int and signed int are the same types). A char might have a signed or unsigned representation.

From 3.9.1 Fundamental types

Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation.



来源:https://stackoverflow.com/questions/22629728/what-is-the-difference-between-char-and-unsigned-char

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