char 是否保证正好是 8 位长? [英] Is char guaranteed to be exactly 8-bit long?

查看:31
本文介绍了char 是否保证正好是 8 位长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅此而已.没有找到任何类似的主题,所以请耐心等待.

That's all. Didn't find any similar topic so bear with me it there is.

推荐答案

来自 一份ANSI C 规范,参见第 3.1.2.5 节 - 类型:

声明为 char 类型的对象是大到足以存放任何成员基本执行字符集.如果所需来源的成员$2.2.1 中列举的字符集是存储在一个 char 对象中,它的值是保证是积极的.如果其他数量存储在一个字符中对象,行为是实现定义:值是被视为签署或非负整数.

An object declared as type char is large enough to store any member of the basic execution character set. If a member of the required source character set enumerated in $2.2.1 is stored in a char object, its value is guaranteed to be positive. If other quantities are stored in a char object, the behavior is implementation-defined: the values are treated as either signed or nonnegative integers.

执行字符集"的概念在第 2.2.1 节 - 字符集中介绍.

The concept of "execution character set" is introduced in Section 2.2.1 - Character sets.

换句话说,一个字符必须至少大到足以包含构成基本执行字符集的至少 95 个不同字符的编码.

In other words, a char has to be at least big enough to contain an encoding of at least the 95 different characters which make up the basic execution character set.

现在添加2.2.4.2 - 数值限制

符合要求的实现应记录中指定的所有限制本节,应规定在标题 .

A conforming implementation shall document all the limits specified in this section, which shall be specified in the headers <limits.h> and <float.h> .

整数类型的大小

下面给出的值应为替换为常量表达式适用于#if 预处理指令.他们的实现定义的值应为大小相等或更大(绝对值)到那些显示的,与相同的符号.

The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

  • 最小的最大位数不是位域的对象(字节)
    CHAR_BIT 8

  • maximum number of bits for smallest object that is not a bit-field (byte)
    CHAR_BIT 8

类型对象的最小值有符号字符
SCHAR_MIN -127

minimum value for an object of type signed char
SCHAR_MIN -127

类型对象的最大值有符号字符
SCHAR_MAX +127

maximum value for an object of type signed char
SCHAR_MAX +127

类型对象的最大值无符号字符
UCHAR_MAX 255

maximum value for an object of type unsigned char
UCHAR_MAX 255

....

所以你知道了 - 字符中的位数必须至少 8.

So there you have it - the number of bits in a char must be at least 8.

这篇关于char 是否保证正好是 8 位长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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