本文介绍了为什么这两个程序的行为不同的ANSI C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

可能重复:结果
  

1

main()
{

 if(-1<(unsigned char)1)
     printf("-1 is less than (unsigned char)1:ANSI semantics");
 else
     printf("-1 NOT less than (unsigned char)1:K&R semantics");
}

2

int array[] = {23,41,12,24,52,11};
#define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))
main()
{
    int d = -1,x;
    if(d<=TOTAL_ELEMENTS -2)
        x = array[d+1];
}

第一个转换unsigned char型1到符号变量在ANSI C,
而第二个程序转换d可一个unsigned int,使
条件前pression在ANSI C.返回false
为什么他们不同的表现?

The first convert unsigned char 1 to a signed variable in ANSI C,while the second program convert d to an unsigned int that makes thecondition expression return false in ANSI C.Why did they behave differently?

推荐答案

有关第一个右手边是一个无符号的字符,以及所有无符号的字符值放入一个符号整数,所以它转换为符号整数。

For the first one the right-hand side is an unsigned char, and all unsigned char values fit into a signed int, so it is converted to signed int.

有关第二个右手边是一个unsigned int,因此左侧是由符号int转换为unsigned int类型。

For the second one the right-hand side is an unsigned int, so the left-hand side is converted from signed int to unsigned int.

又见<一个href=\"https://www.securecoding.cert.org/confluence/display/sec$c$c/INT02-C.+Understand+integer+conversion+rules\"相对=nofollow>整数转换这CERT文档。

这篇关于为什么这两个程序的行为不同的ANSI C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:23