隐式数字类型转换的规则

时间:2017-09-17 20:41:23

标签: c++

我有几个关于带有符号和无符号变量的类型转换的问题。 在这种情况下将选择哪种类型 signed int + unsigned int = ??? 那么当代替变量我有常量时的情况呢

x1(unsigned int) = x1(unsigned int) - 0xFFFFFFFA

我读过类似常量的内容unsigned ints,除非明确写成0xFFFFFFFA(UL)。然后

1) unsigned int - unsigned int = unsigned int
2) unsigned int = unsigned int

如果浮点数为

,该怎么办?
 x1(unsigned int) = x2(signed short int) + x3(unsigned int) + x4(unsigned short int) 
 * 0.1(float);  
 1)float * unsigned short int = float
 2)float + unsigned int = float 
 3)float + signed short int = float
 4)unsigned int = (unsigned int)float

我想这是' a'将是char

signed int = 'a' + signed short int - signed int 
1) 'a' + signed short int = int ???
2) int - signed int = int ???
3) signed int = (signed int) int ???

还有一个

long double = signed int + wchar_t - unsigned int * 10 
1)unsigned int * 10(int) = int
2)wchar_t - int = int
3)signed int + int = int 
4)long double = (long double) int

1 个答案:

答案 0 :(得分:2)

你可以随时使用我评论过的技巧:

struct {} _ = some_expression;

编译器错误会告诉您表达式的确切类型。

对于背景资料,请参阅http://cppreference.com,特别是Implicit Conversion

您还应该阅读标准中的conv.promconv.double