作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有几个关于带符号和无符号变量的类型转换的问题。在这种情况下将选择哪种类型有符号整数 + 无符号整数 = ???
那么当我有常量而不是变量的情况下呢
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.prom和 conv.double在标准中。
关于c++ - 隐式数值类型转换规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46268700/
我是一名优秀的程序员,十分优秀!