The __typeof__() and typeof() operators

With the __typeof__() operator, the compiler lets you specify the data type of an expression. Listing 1 shows an example.

__typeof__(expression)

where expression is any valid C expression or data type. Because the compiler translates a __typeof__() expression into a data type, you can use this expression wherever a normal type would be specified.

Like the sizeof() operator, __typeof__() is only evaluated at compile time, not at runtime. For related information, see “Sizeof() Operator Data Type”.

If you enable the gcc_extensions pragma, the typeof() operator is equivalent to the __typeof__() operator.

Listing 1. Example of __typeof__() and typeof() Operators

char *cp;
int *ip;
long *lp;

__typeof__(*ip) i; /* equivalent to "int i;" */
__typeof__(*lp) l; /* equivalent to "long l;" */

#pragma gcc_extensions on
typeof(*cp) c; /* equivalent to "char c;" */