Cast的立即操作数的含义

meaning of Immediate Operand of Cast

我在看 this 所以 post。想知道两件事

  1. 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);
}
  1. 谁能解释一下语句中'immediate operands of casts'的意思 "An integer constant expression shall have integer type and shall only have operands that are integer constants, ..... whose results are floating constants that are the immediate operands of casts"

(@user963241 在同一 SO post 中有一条未回复的评论)。

欣赏一个 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.

这些有 signedunsigned 个对应项。

"floating constants that are the immediate operands of casts"的意思是指cast的操作数本身(不是经过一些算术运算)是一个浮点数

例如:

(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;
}