gpt4 book ai didi

c++ - 隐式数值类型转换规则

转载 作者:太空狗 更新时间:2023-10-29 21:34:30 25 4
gpt4 key购买 nike

我有几个关于带符号和无符号变量的类型转换的问题。在这种情况下将选择哪种类型有符号整数 + 无符号整数 = ???
那么当我有常量而不是变量的情况下呢


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

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

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

如果数字是 float 会怎样

 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

最佳答案

你总是可以使用我评论的技巧:

struct {} _ = some_expression;

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

有关背景资料,请参阅 http://cppreference.com , 具体来说 Implicit Conversion

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

关于c++ - 隐式数值类型转换规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46268700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com