我在看 this所以发帖。想知道两件事
- C99 标准说
An integer constant expression shall have integer type
但不确定 long long 和 long 是否也以同样的方式处理。我尝试了以下示例,但没有收到任何编译器警告或错误。所以我猜 integer 表示 enum、char、int、long 和 long long。
int main(void)
{
unsigned long long a=4294967296LL; // no need of LL
switch (a)
{
case 4294967296:
printf("Hello");
break;
}
return(0);
}
- 谁能解释一下语句中“强制转换的直接操作数”的含义 “整数常量表达式应具有整数类型,并且只能具有整数常量的操作数,......其结果是作为强制转换的直接操作数的浮点常量”
(@user963241 在同一篇 SO 帖子中有一条未回复的评论)。
欣赏一个 switch case 示例,以证实作为强制转换的立即操作数的浮点常量的使用。
我使用 MinGW 32 位编译器。
最佳答案
根据 C 标准草案 (N1570) 中有关类型的第 6.2.5 节:
There are five standard integer types, designated as
char, short int, int, long int, long long int
.
这些有 signed
和 unsigned
对应物。
“作为强制转换的直接操作数的 float 常量”的含义是指强制转换的操作数本身(不是经过一些算术计算之后)是一个 float 常量。
例如:
(int)(3.14f) //1. Here the operand is an floating constant that is an immediate operand
(int)(22.0/7.0f) //2. Here the operand is NOT an floating constant that is an immediate operand.
您可以像这样在 switch case 语句中使用 1:
switch(op) {
case (int)(3.14f):
break;
}
关于c - Cast的立即操作数的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52715140/