Ç常见算术转换 [英] C usual arithmetic conversions

查看:87
本文介绍了Ç常见算术转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到大约通常的算术转换C99标准。

I was reading in the C99 standard about the usual arithmetic conversions.

如果两个操作数具有相同的类型,则没有进一步转化为
  必要的。

If both operands have the same type, then no further conversion is needed.

否则,如果两个操作数有符号整数类型或两者都有
  无符号整型,具有较低整数的类型的操作数
  转换秩被转换为操作数的类型以更大的
  军衔。

Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.

否则,如果具有无符号整型操作数的秩
  大于或等于另一个操作数的类型的秩,然后
  有符号整数类型的操作数转换为的类型
  操作与无符号整型。

否则,如果操作数的带符号整数类型的类型可以
  再present所有无符号的操作数的类型的值
  整数类型,然后用无符号整型操作数转换
  与符号整型操作数的类型。

Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

否则,两个操作数被转换为无符号整型
  对应于与符号整型操作数的类型

Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

所以我们可以说我有以下的code:

So let's say I have the following code:

#include <stdio.h>

int main()
{
    unsigned int a = 10;
    signed int b = -5;

    printf("%d\n", a + b); /* 5 */
    printf("%u\n", a + b); /* 5 */
    return 0;
}

我以为加粗段适用(因为 unsigned int类型符号int 具有相同的排名,为什么ISN T b转换为无符号?也许它转换为无,但有件事我不明白?

I thought the bolded paragraph applies (since unsigned int and signed int have the same rank. Why isn't b converted to unsigned ? Or perhaps it is converted to unsigned but there is something I don't understand ?

感谢您的时间: - )

Thank you for your time :-)

推荐答案

0X0000000A 加上 0xfffffffb 永远是 0x00000005 无论你面对的符号或无符号的类型,只要只有32位被使用。

0x0000000a plus 0xfffffffb will always be 0x00000005 regardless of whether you are dealing with signed or unsigned types, as long as only 32 bits are used.

这篇关于Ç常见算术转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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