整数范围与类型转换

-2147483647-1 ==  2147483648U
-2147483647-1 < -2147483647
-2147483647-1 <  2147483647
(unsigned)-2147483647-1 <  2147483647

上面四个表达式成立吗?为什么?并用C语言编程测试并提交运行截图。

代码

#include<stdio.h>
#include<string.h>
#define INT_MAX 2147483647 
#define INT_MIN (-INT_MAX - 1) 
int checktruefalse(int a)
{
    if(a)
    {
         printf("   true\n");
         return 1;
    }
    else
    {
          printf("   false\n");
          return 0;
    }
}

int main()
{
    printf("-2147483647-1 == 2147483648u");checktruefalse(-2147483647-1 == 2147483648u);
    printf("-2147483647-1 < 2147483647");checktruefalse(-2147483647-1 < 2147483647);
    printf("-2147483647-1 < -2147483647");checktruefalse(-2147483647-1 < -2147483647);
    printf("(unsigned)-2147483647-1 < 2147483647");checktruefalse((unsigned)-2147483647-1 < 2147483647);
    return 0;
}

运行结果

posted @ 2022-12-22 10:59  油菜园12号  阅读(30)  评论(0编辑  收藏  举报