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

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

问题描述

我正在阅读有关常用算术转换的 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.

否则,如果无符号整数类型的操作数有秩大于或等于另一个操作数的类型的等级,则带符号整数类型的操作数转换为无符号整数类型的操作数.

否则,如果带符号整数类型的操作数的类型可以用无符号表示操作数类型的所有值整数类型,然后转换无符号整数类型的操作数为有符号整数类型的操作数的类型.

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.

假设我有以下代码:

#include <stdio.h>

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

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

我认为加粗的段落适用(因为 unsigned intsigned int 具有相同的等级.为什么不将 b 转换为 unsigned?或者也许它被转换为无符号但有一些我不明白的东西?

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.

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

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