char和无符号char的区别是什么?

时间:2022-02-24 11:36:57

(Edited change C/C++ to C)

(将C/ c++编辑为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).

请帮我查清楚c中的char和unsigned char,特别是我们在嵌入式设备和普通pc之间传输数据时(unsigned char和plain char之间的缓冲区差异)。

2 个解决方案

#1


8  

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
  • 它的实现定义了char是有符号的还是无符号的

Similarities:

相似之处:

  • they are both integer types
  • 它们都是整数类型
  • they are the same size (one byte, at least 8 bits)
  • 它们大小相同(一个字节,至少8位)

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

如果您只是简单地使用它们来传输原始字节值,而不使用算术运算,则没有实际的区别。

#2


3  

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.

类型char是特殊的。它不是无符号字符或有符号字符。这是三种不同的类型(int和带符号的int是相同的类型)。字符可能有一个带符号或无符号表示。

From 3.9.1 Fundamental types

从3.9.1基本类型

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.

普通字符、带符号字符和无符号字符是三种不同的类型。一个char、一个带符号char和一个无符号char占用相同的存储量,并且具有相同的对齐要求(3.11);也就是说,它们有相同的对象表示。

#1


8  

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
  • 它的实现定义了char是有符号的还是无符号的

Similarities:

相似之处:

  • they are both integer types
  • 它们都是整数类型
  • they are the same size (one byte, at least 8 bits)
  • 它们大小相同(一个字节,至少8位)

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

如果您只是简单地使用它们来传输原始字节值,而不使用算术运算,则没有实际的区别。

#2


3  

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.

类型char是特殊的。它不是无符号字符或有符号字符。这是三种不同的类型(int和带符号的int是相同的类型)。字符可能有一个带符号或无符号表示。

From 3.9.1 Fundamental types

从3.9.1基本类型

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.

普通字符、带符号字符和无符号字符是三种不同的类型。一个char、一个带符号char和一个无符号char占用相同的存储量,并且具有相同的对齐要求(3.11);也就是说,它们有相同的对象表示。