GCC /锵/ MSVC扩展设定C枚举的大小? [英] GCC/Clang/MSVC extension for specifying C enum size?

查看:370
本文介绍了GCC /锵/ MSVC扩展设定C枚举的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有任何扩展功能,在每个编译器指定C 枚举的大小?

Is there any extension feature to specify size of C enum on each compiler?


  • GCC


  • MSVC

推荐答案

使用GCC,你不能指定精确的长度,但你可以把它占用最短的长度可能与 -fshort-枚举。例如:

With GCC, you cannot specify the exact length, but you can have it take up the shortest length possible with -fshort-enums. Example:

#include <stdio.h>

typedef enum
{
    f1, f2
} foo;

int main()
{
    printf("%i\n", sizeof(foo));
    return 0;
}

编译:

gcc program.c -fshort-enums

输出:

1

不过,如果你想链接到任何东西,你必须确保,无论谁在你的头看起来也使用 -fshort-枚举或它不会ABI标准(你会看到一些真正有趣的错误)。

However, if you ever want to link to anything, you have to make sure that whoever looks at your headers also uses -fshort-enums or it will not be ABI compliant (and you will see some really funny errors).

这篇关于GCC /锵/ MSVC扩展设定C枚举的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆