This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
gcc_extensions [2019/07/25 12:16] rpjday created |
gcc_extensions [2019/07/25 12:53] rpjday [Case ranges] |
||
---|---|---|---|
Line 6: | Line 6: | ||
==== Statements and declarations in expressions ==== | ==== Statements and declarations in expressions ==== | ||
+ | |||
+ | A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression. | ||
<code> | <code> | ||
Line 11: | Line 13: | ||
({int _a = (a), _b = (b); _a > _b ? _a : _b; }) | ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) | ||
</code> | </code> | ||
+ | |||
+ | ==== Local labels ==== | ||
+ | |||
+ | GCC allows you to declare local labels in any nested block scope. A local label is just like an ordinary label, but you can only reference it (with a goto statement, or by taking its address) within the block in which it is declared. | ||
+ | |||
+ | <code> | ||
+ | __label__ label; | ||
+ | </code> | ||
+ | |||
+ | ==== Labels as values ==== | ||
+ | |||
+ | You can get the address of a label defined in the current function (or a containing function) with the unary operator ‘&&’. The value has type void *. This value is a constant and can be used wherever a constant of that type is valid. | ||
+ | |||
+ | <code> | ||
+ | void *ptr; | ||
+ | /* … */ | ||
+ | ptr = &&foo; | ||
+ | ... | ||
+ | goto *ptr; | ||
+ | </code> | ||
+ | |||
+ | ==== Nested functions ==== | ||
+ | |||
+ | <code> | ||
+ | foo (double a, double b) | ||
+ | { | ||
+ | double square (double z) { return z * z; } | ||
+ | |||
+ | return square (a) + square (b); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | See "trampolines." | ||
+ | |||
+ | ==== Nonlocal gotos ==== | ||
+ | |||
+ | Use ''%%__builtin_setjmp%%'' and ''%%__builtin_longjmp%%''. | ||
+ | |||
+ | ==== Constructing function calls ==== | ||
+ | |||
+ | ==== typeof ==== | ||
+ | |||
+ | <code> | ||
+ | #define max(a,b) \ | ||
+ | ({ typeof (a) _a = (a); \ | ||
+ | typeof (b) _b = (b); \ | ||
+ | _a > _b ? _a : _b; }) | ||
+ | </code> | ||
+ | |||
+ | ==== Conditionals ==== | ||
+ | |||
+ | Equivalent: | ||
+ | |||
+ | <code> | ||
+ | x ? x : y | ||
+ | x ? : y | ||
+ | </code> | ||
+ | |||
+ | ==== Non-constant initializers ==== | ||
+ | |||
+ | ==== Designated initializers ==== | ||
+ | |||
+ | ==== Case ranges ==== | ||
+ | |||
+ | <code> | ||
+ | case low ... high: | ||
+ | </code> | ||
+ | |||
+ | Don't forget spaces. | ||
+ | |||
+ | |||
+ |