数据类型char,取值范围0-255
data type char and its value range mentioned as 0-255
我在 link data type
查看数据类型
写成char类型是1个字节,范围是-128到127或者0到255。
这怎么可能?默认情况下 char
表示签名权。
编辑:还有一个问题。但这不是同一个问题。标题说这段代码有什么问题,搜索不会轻易列出这个答案。问题要分析清楚才能看懂。
编辑:看了几个答案和评论后,我又有了一个疑问。双引号内的字符串被视为 char。如果我将双引号字符串传递给参数类型为 signed char 的函数,我会收到警告。 itoa 和许多其他库函数也使用 char 类型参数而不是 signed char。当然类型转换会避免这个问题。那么对于操作空终止字符串的函数(例如 LCD 显示相关函数)来说,什么是最好的参数类型呢?使用 signed char 或 unsigned char(因为 char 是实现定义的,我猜它可能不可移植)
char
"has the same representation and alignment as either signed char
or unsigned char
, but is always a distinct type".
不,默认情况下并不表示有符号字符。根据 C 标准,char
是与有符号字符和无符号字符不同的类型,其行为仅类似于其他两个字符之一。
The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.
并在上一段的注释中:
CHAR_MIN, defined in <limits.h>
, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options. Irrespective of the choice made, char is a separate type from the other two and is not compatible with either.
char
具有实现定义的符号。这意味着一个编译器可以选择将其实现为 signed
而另一个编译器可以选择将其实现为 unsigned
.
这就是为什么您不应该使用 char
类型来存储数字的原因。更好的类型是 uint8_t
.
我在 link data type
查看数据类型写成char类型是1个字节,范围是-128到127或者0到255。
这怎么可能?默认情况下 char
表示签名权。
编辑:还有一个问题
编辑:看了几个答案和评论后,我又有了一个疑问。双引号内的字符串被视为 char。如果我将双引号字符串传递给参数类型为 signed char 的函数,我会收到警告。 itoa 和许多其他库函数也使用 char 类型参数而不是 signed char。当然类型转换会避免这个问题。那么对于操作空终止字符串的函数(例如 LCD 显示相关函数)来说,什么是最好的参数类型呢?使用 signed char 或 unsigned char(因为 char 是实现定义的,我猜它可能不可移植)
char
"has the same representation and alignment as either signed char
or unsigned char
, but is always a distinct type".
不,默认情况下并不表示有符号字符。根据 C 标准,char
是与有符号字符和无符号字符不同的类型,其行为仅类似于其他两个字符之一。
The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.
并在上一段的注释中:
CHAR_MIN, defined in
<limits.h>
, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options. Irrespective of the choice made, char is a separate type from the other two and is not compatible with either.
char
具有实现定义的符号。这意味着一个编译器可以选择将其实现为 signed
而另一个编译器可以选择将其实现为 unsigned
.
这就是为什么您不应该使用 char
类型来存储数字的原因。更好的类型是 uint8_t
.