代码之家  ›  专栏  ›  技术社区  ›  Rajesh

Cast的立即数的意义

  •  1
  • Rajesh  · 技术社区  · 6 年前

    this 那就贴吧。我想知道两件事

    整数常量表达式应为整数类型

    但不确定long和long是否也以同样的方式对待。我尝试了下面的例子,没有得到任何编译器警告或错误。所以我猜integer意味着enum,char,int,long和long。

    int main(void)
    {
        unsigned long long a=4294967296LL; // no need of LL
        switch (a)
        {
        case 4294967296:
            printf("Hello");
            break;
        }
    return(0);
    }
    
    1. 有人能解释一下语句中“类型转换的立即操作数”的含义吗

    (在同一个SO帖子中,@user963241有一条未回复的评论)。

    我使用MinGW 32位编译器。

    1 回复  |  直到 6 年前
        1
  •  1
  •   pmg    6 年前

    根据C标准草案(N1570)中关于类型的第6.2.5节:

    有五种标准整数类型,指定为 char, short int, int, long int, long long int

    signed unsigned 相对应的人。

    自己 (不是经过一些算术计算)是一个浮动常数。

    例如:

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