什么是无符号的字符? [英] What is an unsigned char?

查看:290
本文介绍了什么是无符号的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C / C ++,什么是 unsigned char型使用?如何从一个普通此不同的字符

In C/C++, what is an unsigned char used for? How is this different from a regular char?

推荐答案

在C ++中,有三个的不同的的字符类型:

In C++, there are three distinct character types:


  • 字符

  • 符号字符

  • unsigned char型

  • char
  • signed char
  • unsigned char

如果您使用的是字符类型的文本的,使用不合格的字符

If you are using character types for text, use the unqualified char:


  • 这是字符文字像类型'一' 0

  • 这是构成C字符串喜欢的类型ABCDE

  • it is the type of character literals like 'a' or '0'.
  • 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:


  • 符号字符,它给你的至少的-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(字符)==的sizeof(长)== 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.

这篇关于什么是无符号的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆