相关疑难解决方法(0)

为什么"sizeof(a?true:false)"给出四个字节的输出?

我有一小段关于sizeof三元运算符的运算符的代码:

#include <stdio.h>
#include <stdbool.h>

int main()
{
    bool a = true;
    printf("%zu\n", sizeof(bool));  // Ok
    printf("%zu\n", sizeof(a));     // Ok
    printf("%zu\n", sizeof(a ? true : false)); // Why 4?
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出(GCC):

1
1
4 // Why 4?
Run Code Online (Sandbox Code Playgroud)

但在这里,

printf("%zu\n", sizeof(a ? true : false)); // Why 4?
Run Code Online (Sandbox Code Playgroud)

三元运算符返回boolean类型,sizeof bool类型是1C中的字节.

为什么要sizeof(a ? true : false)输出四个字节?

c boolean sizeof conditional-operator c11

131
推荐指数
5
解决办法
1万
查看次数

标签 统计

boolean ×1

c ×1

c11 ×1

conditional-operator ×1

sizeof ×1