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
.
这篇关于什么是无符号字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是无符号字符?
基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01