What is an unsigned char?(什么是无符号字符?)
问题描述
在 C/C++ 中,unsigned char 用于什么?它与普通的 char 有何不同?
In C/C++, what an unsigned char is used for? How is it different from a regular char?
推荐答案
在 C++ 中,有三种不同的字符类型:
In C++, there are three distinct character types:
char有符号的字符无符号字符
如果您为 text 使用字符类型,请使用不合格的 char:
If you are using character types for text, use the unqualified char:
- 它是字符文字的类型,如
'a'或'0'(仅在 C++ 中,在 C 中它们的类型是int) - 它是组成C字符串的类型,如
"abcde"
- it is the type of character literals like
'a'or'0'(in C++ only, in C their type isint) - it is the type that makes up C strings like
"abcde"
它也可以作为数字值计算,但未指定该值是被视为有符号还是无符号.当心通过不等式进行字符比较 - 尽管如果您将自己限制为 ASCII (0-127),那么您就很安全了.
It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through inequalities - although if you limit yourself to ASCII (0-127) you're just about safe.
如果您将字符类型用作数字,请使用:
If you are using character types as numbers, use:
signed char,它给你至少 -127 到 127 的范围.(-128 到 127 很常见)unsigned char,它给你至少 0 到 255 的范围.
signed char, which gives you at least the -127 to 127 range. (-128 to 127 is common)unsigned char, which gives you at least the 0 to 255 range.
至少",因为 C++ 标准只给出了每个数字类型需要覆盖的最小范围的值.sizeof (char) 需要为 1(即一个字节),但理论上一个字节可以是例如 32 位.sizeof 仍会报告其大小为 1 - 这意味着您可以有 sizeof (char)== sizeof (long) == 1.
"At least", because the C++ standard only gives the minimum range of values that each numeric type is required to cover. sizeof (char) is required to be 1 (i.e. one byte), but a byte could in theory be for example 32 bits. sizeof would still be report its size as 1 - meaning that you could have sizeof (char) == sizeof (long) == 1.
这篇关于什么是无符号字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是无符号字符?
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
