c - Cast的立即操作数的含义

标签 c mingw32

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

  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. 谁能解释一下语句中“强制转换的直接操作数”的含义 “整数常量表达式应具有整数类型,并且只能具有整数常量的操作数,......其结果是作为强制转换的直接操作数的浮点常量”

(@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.

这些有 signedunsigned 对应物。

“作为强制转换的直接操作数的 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/

相关文章:

C++ 代码未在 NetBeans 8.0 中编译(msys mkdir 可能存在问题)

c++ - 安装MinGW时如何设置环境变量PATH(Windows 32位)

c - strcpy() 段错误

c - 在 C 中打印字符串的所有排列

gnu - 如何添加到 LDFLAGS 的路径

使用 Mingw 编译

c - 在动态数组中存储值

c++ - 如何读取x86处理器中的中断标志

C - 大约 30 次读取后无法读取套接字

c++ - mingw32的Qt编译错误