【问题标题】:GCC typeof extensionGCC 类型扩展
【发布时间】:2013-01-17 12:06:11
【问题描述】:

我不明白为什么会这样:

/* gcc range extension */
__extension__ static int fn(int n)
{
    switch (n) {
        case 0: return 0;
        case 1 ... 1000: return 1;
        default: return -1;
    }
}

但这不是:

/* gcc typeof extension */
__extension__ static void fn(int n)
{
    typeof(n) a = n;

    printf("%d\n", a);
}

gcc 返回:

demo.c:14: warning: implicit declaration of function ‘typeof’
demo.c:14: warning: nested extern declaration of ‘typeof’
demo.c:14: error: expected ‘;’ before ‘a’
demo.c:16: error: ‘a’ undeclared (first use in this function)
demo.c:16: error: (Each undeclared identifier is reported only once
demo.c:16: error: for each function it appears in.)

我知道我可以使用 -std=gnu99 编译以避免错误,但第一个使用 -std=c99 并且还使用扩展名

【问题讨论】:

    标签: c gcc typeof gcc-extensions


    【解决方案1】:

    __extension__ 永远不会重新启用非 ANSI 兼容关键字(__extension__ 的唯一效果是对-pedantic 的警告抑制)。如果要在 ANSI 模式下编译,请使用 __typeof__

    【讨论】:

    • 致任何想了解更多信息的人:typeof
    【解决方案2】:

    如果您正在编写包含在 ISO C 程序中时必须工作的头文件,请编写 __typeof__ 而不是 typeof

    请参阅此link 以获得更详细的描述和可能的修复。

    注意:__extension__ 在使用 ANSI C -pedantic 模式时除了抑制警告之外没有任何作用。

    所以是这样的:

    /* gcc typeof extension */
    __extension__ static void fn(int n)
    {
        __typeof__(n) a = n;
    
        printf("%d\n", a);
    }
    

    【讨论】:

    • 感谢您的回答和链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多