c - 带括号的子表达式的类型提升规则是什么?
问题描述
假设一个整数表达式由多个无符号整数类型uint16_t
和uint32_t
. 该表达式包含一个带括号的子表达式,其中所有元素的类型都是uint16_t
。
uint32_t
括号内的子表达式中的元素是否应该在评估子表达式之前被提升?
例如:
#include <stdint.h>
#include <stdio.h>
int main(void)
{
uint16_t a16 = 0x2000;
uint16_t b16 = 0x3000;
uint32_t c32 = 0x00000001;
uint32_t d32;
// Should (a16 * b16) be evaluated to 0x06000000, or to 0x0000?
// Should d32 be evaluated to 0x06000001, or to 0x00000001?
d32 = c32 + (a16 * b16);
printf("d32=0x%08x\n", d32);
return 0;
}
在ideone在线编译器中尝试这个建议a16
并在乘法之前b16
提升。uint32_t
这是 C 标准规定的吗?为什么不在uint16_t
括号内求值?
解决方案
在乘法之前,所有比窄的类型int
都被提升为一个。int
因此,如果您有 32 位 2 的补码int
,那么结果实际上是两种int32_t
类型的乘积。
表达式中的括号对这种隐式类型转换或最终结果都没有影响。
推荐阅读
- asp.net-core - 使用整数用户 ID 扩展 EF Core Identity UserStore
- php - Easy admin bundle:如何访问 MenuItem::linktoRoute 第四个参数的参数数组
- python - 如何从数组中获取两个 int 数的值
- lua - Lua脚本如何调用/调用zenity或notify-send?
- certificate - 是否可以确定消息是否已由同一链中的证书签名?
- r - R Simmer - 根据队列大小在罐子之间重新分配资源
- json - 告诉 VS Code 从目录解析 JSON Schema 路径
- sql - 从 SQL 查询中排除组合
- c++ - 在 Oracle DB OCCI 界面中使用 UPDATE
- jfrog - jFrog Artifactory 开源不支持多LDAP活动目录认证