C语言类型转换优先级

最近在工作中遇到了一个溢出问题。问题是这样:

#include <stdio.h>
#include <stdlib.h>

typedef signed long long    I64;
typedef unsigned long long  U64;
typedef double              F64;

int main()
{
    F64 amount1 = -4000000;
    U64 volume  = 2;
    I64 res1    = amount1 / volume;
    printf("res1:%lld\n", res1);

    I64 amount2 = -4000000;
    I64 res2    = amount2 / volume;
    printf("res2:%lld\n", res2);

    I64 res3    = amount2 / (I64)volume;
    printf("res3:%lld\n", res3);

    return 0;
}

输出结果为:

res1:-2000000
res2:9223372036852775808
res3:-2000000

可以看出类型转换的优先级为:

F64 > U64 > I64


标题:C语言类型转换优先级
作者:staymeloo7
联系方式:staycoolsun@gmail.com

    评论
    0 评论
avatar

取消