将char与c语言中的unsigned short进行比较时的行为是什么? [英] what is behavior when char is compared with unsigned short in c language?

查看:267
本文介绍了将char与c语言中的unsigned short进行比较时的行为是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下程序时:

  void func(unsigned short maxNum,unsigned short di){如果(di == 0){返回;}查理一世对于(i = di; i< = maxNum; i + = di){printf(%u",i);}printf("\ n");}int main(int argc,char ** argv){func(256,100);返回0;} 

这是一个无休止的循环,但是我不知道何时将char与unsigned short进行比较,char是否转换为unsigned short?在这种情况下,char溢出并且大于maxNum.我真的不知道如何解释该程序的结果.

解决方案

实施定义的行为未定义的行为 CHAR_MAX<256


让我们整理一下:

  ... unsigned short maxNum...无符号短二查理一世对于(i = di; i< = maxNum; i + = di){printf(%u",i);} 

char 可以是 signed char unsigned char .让我们假设它是 signed .

当两个都是16位时,

unsigned short 的范围可能与 unsigned 相同.然而,更常见的是找到 unsigned short 为16位,而 int,unsigned 为32位.

还有其他可能,但让我们继续以上两个假设.

如果分配的值不在 char 的范围内,但

i = di 可能很有趣,但是100始终在 char 范围内,所以 i 是100.

i< = maxNum 中的每个参数都经过通常的整数促销,因此,首先 signed char i 变为 int 100 ,而16位的 maxNum 变为 int 256 .如 100<256 为true,则输入循环体.注意,由于 CHAR_MAX 小于256,因此 i 永远不会期望值达到256,即使在随后的循环中也是如此-这说明了看到的永远循环.但请等待更多

对于 printf(%u",i); printf()期望匹配的 unsigned 参数.但是 i 作为一种类型,其范围比 int 少,因此被提升为 int ,其值与...自变量的一部分相同.通常,打印不匹配的说明符和类型是未定义行为,但例外情况是:该值可表示为有符号和无符号类型.因为第一次是100,所以一切都很好.

在循环结束时, i + = di 就像 i = i + di; .加法参数经过通常的整数促销,并成为 int 100 添加到 int 100 中.那笔钱是200.到目前为止,没有什么奇怪的.但是,将200分配给 signed char 会掩盖200,因为它超出了范围.这是实施定义的行为.分配的值可以是0或1或2....典型地,通过加/减256直到在范围内,该值被包裹(修改").100 + 100 -256->-56.

但是第二个 printf(%u",i); 尝试打印-56,并且 未定义的行为./p>


提示:启用所有警告,好的编译器会指出很多这样的问题,并节省您的时间.

When I run the following program:

void func(unsigned short maxNum, unsigned short di)
{
    if (di == 0) {
        return;
    }

    char i;

    for (i = di; i <= maxNum; i += di) {
        printf("%u ", i);
    }

    printf("\n");
}

int main(int argc, char **argv)
{
    func(256, 100);
    return 0;
}

It is endless loop, but i wonder when char is compared with unsigned short, is char translated to unsigned short? In this situation, char is overflow and larger than maxNum. I really do not know how to explain the results of this program.

解决方案

Implementation defined behavior, Undefined behavior and CHAR_MAX < 256


Let us sort out:

... unsigned short maxNum
... unsigned short di
char i;
for (i = di; i <= maxNum; i += di) {
    printf("%u ", i);
}

char may be a signed char or an unsigned char. Let us assume it is signed.

unsigned short may have the same range as unsigned when both are 16-bit. Yet it is more common to find unsigned short as 16-bit and int, unsigned as 32-bit.

Other possibles exist, yet let us go forward with the above two assumptions.

i = di could be interesting if the value assigned was outside the range of a char, but 100 is always within char range, so i is 100.

Each argument in i <= maxNum goes through usual integer promotions so the signed char i first becomes an int 100 and the 16-bit maxNum becomes an int 256. As 100 < 256 is true, the loop body is entered. Notice i would never expect to have a value as large as 256 since CHAR_MAX is less than 256 - even on following loops - This explains the seen forever loop. But wait there's more

With printf("%u ", i);, printf() expects a matching unsigned argument. But i as a type with less range then int gets promoted to a int with the same value as part of a ... argument. Usually printing mis-matched specifiers and type is undefined behavior with an exception: when the value is representable as both a signed and unsigned type. As 100 is the first time, all is OK.

At the loop end, i += di is like i = i + di;. The addition arguments go through usual integer promotions and become int 100 added to int 100. That sum is 200. So far nothing strange. Yet assigning a 200 to a signed char coverts the 200 as it is out of range. This is implementation defined behavior. The assigned value could have been 0 or 1 or 2.... Typically, the value is wrapped around ("modded") by adding/subtracting 256 until in range. 100 + 100 -256 --> -56.

But the 2nd printf("%u ", i); attempts printing -56 and that is undefined behavior.


Tip: enable all warnings, Good compilers will point out many of these problems and save you time.

这篇关于将char与c语言中的unsigned short进行比较时的行为是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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