我了解到,在C语言中char类型的范围是-128到127,但是看起来不像那样 [英] I learned that in C language char type ranges from -128 to 127, but it doesn't seem like that

查看:89
本文介绍了我了解到,在C语言中char类型的范围是-128到127,但是看起来不像那样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常基本的问题,但我无法解决.这就是我的工作.

This might be a very basic problem, but I couldn't manage to. Here is what I am working with.

#include <stdio.h>

int main(void)
{
    char c1, c2;
    int s;
    c1 = 128;
    c2 = -128;

    s = sizeof(char);

    printf("size of char: %d\n", s);
    printf("c1: %x, c2: %x\n", c1, c2);
    printf("true or false: %d\n", c1 == c2);
}

结果是这样的.

size of char: 1
c1: ffffff80, c2: ffffff80
true or false: 1

我将值128分配给带符号(普通)的char类型,但没有溢出.

i assigned the value 128 to signed(normal) char type, but it didn't overflow.

此外,c1和c2似乎都保留4个字节,而-128和128是相同的值.

In additon, c1 and c2 both seems to hold 4bytes, and -128 and 128 are the same value.

我如何理解这些事实?我需要你的帮助.非常感谢.

How can I understand these facts? I need your help. Thank you very much.

推荐答案

c1 = 128; 中,128不适合您的带符号八位 char C实现的用途.按照C 2018 6.5.16.1 2将128转换为 char :将右操作数的值转换为赋值表达式的类型…"

In c1 = 128;, 128 does not fit in the signed eight-bit char that your C implementation uses. 128 is converted to char per C 2018 6.5.16.1 2: "the value of the right operand is converted to the type of the assignment expression…"

根据6.3.1.3 3:转换是实现定义的:否则,将对新类型进行签名,并且不能在其中表示值;结果要么是实现定义的,要么是实现定义的信号被提出."您的C实现将128(即10000000 2 )作为无符号二进制数字转换为−128,当对符号二进制使用二进制补码时,它用相同的位表示.因此,结果是 c1 包含值-128.

The conversion is implementation-defined, per 6.3.1.3 3: "Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised." Your C implementation converted 128, which is 100000002 as an unsigned binary numeral, to −128, which is represented with the same bits when using two’s complement for signed binary. Thus, the result is that c1 contains the value −128.

printf("c1:%x,c2:%x \ n",c1,c2); 中, c1 转换为 int .这是因为根据6.5.2.2 7:使用 ... 参数调用函数的规则是将默认参数提升应用于相应的参数,"默认参数提升是对尾随参数执行的.

In printf("c1: %x, c2: %x\n", c1, c2);, c1 is converted to an int. This is because the rules for calling functions with ... parameters are to apply the default argument promotions to the corresponding arguments, per 6.5.2.2 7: "The default argument promotions are performed on trailing arguments."

根据6.5.2.2,默认的参数提升包括整数提升.6.在大多数C实现中,当 char 的范围比 int 窄时,整数促销按6.3.1.1 2将 char 转换为 int :如果 int 可以表示原始类型的所有值,则……,该值将转换为 int …"

The default argument promotions include the integer promotions, per 6.5.2.2 6. When the range of char is narrower than int, as it is in most C implementations, the integer promotions convert a char to an int, per 6.3.1.1 2: "If an int can represent all values of the original type…, the value is converted to an int…"

因此,在 printf("c1:%x,c2:%x \ n",c1,c2); 中,传递了-128的 int 值作为第二个论点.您的C实现使用 int 的32位二进制补码,其中−128用11111111111111111111111110000000位表示,我们可以用十六进制表示为ffffff80.

Thus, in printf("c1: %x, c2: %x\n", c1, c2);, an int value of −128 is passed as the second argument. Your C implementation uses 32-bit two’s complement for int, in which −128 is represented with the bits 11111111111111111111111110000000, which we can express in hexadecimal as ffffff80.

格式字符串使用%x 指定转换.%x 的正确参数类型是 unsigned int .但是,您的C实现已接受 int 并将其位重新解释为 unsigned int .因此,位11111111111111111111111110000000被转换为字符串"ffffff80".

The format string specifies a conversion using %x. The proper argument type for %x is unsigned int. However, your C implementation has accepted the int and reinterpreted its bits as an unsigned int. Thus, the bits 11111111111111111111111110000000 are converted to the string "ffffff80".

这说明了为什么打印"ffffff80"的原因.这不是因为 c1 具有四个字节,而是因为在将其传递给 printf 之前已将其转换为四字节类型.此外,将负值转换为该四字节类型会导致四个字节设置了很多位.

This explains why "ffffff80" is printed. It is not because c1 has four bytes but because it was converted to a four-byte type before being passed to printf. Further, the conversion of a negative value to that four-byte type resulted in four bytes with many bits set.

关于将 c1 == c2 评估为真(1),这仅仅是因为如上所述,给 c1 赋予了值-128,并且 c2 =-128; 还将值-128分配给 c2 ,因此 c1 c2 具有相同的值.

Regarding c1 == c2 evaluating to true (1), this is simply because c1 was given the value −128 as explained above, and c2 = -128; also assigns the value −128 to c2, so c1 and c2 have the same value.

这篇关于我了解到,在C语言中char类型的范围是-128到127,但是看起来不像那样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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