GCC typeof extension
Asked Answered
B

2

5

I don't understand why this works:

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

But this does not:

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

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

gcc returns:

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.)

I know I can compile with -std=gnu99 to avoid the error but the first one works with -std=c99 and uses also an extension

Bick answered 17/1, 2013 at 12:6 Comment(0)
R
16

Non-ANSI compatible keywords are not ever reenabled by __extension__ (the only effect of __extension__ is warning suppression for -pedantic). Use __typeof__ if you want to compile in ANSI mode.

Resendez answered 17/1, 2013 at 12:11 Comment(1)
To anyone looking for more information: typeofPhototopography
A
4

If you are writing a header file that must work when included in ISO C programs, write __typeof__ instead of typeof.

Refer to this link for a more elaborate description and the possible fixes.

Note: __extension__ has no effect aside from suppressing the warnings when using ANSI C -pedantic mode.

So something like this:

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

    printf("%d\n", a);
}
Anatolio answered 17/1, 2013 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.