1 @c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1996, 1998, 1999, 2000, 2001,
2 @c 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 @c Free Software Foundation, Inc.
5 @c This is part of the GCC manual.
6 @c For copying conditions, see the file gcc.texi.
9 @chapter Extensions to the C Language Family
10 @cindex extensions, C language
11 @cindex C language extensions
14 GNU C provides several language features not found in ISO standard C@.
15 (The @option{-pedantic} option directs GCC to print a warning message if
16 any of these features is used.) To test for the availability of these
17 features in conditional compilation, check for a predefined macro
18 @code{__GNUC__}, which is always defined under GCC@.
20 These extensions are available in C and Objective-C@. Most of them are
21 also available in C++. @xref{C++ Extensions,,Extensions to the
22 C++ Language}, for extensions that apply @emph{only} to C++.
24 Some features that are in ISO C99 but not C89 or C++ are also, as
25 extensions, accepted by GCC in C89 mode and in C++.
28 * Statement Exprs:: Putting statements and declarations inside expressions.
29 * Local Labels:: Labels local to a block.
30 * Labels as Values:: Getting pointers to labels, and computed gotos.
31 * Nested Functions:: As in Algol and Pascal, lexical scoping of functions.
32 * Constructing Calls:: Dispatching a call to another function.
33 * Typeof:: @code{typeof}: referring to the type of an expression.
34 * Conditionals:: Omitting the middle operand of a @samp{?:} expression.
35 * Long Long:: Double-word integers---@code{long long int}.
36 * Complex:: Data types for complex numbers.
37 * Floating Types:: Additional Floating Types.
38 * Half-Precision:: Half-Precision Floating Point.
39 * Decimal Float:: Decimal Floating Types.
40 * Hex Floats:: Hexadecimal floating-point constants.
41 * Fixed-Point:: Fixed-Point Types.
42 * Zero Length:: Zero-length arrays.
43 * Variable Length:: Arrays whose length is computed at run time.
44 * Empty Structures:: Structures with no members.
45 * Variadic Macros:: Macros with a variable number of arguments.
46 * Escaped Newlines:: Slightly looser rules for escaped newlines.
47 * Subscripting:: Any array can be subscripted, even if not an lvalue.
48 * Pointer Arith:: Arithmetic on @code{void}-pointers and function pointers.
49 * Initializers:: Non-constant initializers.
50 * Compound Literals:: Compound literals give structures, unions
52 * Designated Inits:: Labeling elements of initializers.
53 * Cast to Union:: Casting to union type from any member of the union.
54 * Case Ranges:: `case 1 ... 9' and such.
55 * Mixed Declarations:: Mixing declarations and code.
56 * Function Attributes:: Declaring that functions have no side effects,
57 or that they can never return.
58 * Attribute Syntax:: Formal syntax for attributes.
59 * Function Prototypes:: Prototype declarations and old-style definitions.
60 * C++ Comments:: C++ comments are recognized.
61 * Dollar Signs:: Dollar sign is allowed in identifiers.
62 * Character Escapes:: @samp{\e} stands for the character @key{ESC}.
63 * Variable Attributes:: Specifying attributes of variables.
64 * Type Attributes:: Specifying attributes of types.
65 * Alignment:: Inquiring about the alignment of a type or variable.
66 * Inline:: Defining inline functions (as fast as macros).
67 * Extended Asm:: Assembler instructions with C expressions as operands.
68 (With them you can define ``built-in'' functions.)
69 * Constraints:: Constraints for asm operands
70 * Asm Labels:: Specifying the assembler name to use for a C symbol.
71 * Explicit Reg Vars:: Defining variables residing in specified registers.
72 * Alternate Keywords:: @code{__const__}, @code{__asm__}, etc., for header files.
73 * Incomplete Enums:: @code{enum foo;}, with details to follow.
74 * Function Names:: Printable strings which are the name of the current
76 * Return Address:: Getting the return or frame address of a function.
77 * Vector Extensions:: Using vector instructions through built-in functions.
78 * Offsetof:: Special syntax for implementing @code{offsetof}.
79 * Atomic Builtins:: Built-in functions for atomic memory access.
80 * Object Size Checking:: Built-in functions for limited buffer overflow
82 * Other Builtins:: Other built-in functions.
83 * Target Builtins:: Built-in functions specific to particular targets.
84 * Target Format Checks:: Format checks specific to particular targets.
85 * Pragmas:: Pragmas accepted by GCC.
86 * Unnamed Fields:: Unnamed struct/union fields within structs/unions.
87 * Thread-Local:: Per-thread variables.
88 * Binary constants:: Binary constants using the @samp{0b} prefix.
92 @section Statements and Declarations in Expressions
93 @cindex statements inside expressions
94 @cindex declarations inside expressions
95 @cindex expressions containing statements
96 @cindex macros, statements in expressions
98 @c the above section title wrapped and causes an underfull hbox.. i
99 @c changed it from "within" to "in". --mew 4feb93
100 A compound statement enclosed in parentheses may appear as an expression
101 in GNU C@. This allows you to use loops, switches, and local variables
102 within an expression.
104 Recall that a compound statement is a sequence of statements surrounded
105 by braces; in this construct, parentheses go around the braces. For
109 (@{ int y = foo (); int z;
116 is a valid (though slightly more complex than necessary) expression
117 for the absolute value of @code{foo ()}.
119 The last thing in the compound statement should be an expression
120 followed by a semicolon; the value of this subexpression serves as the
121 value of the entire construct. (If you use some other kind of statement
122 last within the braces, the construct has type @code{void}, and thus
123 effectively no value.)
125 This feature is especially useful in making macro definitions ``safe'' (so
126 that they evaluate each operand exactly once). For example, the
127 ``maximum'' function is commonly defined as a macro in standard C as
131 #define max(a,b) ((a) > (b) ? (a) : (b))
135 @cindex side effects, macro argument
136 But this definition computes either @var{a} or @var{b} twice, with bad
137 results if the operand has side effects. In GNU C, if you know the
138 type of the operands (here taken as @code{int}), you can define
139 the macro safely as follows:
142 #define maxint(a,b) \
143 (@{int _a = (a), _b = (b); _a > _b ? _a : _b; @})
146 Embedded statements are not allowed in constant expressions, such as
147 the value of an enumeration constant, the width of a bit-field, or
148 the initial value of a static variable.
150 If you don't know the type of the operand, you can still do this, but you
151 must use @code{typeof} (@pxref{Typeof}).
153 In G++, the result value of a statement expression undergoes array and
154 function pointer decay, and is returned by value to the enclosing
155 expression. For instance, if @code{A} is a class, then
164 will construct a temporary @code{A} object to hold the result of the
165 statement expression, and that will be used to invoke @code{Foo}.
166 Therefore the @code{this} pointer observed by @code{Foo} will not be the
169 Any temporaries created within a statement within a statement expression
170 will be destroyed at the statement's end. This makes statement
171 expressions inside macros slightly different from function calls. In
172 the latter case temporaries introduced during argument evaluation will
173 be destroyed at the end of the statement that includes the function
174 call. In the statement expression case they will be destroyed during
175 the statement expression. For instance,
178 #define macro(a) (@{__typeof__(a) b = (a); b + 3; @})
179 template<typename T> T function(T a) @{ T b = a; return b + 3; @}
189 will have different places where temporaries are destroyed. For the
190 @code{macro} case, the temporary @code{X} will be destroyed just after
191 the initialization of @code{b}. In the @code{function} case that
192 temporary will be destroyed when the function returns.
194 These considerations mean that it is probably a bad idea to use
195 statement-expressions of this form in header files that are designed to
196 work with C++. (Note that some versions of the GNU C Library contained
197 header files using statement-expression that lead to precisely this
200 Jumping into a statement expression with @code{goto} or using a
201 @code{switch} statement outside the statement expression with a
202 @code{case} or @code{default} label inside the statement expression is
203 not permitted. Jumping into a statement expression with a computed
204 @code{goto} (@pxref{Labels as Values}) yields undefined behavior.
205 Jumping out of a statement expression is permitted, but if the
206 statement expression is part of a larger expression then it is
207 unspecified which other subexpressions of that expression have been
208 evaluated except where the language definition requires certain
209 subexpressions to be evaluated before or after the statement
210 expression. In any case, as with a function call the evaluation of a
211 statement expression is not interleaved with the evaluation of other
212 parts of the containing expression. For example,
215 foo (), ((@{ bar1 (); goto a; 0; @}) + bar2 ()), baz();
219 will call @code{foo} and @code{bar1} and will not call @code{baz} but
220 may or may not call @code{bar2}. If @code{bar2} is called, it will be
221 called after @code{foo} and before @code{bar1}
224 @section Locally Declared Labels
226 @cindex macros, local labels
228 GCC allows you to declare @dfn{local labels} in any nested block
229 scope. A local label is just like an ordinary label, but you can
230 only reference it (with a @code{goto} statement, or by taking its
231 address) within the block in which it was declared.
233 A local label declaration looks like this:
236 __label__ @var{label};
243 __label__ @var{label1}, @var{label2}, /* @r{@dots{}} */;
246 Local label declarations must come at the beginning of the block,
247 before any ordinary declarations or statements.
249 The label declaration defines the label @emph{name}, but does not define
250 the label itself. You must do this in the usual way, with
251 @code{@var{label}:}, within the statements of the statement expression.
253 The local label feature is useful for complex macros. If a macro
254 contains nested loops, a @code{goto} can be useful for breaking out of
255 them. However, an ordinary label whose scope is the whole function
256 cannot be used: if the macro can be expanded several times in one
257 function, the label will be multiply defined in that function. A
258 local label avoids this problem. For example:
261 #define SEARCH(value, array, target) \
264 typeof (target) _SEARCH_target = (target); \
265 typeof (*(array)) *_SEARCH_array = (array); \
268 for (i = 0; i < max; i++) \
269 for (j = 0; j < max; j++) \
270 if (_SEARCH_array[i][j] == _SEARCH_target) \
271 @{ (value) = i; goto found; @} \
277 This could also be written using a statement-expression:
280 #define SEARCH(array, target) \
283 typeof (target) _SEARCH_target = (target); \
284 typeof (*(array)) *_SEARCH_array = (array); \
287 for (i = 0; i < max; i++) \
288 for (j = 0; j < max; j++) \
289 if (_SEARCH_array[i][j] == _SEARCH_target) \
290 @{ value = i; goto found; @} \
297 Local label declarations also make the labels they declare visible to
298 nested functions, if there are any. @xref{Nested Functions}, for details.
300 @node Labels as Values
301 @section Labels as Values
302 @cindex labels as values
303 @cindex computed gotos
304 @cindex goto with computed label
305 @cindex address of a label
307 You can get the address of a label defined in the current function
308 (or a containing function) with the unary operator @samp{&&}. The
309 value has type @code{void *}. This value is a constant and can be used
310 wherever a constant of that type is valid. For example:
318 To use these values, you need to be able to jump to one. This is done
319 with the computed goto statement@footnote{The analogous feature in
320 Fortran is called an assigned goto, but that name seems inappropriate in
321 C, where one can do more than simply store label addresses in label
322 variables.}, @code{goto *@var{exp};}. For example,
329 Any expression of type @code{void *} is allowed.
331 One way of using these constants is in initializing a static array that
332 will serve as a jump table:
335 static void *array[] = @{ &&foo, &&bar, &&hack @};
338 Then you can select a label with indexing, like this:
345 Note that this does not check whether the subscript is in bounds---array
346 indexing in C never does that.
348 Such an array of label values serves a purpose much like that of the
349 @code{switch} statement. The @code{switch} statement is cleaner, so
350 use that rather than an array unless the problem does not fit a
351 @code{switch} statement very well.
353 Another use of label values is in an interpreter for threaded code.
354 The labels within the interpreter function can be stored in the
355 threaded code for super-fast dispatching.
357 You may not use this mechanism to jump to code in a different function.
358 If you do that, totally unpredictable things will happen. The best way to
359 avoid this is to store the label address only in automatic variables and
360 never pass it as an argument.
362 An alternate way to write the above example is
365 static const int array[] = @{ &&foo - &&foo, &&bar - &&foo,
367 goto *(&&foo + array[i]);
371 This is more friendly to code living in shared libraries, as it reduces
372 the number of dynamic relocations that are needed, and by consequence,
373 allows the data to be read-only.
375 The @code{&&foo} expressions for the same label might have different values
376 if the containing function is inlined or cloned. If a program relies on
377 them being always the same, @code{__attribute__((__noinline__))} should
378 be used to prevent inlining. If @code{&&foo} is used
379 in a static variable initializer, inlining is forbidden.
381 @node Nested Functions
382 @section Nested Functions
383 @cindex nested functions
384 @cindex downward funargs
387 A @dfn{nested function} is a function defined inside another function.
388 (Nested functions are not supported for GNU C++.) The nested function's
389 name is local to the block where it is defined. For example, here we
390 define a nested function named @code{square}, and call it twice:
394 foo (double a, double b)
396 double square (double z) @{ return z * z; @}
398 return square (a) + square (b);
403 The nested function can access all the variables of the containing
404 function that are visible at the point of its definition. This is
405 called @dfn{lexical scoping}. For example, here we show a nested
406 function which uses an inherited variable named @code{offset}:
410 bar (int *array, int offset, int size)
412 int access (int *array, int index)
413 @{ return array[index + offset]; @}
416 for (i = 0; i < size; i++)
417 /* @r{@dots{}} */ access (array, i) /* @r{@dots{}} */
422 Nested function definitions are permitted within functions in the places
423 where variable definitions are allowed; that is, in any block, mixed
424 with the other declarations and statements in the block.
426 It is possible to call the nested function from outside the scope of its
427 name by storing its address or passing the address to another function:
430 hack (int *array, int size)
432 void store (int index, int value)
433 @{ array[index] = value; @}
435 intermediate (store, size);
439 Here, the function @code{intermediate} receives the address of
440 @code{store} as an argument. If @code{intermediate} calls @code{store},
441 the arguments given to @code{store} are used to store into @code{array}.
442 But this technique works only so long as the containing function
443 (@code{hack}, in this example) does not exit.
445 If you try to call the nested function through its address after the
446 containing function has exited, all hell will break loose. If you try
447 to call it after a containing scope level has exited, and if it refers
448 to some of the variables that are no longer in scope, you may be lucky,
449 but it's not wise to take the risk. If, however, the nested function
450 does not refer to anything that has gone out of scope, you should be
453 GCC implements taking the address of a nested function using a technique
454 called @dfn{trampolines}. A paper describing them is available as
457 @uref{http://people.debian.org/~aaronl/Usenix88-lexic.pdf}.
459 A nested function can jump to a label inherited from a containing
460 function, provided the label was explicitly declared in the containing
461 function (@pxref{Local Labels}). Such a jump returns instantly to the
462 containing function, exiting the nested function which did the
463 @code{goto} and any intermediate functions as well. Here is an example:
467 bar (int *array, int offset, int size)
470 int access (int *array, int index)
474 return array[index + offset];
478 for (i = 0; i < size; i++)
479 /* @r{@dots{}} */ access (array, i) /* @r{@dots{}} */
483 /* @r{Control comes here from @code{access}
484 if it detects an error.} */
491 A nested function always has no linkage. Declaring one with
492 @code{extern} or @code{static} is erroneous. If you need to declare the nested function
493 before its definition, use @code{auto} (which is otherwise meaningless
494 for function declarations).
497 bar (int *array, int offset, int size)
500 auto int access (int *, int);
502 int access (int *array, int index)
506 return array[index + offset];
512 @node Constructing Calls
513 @section Constructing Function Calls
514 @cindex constructing calls
515 @cindex forwarding calls
517 Using the built-in functions described below, you can record
518 the arguments a function received, and call another function
519 with the same arguments, without knowing the number or types
522 You can also record the return value of that function call,
523 and later return that value, without knowing what data type
524 the function tried to return (as long as your caller expects
527 However, these built-in functions may interact badly with some
528 sophisticated features or other extensions of the language. It
529 is, therefore, not recommended to use them outside very simple
530 functions acting as mere forwarders for their arguments.
532 @deftypefn {Built-in Function} {void *} __builtin_apply_args ()
533 This built-in function returns a pointer to data
534 describing how to perform a call with the same arguments as were passed
535 to the current function.
537 The function saves the arg pointer register, structure value address,
538 and all registers that might be used to pass arguments to a function
539 into a block of memory allocated on the stack. Then it returns the
540 address of that block.
543 @deftypefn {Built-in Function} {void *} __builtin_apply (void (*@var{function})(), void *@var{arguments}, size_t @var{size})
544 This built-in function invokes @var{function}
545 with a copy of the parameters described by @var{arguments}
548 The value of @var{arguments} should be the value returned by
549 @code{__builtin_apply_args}. The argument @var{size} specifies the size
550 of the stack argument data, in bytes.
552 This function returns a pointer to data describing
553 how to return whatever value was returned by @var{function}. The data
554 is saved in a block of memory allocated on the stack.
556 It is not always simple to compute the proper value for @var{size}. The
557 value is used by @code{__builtin_apply} to compute the amount of data
558 that should be pushed on the stack and copied from the incoming argument
562 @deftypefn {Built-in Function} {void} __builtin_return (void *@var{result})
563 This built-in function returns the value described by @var{result} from
564 the containing function. You should specify, for @var{result}, a value
565 returned by @code{__builtin_apply}.
568 @deftypefn {Built-in Function} __builtin_va_arg_pack ()
569 This built-in function represents all anonymous arguments of an inline
570 function. It can be used only in inline functions which will be always
571 inlined, never compiled as a separate function, such as those using
572 @code{__attribute__ ((__always_inline__))} or
573 @code{__attribute__ ((__gnu_inline__))} extern inline functions.
574 It must be only passed as last argument to some other function
575 with variable arguments. This is useful for writing small wrapper
576 inlines for variable argument functions, when using preprocessor
577 macros is undesirable. For example:
579 extern int myprintf (FILE *f, const char *format, ...);
580 extern inline __attribute__ ((__gnu_inline__)) int
581 myprintf (FILE *f, const char *format, ...)
583 int r = fprintf (f, "myprintf: ");
586 int s = fprintf (f, format, __builtin_va_arg_pack ());
594 @deftypefn {Built-in Function} __builtin_va_arg_pack_len ()
595 This built-in function returns the number of anonymous arguments of
596 an inline function. It can be used only in inline functions which
597 will be always inlined, never compiled as a separate function, such
598 as those using @code{__attribute__ ((__always_inline__))} or
599 @code{__attribute__ ((__gnu_inline__))} extern inline functions.
600 For example following will do link or runtime checking of open
601 arguments for optimized code:
604 extern inline __attribute__((__gnu_inline__)) int
605 myopen (const char *path, int oflag, ...)
607 if (__builtin_va_arg_pack_len () > 1)
608 warn_open_too_many_arguments ();
610 if (__builtin_constant_p (oflag))
612 if ((oflag & O_CREAT) != 0 && __builtin_va_arg_pack_len () < 1)
614 warn_open_missing_mode ();
615 return __open_2 (path, oflag);
617 return open (path, oflag, __builtin_va_arg_pack ());
620 if (__builtin_va_arg_pack_len () < 1)
621 return __open_2 (path, oflag);
623 return open (path, oflag, __builtin_va_arg_pack ());
630 @section Referring to a Type with @code{typeof}
633 @cindex macros, types of arguments
635 Another way to refer to the type of an expression is with @code{typeof}.
636 The syntax of using of this keyword looks like @code{sizeof}, but the
637 construct acts semantically like a type name defined with @code{typedef}.
639 There are two ways of writing the argument to @code{typeof}: with an
640 expression or with a type. Here is an example with an expression:
647 This assumes that @code{x} is an array of pointers to functions;
648 the type described is that of the values of the functions.
650 Here is an example with a typename as the argument:
657 Here the type described is that of pointers to @code{int}.
659 If you are writing a header file that must work when included in ISO C
660 programs, write @code{__typeof__} instead of @code{typeof}.
661 @xref{Alternate Keywords}.
663 A @code{typeof}-construct can be used anywhere a typedef name could be
664 used. For example, you can use it in a declaration, in a cast, or inside
665 of @code{sizeof} or @code{typeof}.
667 The operand of @code{typeof} is evaluated for its side effects if and
668 only if it is an expression of variably modified type or the name of
671 @code{typeof} is often useful in conjunction with the
672 statements-within-expressions feature. Here is how the two together can
673 be used to define a safe ``maximum'' macro that operates on any
674 arithmetic type and evaluates each of its arguments exactly once:
678 (@{ typeof (a) _a = (a); \
679 typeof (b) _b = (b); \
680 _a > _b ? _a : _b; @})
683 @cindex underscores in variables in macros
684 @cindex @samp{_} in variables in macros
685 @cindex local variables in macros
686 @cindex variables, local, in macros
687 @cindex macros, local variables in
689 The reason for using names that start with underscores for the local
690 variables is to avoid conflicts with variable names that occur within the
691 expressions that are substituted for @code{a} and @code{b}. Eventually we
692 hope to design a new form of declaration syntax that allows you to declare
693 variables whose scopes start only after their initializers; this will be a
694 more reliable way to prevent such conflicts.
697 Some more examples of the use of @code{typeof}:
701 This declares @code{y} with the type of what @code{x} points to.
708 This declares @code{y} as an array of such values.
715 This declares @code{y} as an array of pointers to characters:
718 typeof (typeof (char *)[4]) y;
722 It is equivalent to the following traditional C declaration:
728 To see the meaning of the declaration using @code{typeof}, and why it
729 might be a useful way to write, rewrite it with these macros:
732 #define pointer(T) typeof(T *)
733 #define array(T, N) typeof(T [N])
737 Now the declaration can be rewritten this way:
740 array (pointer (char), 4) y;
744 Thus, @code{array (pointer (char), 4)} is the type of arrays of 4
745 pointers to @code{char}.
748 @emph{Compatibility Note:} In addition to @code{typeof}, GCC 2 supported
749 a more limited extension which permitted one to write
752 typedef @var{T} = @var{expr};
756 with the effect of declaring @var{T} to have the type of the expression
757 @var{expr}. This extension does not work with GCC 3 (versions between
758 3.0 and 3.2 will crash; 3.2.1 and later give an error). Code which
759 relies on it should be rewritten to use @code{typeof}:
762 typedef typeof(@var{expr}) @var{T};
766 This will work with all versions of GCC@.
769 @section Conditionals with Omitted Operands
770 @cindex conditional expressions, extensions
771 @cindex omitted middle-operands
772 @cindex middle-operands, omitted
773 @cindex extensions, @code{?:}
774 @cindex @code{?:} extensions
776 The middle operand in a conditional expression may be omitted. Then
777 if the first operand is nonzero, its value is the value of the conditional
780 Therefore, the expression
787 has the value of @code{x} if that is nonzero; otherwise, the value of
790 This example is perfectly equivalent to
796 @cindex side effect in ?:
797 @cindex ?: side effect
799 In this simple case, the ability to omit the middle operand is not
800 especially useful. When it becomes useful is when the first operand does,
801 or may (if it is a macro argument), contain a side effect. Then repeating
802 the operand in the middle would perform the side effect twice. Omitting
803 the middle operand uses the value already computed without the undesirable
804 effects of recomputing it.
807 @section Double-Word Integers
808 @cindex @code{long long} data types
809 @cindex double-word arithmetic
810 @cindex multiprecision arithmetic
811 @cindex @code{LL} integer suffix
812 @cindex @code{ULL} integer suffix
814 ISO C99 supports data types for integers that are at least 64 bits wide,
815 and as an extension GCC supports them in C89 mode and in C++.
816 Simply write @code{long long int} for a signed integer, or
817 @code{unsigned long long int} for an unsigned integer. To make an
818 integer constant of type @code{long long int}, add the suffix @samp{LL}
819 to the integer. To make an integer constant of type @code{unsigned long
820 long int}, add the suffix @samp{ULL} to the integer.
822 You can use these types in arithmetic like any other integer types.
823 Addition, subtraction, and bitwise boolean operations on these types
824 are open-coded on all types of machines. Multiplication is open-coded
825 if the machine supports fullword-to-doubleword a widening multiply
826 instruction. Division and shifts are open-coded only on machines that
827 provide special support. The operations that are not open-coded use
828 special library routines that come with GCC@.
830 There may be pitfalls when you use @code{long long} types for function
831 arguments, unless you declare function prototypes. If a function
832 expects type @code{int} for its argument, and you pass a value of type
833 @code{long long int}, confusion will result because the caller and the
834 subroutine will disagree about the number of bytes for the argument.
835 Likewise, if the function expects @code{long long int} and you pass
836 @code{int}. The best way to avoid such problems is to use prototypes.
839 @section Complex Numbers
840 @cindex complex numbers
841 @cindex @code{_Complex} keyword
842 @cindex @code{__complex__} keyword
844 ISO C99 supports complex floating data types, and as an extension GCC
845 supports them in C89 mode and in C++, and supports complex integer data
846 types which are not part of ISO C99. You can declare complex types
847 using the keyword @code{_Complex}. As an extension, the older GNU
848 keyword @code{__complex__} is also supported.
850 For example, @samp{_Complex double x;} declares @code{x} as a
851 variable whose real part and imaginary part are both of type
852 @code{double}. @samp{_Complex short int y;} declares @code{y} to
853 have real and imaginary parts of type @code{short int}; this is not
854 likely to be useful, but it shows that the set of complex types is
857 To write a constant with a complex data type, use the suffix @samp{i} or
858 @samp{j} (either one; they are equivalent). For example, @code{2.5fi}
859 has type @code{_Complex float} and @code{3i} has type
860 @code{_Complex int}. Such a constant always has a pure imaginary
861 value, but you can form any complex value you like by adding one to a
862 real constant. This is a GNU extension; if you have an ISO C99
863 conforming C library (such as GNU libc), and want to construct complex
864 constants of floating type, you should include @code{<complex.h>} and
865 use the macros @code{I} or @code{_Complex_I} instead.
867 @cindex @code{__real__} keyword
868 @cindex @code{__imag__} keyword
869 To extract the real part of a complex-valued expression @var{exp}, write
870 @code{__real__ @var{exp}}. Likewise, use @code{__imag__} to
871 extract the imaginary part. This is a GNU extension; for values of
872 floating type, you should use the ISO C99 functions @code{crealf},
873 @code{creal}, @code{creall}, @code{cimagf}, @code{cimag} and
874 @code{cimagl}, declared in @code{<complex.h>} and also provided as
875 built-in functions by GCC@.
877 @cindex complex conjugation
878 The operator @samp{~} performs complex conjugation when used on a value
879 with a complex type. This is a GNU extension; for values of
880 floating type, you should use the ISO C99 functions @code{conjf},
881 @code{conj} and @code{conjl}, declared in @code{<complex.h>} and also
882 provided as built-in functions by GCC@.
884 GCC can allocate complex automatic variables in a noncontiguous
885 fashion; it's even possible for the real part to be in a register while
886 the imaginary part is on the stack (or vice-versa). Only the DWARF2
887 debug info format can represent this, so use of DWARF2 is recommended.
888 If you are using the stabs debug info format, GCC describes a noncontiguous
889 complex variable as if it were two separate variables of noncomplex type.
890 If the variable's actual name is @code{foo}, the two fictitious
891 variables are named @code{foo$real} and @code{foo$imag}. You can
892 examine and set these two fictitious variables with your debugger.
895 @section Additional Floating Types
896 @cindex additional floating types
897 @cindex @code{__float80} data type
898 @cindex @code{__float128} data type
899 @cindex @code{w} floating point suffix
900 @cindex @code{q} floating point suffix
901 @cindex @code{W} floating point suffix
902 @cindex @code{Q} floating point suffix
904 As an extension, the GNU C compiler supports additional floating
905 types, @code{__float80} and @code{__float128} to support 80bit
906 (@code{XFmode}) and 128 bit (@code{TFmode}) floating types.
907 Support for additional types includes the arithmetic operators:
908 add, subtract, multiply, divide; unary arithmetic operators;
909 relational operators; equality operators; and conversions to and from
910 integer and other floating types. Use a suffix @samp{w} or @samp{W}
911 in a literal constant of type @code{__float80} and @samp{q} or @samp{Q}
912 for @code{_float128}. You can declare complex types using the
913 corresponding internal complex type, @code{XCmode} for @code{__float80}
914 type and @code{TCmode} for @code{__float128} type:
917 typedef _Complex float __attribute__((mode(TC))) _Complex128;
918 typedef _Complex float __attribute__((mode(XC))) _Complex80;
921 Not all targets support additional floating point types. @code{__float80}
922 and @code{__float128} types are supported on i386, x86_64 and ia64 targets.
925 @section Half-Precision Floating Point
926 @cindex half-precision floating point
927 @cindex @code{__fp16} data type
929 On ARM targets, GCC supports half-precision (16-bit) floating point via
930 the @code{__fp16} type. You must enable this type explicitly
931 with the @option{-mfp16-format} command-line option in order to use it.
933 ARM supports two incompatible representations for half-precision
934 floating-point values. You must choose one of the representations and
935 use it consistently in your program.
937 Specifying @option{-mfp16-format=ieee} selects the IEEE 754-2008 format.
938 This format can represent normalized values in the range of @math{2^{-14}} to 65504.
939 There are 11 bits of significand precision, approximately 3
942 Specifying @option{-mfp16-format=alternative} selects the ARM
943 alternative format. This representation is similar to the IEEE
944 format, but does not support infinities or NaNs. Instead, the range
945 of exponents is extended, so that this format can represent normalized
946 values in the range of @math{2^{-14}} to 131008.
948 The @code{__fp16} type is a storage format only. For purposes
949 of arithmetic and other operations, @code{__fp16} values in C or C++
950 expressions are automatically promoted to @code{float}. In addition,
951 you cannot declare a function with a return value or parameters
952 of type @code{__fp16}.
954 Note that conversions from @code{double} to @code{__fp16}
955 involve an intermediate conversion to @code{float}. Because
956 of rounding, this can sometimes produce a different result than a
959 ARM provides hardware support for conversions between
960 @code{__fp16} and @code{float} values
961 as an extension to VFP and NEON (Advanced SIMD). GCC generates
962 code using the instructions provided by this extension if you compile
963 with the options @option{-mfpu=neon-fp16 -mfloat-abi=softfp},
964 in addition to the @option{-mfp16-format} option to select
965 a half-precision format.
967 Language-level support for the @code{__fp16} data type is
968 independent of whether GCC generates code using hardware floating-point
969 instructions. In cases where hardware support is not specified, GCC
970 implements conversions between @code{__fp16} and @code{float} values
974 @section Decimal Floating Types
975 @cindex decimal floating types
976 @cindex @code{_Decimal32} data type
977 @cindex @code{_Decimal64} data type
978 @cindex @code{_Decimal128} data type
979 @cindex @code{df} integer suffix
980 @cindex @code{dd} integer suffix
981 @cindex @code{dl} integer suffix
982 @cindex @code{DF} integer suffix
983 @cindex @code{DD} integer suffix
984 @cindex @code{DL} integer suffix
986 As an extension, the GNU C compiler supports decimal floating types as
987 defined in the N1312 draft of ISO/IEC WDTR24732. Support for decimal
988 floating types in GCC will evolve as the draft technical report changes.
989 Calling conventions for any target might also change. Not all targets
990 support decimal floating types.
992 The decimal floating types are @code{_Decimal32}, @code{_Decimal64}, and
993 @code{_Decimal128}. They use a radix of ten, unlike the floating types
994 @code{float}, @code{double}, and @code{long double} whose radix is not
995 specified by the C standard but is usually two.
997 Support for decimal floating types includes the arithmetic operators
998 add, subtract, multiply, divide; unary arithmetic operators;
999 relational operators; equality operators; and conversions to and from
1000 integer and other floating types. Use a suffix @samp{df} or
1001 @samp{DF} in a literal constant of type @code{_Decimal32}, @samp{dd}
1002 or @samp{DD} for @code{_Decimal64}, and @samp{dl} or @samp{DL} for
1005 GCC support of decimal float as specified by the draft technical report
1010 When the value of a decimal floating type cannot be represented in the
1011 integer type to which it is being converted, the result is undefined
1012 rather than the result value specified by the draft technical report.
1015 GCC does not provide the C library functionality associated with
1016 @file{math.h}, @file{fenv.h}, @file{stdio.h}, @file{stdlib.h}, and
1017 @file{wchar.h}, which must come from a separate C library implementation.
1018 Because of this the GNU C compiler does not define macro
1019 @code{__STDC_DEC_FP__} to indicate that the implementation conforms to
1020 the technical report.
1023 Types @code{_Decimal32}, @code{_Decimal64}, and @code{_Decimal128}
1024 are supported by the DWARF2 debug information format.
1030 ISO C99 supports floating-point numbers written not only in the usual
1031 decimal notation, such as @code{1.55e1}, but also numbers such as
1032 @code{0x1.fp3} written in hexadecimal format. As a GNU extension, GCC
1033 supports this in C89 mode (except in some cases when strictly
1034 conforming) and in C++. In that format the
1035 @samp{0x} hex introducer and the @samp{p} or @samp{P} exponent field are
1036 mandatory. The exponent is a decimal number that indicates the power of
1037 2 by which the significant part will be multiplied. Thus @samp{0x1.f} is
1044 @samp{p3} multiplies it by 8, and the value of @code{0x1.fp3}
1045 is the same as @code{1.55e1}.
1047 Unlike for floating-point numbers in the decimal notation the exponent
1048 is always required in the hexadecimal notation. Otherwise the compiler
1049 would not be able to resolve the ambiguity of, e.g., @code{0x1.f}. This
1050 could mean @code{1.0f} or @code{1.9375} since @samp{f} is also the
1051 extension for floating-point constants of type @code{float}.
1054 @section Fixed-Point Types
1055 @cindex fixed-point types
1056 @cindex @code{_Fract} data type
1057 @cindex @code{_Accum} data type
1058 @cindex @code{_Sat} data type
1059 @cindex @code{hr} fixed-suffix
1060 @cindex @code{r} fixed-suffix
1061 @cindex @code{lr} fixed-suffix
1062 @cindex @code{llr} fixed-suffix
1063 @cindex @code{uhr} fixed-suffix
1064 @cindex @code{ur} fixed-suffix
1065 @cindex @code{ulr} fixed-suffix
1066 @cindex @code{ullr} fixed-suffix
1067 @cindex @code{hk} fixed-suffix
1068 @cindex @code{k} fixed-suffix
1069 @cindex @code{lk} fixed-suffix
1070 @cindex @code{llk} fixed-suffix
1071 @cindex @code{uhk} fixed-suffix
1072 @cindex @code{uk} fixed-suffix
1073 @cindex @code{ulk} fixed-suffix
1074 @cindex @code{ullk} fixed-suffix
1075 @cindex @code{HR} fixed-suffix
1076 @cindex @code{R} fixed-suffix
1077 @cindex @code{LR} fixed-suffix
1078 @cindex @code{LLR} fixed-suffix
1079 @cindex @code{UHR} fixed-suffix
1080 @cindex @code{UR} fixed-suffix
1081 @cindex @code{ULR} fixed-suffix
1082 @cindex @code{ULLR} fixed-suffix
1083 @cindex @code{HK} fixed-suffix
1084 @cindex @code{K} fixed-suffix
1085 @cindex @code{LK} fixed-suffix
1086 @cindex @code{LLK} fixed-suffix
1087 @cindex @code{UHK} fixed-suffix
1088 @cindex @code{UK} fixed-suffix
1089 @cindex @code{ULK} fixed-suffix
1090 @cindex @code{ULLK} fixed-suffix
1092 As an extension, the GNU C compiler supports fixed-point types as
1093 defined in the N1169 draft of ISO/IEC DTR 18037. Support for fixed-point
1094 types in GCC will evolve as the draft technical report changes.
1095 Calling conventions for any target might also change. Not all targets
1096 support fixed-point types.
1098 The fixed-point types are
1099 @code{short _Fract},
1102 @code{long long _Fract},
1103 @code{unsigned short _Fract},
1104 @code{unsigned _Fract},
1105 @code{unsigned long _Fract},
1106 @code{unsigned long long _Fract},
1107 @code{_Sat short _Fract},
1109 @code{_Sat long _Fract},
1110 @code{_Sat long long _Fract},
1111 @code{_Sat unsigned short _Fract},
1112 @code{_Sat unsigned _Fract},
1113 @code{_Sat unsigned long _Fract},
1114 @code{_Sat unsigned long long _Fract},
1115 @code{short _Accum},
1118 @code{long long _Accum},
1119 @code{unsigned short _Accum},
1120 @code{unsigned _Accum},
1121 @code{unsigned long _Accum},
1122 @code{unsigned long long _Accum},
1123 @code{_Sat short _Accum},
1125 @code{_Sat long _Accum},
1126 @code{_Sat long long _Accum},
1127 @code{_Sat unsigned short _Accum},
1128 @code{_Sat unsigned _Accum},
1129 @code{_Sat unsigned long _Accum},
1130 @code{_Sat unsigned long long _Accum}.
1132 Fixed-point data values contain fractional and optional integral parts.
1133 The format of fixed-point data varies and depends on the target machine.
1135 Support for fixed-point types includes:
1138 prefix and postfix increment and decrement operators (@code{++}, @code{--})
1140 unary arithmetic operators (@code{+}, @code{-}, @code{!})
1142 binary arithmetic operators (@code{+}, @code{-}, @code{*}, @code{/})
1144 binary shift operators (@code{<<}, @code{>>})
1146 relational operators (@code{<}, @code{<=}, @code{>=}, @code{>})
1148 equality operators (@code{==}, @code{!=})
1150 assignment operators (@code{+=}, @code{-=}, @code{*=}, @code{/=},
1151 @code{<<=}, @code{>>=})
1153 conversions to and from integer, floating-point, or fixed-point types
1156 Use a suffix in a fixed-point literal constant:
1158 @item @samp{hr} or @samp{HR} for @code{short _Fract} and
1159 @code{_Sat short _Fract}
1160 @item @samp{r} or @samp{R} for @code{_Fract} and @code{_Sat _Fract}
1161 @item @samp{lr} or @samp{LR} for @code{long _Fract} and
1162 @code{_Sat long _Fract}
1163 @item @samp{llr} or @samp{LLR} for @code{long long _Fract} and
1164 @code{_Sat long long _Fract}
1165 @item @samp{uhr} or @samp{UHR} for @code{unsigned short _Fract} and
1166 @code{_Sat unsigned short _Fract}
1167 @item @samp{ur} or @samp{UR} for @code{unsigned _Fract} and
1168 @code{_Sat unsigned _Fract}
1169 @item @samp{ulr} or @samp{ULR} for @code{unsigned long _Fract} and
1170 @code{_Sat unsigned long _Fract}
1171 @item @samp{ullr} or @samp{ULLR} for @code{unsigned long long _Fract}
1172 and @code{_Sat unsigned long long _Fract}
1173 @item @samp{hk} or @samp{HK} for @code{short _Accum} and
1174 @code{_Sat short _Accum}
1175 @item @samp{k} or @samp{K} for @code{_Accum} and @code{_Sat _Accum}
1176 @item @samp{lk} or @samp{LK} for @code{long _Accum} and
1177 @code{_Sat long _Accum}
1178 @item @samp{llk} or @samp{LLK} for @code{long long _Accum} and
1179 @code{_Sat long long _Accum}
1180 @item @samp{uhk} or @samp{UHK} for @code{unsigned short _Accum} and
1181 @code{_Sat unsigned short _Accum}
1182 @item @samp{uk} or @samp{UK} for @code{unsigned _Accum} and
1183 @code{_Sat unsigned _Accum}
1184 @item @samp{ulk} or @samp{ULK} for @code{unsigned long _Accum} and
1185 @code{_Sat unsigned long _Accum}
1186 @item @samp{ullk} or @samp{ULLK} for @code{unsigned long long _Accum}
1187 and @code{_Sat unsigned long long _Accum}
1190 GCC support of fixed-point types as specified by the draft technical report
1195 Pragmas to control overflow and rounding behaviors are not implemented.
1198 Fixed-point types are supported by the DWARF2 debug information format.
1201 @section Arrays of Length Zero
1202 @cindex arrays of length zero
1203 @cindex zero-length arrays
1204 @cindex length-zero arrays
1205 @cindex flexible array members
1207 Zero-length arrays are allowed in GNU C@. They are very useful as the
1208 last element of a structure which is really a header for a variable-length
1217 struct line *thisline = (struct line *)
1218 malloc (sizeof (struct line) + this_length);
1219 thisline->length = this_length;
1222 In ISO C90, you would have to give @code{contents} a length of 1, which
1223 means either you waste space or complicate the argument to @code{malloc}.
1225 In ISO C99, you would use a @dfn{flexible array member}, which is
1226 slightly different in syntax and semantics:
1230 Flexible array members are written as @code{contents[]} without
1234 Flexible array members have incomplete type, and so the @code{sizeof}
1235 operator may not be applied. As a quirk of the original implementation
1236 of zero-length arrays, @code{sizeof} evaluates to zero.
1239 Flexible array members may only appear as the last member of a
1240 @code{struct} that is otherwise non-empty.
1243 A structure containing a flexible array member, or a union containing
1244 such a structure (possibly recursively), may not be a member of a
1245 structure or an element of an array. (However, these uses are
1246 permitted by GCC as extensions.)
1249 GCC versions before 3.0 allowed zero-length arrays to be statically
1250 initialized, as if they were flexible arrays. In addition to those
1251 cases that were useful, it also allowed initializations in situations
1252 that would corrupt later data. Non-empty initialization of zero-length
1253 arrays is now treated like any case where there are more initializer
1254 elements than the array holds, in that a suitable warning about "excess
1255 elements in array" is given, and the excess elements (all of them, in
1256 this case) are ignored.
1258 Instead GCC allows static initialization of flexible array members.
1259 This is equivalent to defining a new structure containing the original
1260 structure followed by an array of sufficient size to contain the data.
1261 I.e.@: in the following, @code{f1} is constructed as if it were declared
1267 @} f1 = @{ 1, @{ 2, 3, 4 @} @};
1270 struct f1 f1; int data[3];
1271 @} f2 = @{ @{ 1 @}, @{ 2, 3, 4 @} @};
1275 The convenience of this extension is that @code{f1} has the desired
1276 type, eliminating the need to consistently refer to @code{f2.f1}.
1278 This has symmetry with normal static arrays, in that an array of
1279 unknown size is also written with @code{[]}.
1281 Of course, this extension only makes sense if the extra data comes at
1282 the end of a top-level object, as otherwise we would be overwriting
1283 data at subsequent offsets. To avoid undue complication and confusion
1284 with initialization of deeply nested arrays, we simply disallow any
1285 non-empty initialization except when the structure is the top-level
1286 object. For example:
1289 struct foo @{ int x; int y[]; @};
1290 struct bar @{ struct foo z; @};
1292 struct foo a = @{ 1, @{ 2, 3, 4 @} @}; // @r{Valid.}
1293 struct bar b = @{ @{ 1, @{ 2, 3, 4 @} @} @}; // @r{Invalid.}
1294 struct bar c = @{ @{ 1, @{ @} @} @}; // @r{Valid.}
1295 struct foo d[1] = @{ @{ 1 @{ 2, 3, 4 @} @} @}; // @r{Invalid.}
1298 @node Empty Structures
1299 @section Structures With No Members
1300 @cindex empty structures
1301 @cindex zero-size structures
1303 GCC permits a C structure to have no members:
1310 The structure will have size zero. In C++, empty structures are part
1311 of the language. G++ treats empty structures as if they had a single
1312 member of type @code{char}.
1314 @node Variable Length
1315 @section Arrays of Variable Length
1316 @cindex variable-length arrays
1317 @cindex arrays of variable length
1320 Variable-length automatic arrays are allowed in ISO C99, and as an
1321 extension GCC accepts them in C89 mode and in C++. (However, GCC's
1322 implementation of variable-length arrays does not yet conform in detail
1323 to the ISO C99 standard.) These arrays are
1324 declared like any other automatic arrays, but with a length that is not
1325 a constant expression. The storage is allocated at the point of
1326 declaration and deallocated when the brace-level is exited. For
1331 concat_fopen (char *s1, char *s2, char *mode)
1333 char str[strlen (s1) + strlen (s2) + 1];
1336 return fopen (str, mode);
1340 @cindex scope of a variable length array
1341 @cindex variable-length array scope
1342 @cindex deallocating variable length arrays
1343 Jumping or breaking out of the scope of the array name deallocates the
1344 storage. Jumping into the scope is not allowed; you get an error
1347 @cindex @code{alloca} vs variable-length arrays
1348 You can use the function @code{alloca} to get an effect much like
1349 variable-length arrays. The function @code{alloca} is available in
1350 many other C implementations (but not in all). On the other hand,
1351 variable-length arrays are more elegant.
1353 There are other differences between these two methods. Space allocated
1354 with @code{alloca} exists until the containing @emph{function} returns.
1355 The space for a variable-length array is deallocated as soon as the array
1356 name's scope ends. (If you use both variable-length arrays and
1357 @code{alloca} in the same function, deallocation of a variable-length array
1358 will also deallocate anything more recently allocated with @code{alloca}.)
1360 You can also use variable-length arrays as arguments to functions:
1364 tester (int len, char data[len][len])
1370 The length of an array is computed once when the storage is allocated
1371 and is remembered for the scope of the array in case you access it with
1374 If you want to pass the array first and the length afterward, you can
1375 use a forward declaration in the parameter list---another GNU extension.
1379 tester (int len; char data[len][len], int len)
1385 @cindex parameter forward declaration
1386 The @samp{int len} before the semicolon is a @dfn{parameter forward
1387 declaration}, and it serves the purpose of making the name @code{len}
1388 known when the declaration of @code{data} is parsed.
1390 You can write any number of such parameter forward declarations in the
1391 parameter list. They can be separated by commas or semicolons, but the
1392 last one must end with a semicolon, which is followed by the ``real''
1393 parameter declarations. Each forward declaration must match a ``real''
1394 declaration in parameter name and data type. ISO C99 does not support
1395 parameter forward declarations.
1397 @node Variadic Macros
1398 @section Macros with a Variable Number of Arguments.
1399 @cindex variable number of arguments
1400 @cindex macro with variable arguments
1401 @cindex rest argument (in macro)
1402 @cindex variadic macros
1404 In the ISO C standard of 1999, a macro can be declared to accept a
1405 variable number of arguments much as a function can. The syntax for
1406 defining the macro is similar to that of a function. Here is an
1410 #define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)
1413 Here @samp{@dots{}} is a @dfn{variable argument}. In the invocation of
1414 such a macro, it represents the zero or more tokens until the closing
1415 parenthesis that ends the invocation, including any commas. This set of
1416 tokens replaces the identifier @code{__VA_ARGS__} in the macro body
1417 wherever it appears. See the CPP manual for more information.
1419 GCC has long supported variadic macros, and used a different syntax that
1420 allowed you to give a name to the variable arguments just like any other
1421 argument. Here is an example:
1424 #define debug(format, args...) fprintf (stderr, format, args)
1427 This is in all ways equivalent to the ISO C example above, but arguably
1428 more readable and descriptive.
1430 GNU CPP has two further variadic macro extensions, and permits them to
1431 be used with either of the above forms of macro definition.
1433 In standard C, you are not allowed to leave the variable argument out
1434 entirely; but you are allowed to pass an empty argument. For example,
1435 this invocation is invalid in ISO C, because there is no comma after
1442 GNU CPP permits you to completely omit the variable arguments in this
1443 way. In the above examples, the compiler would complain, though since
1444 the expansion of the macro still has the extra comma after the format
1447 To help solve this problem, CPP behaves specially for variable arguments
1448 used with the token paste operator, @samp{##}. If instead you write
1451 #define debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
1454 and if the variable arguments are omitted or empty, the @samp{##}
1455 operator causes the preprocessor to remove the comma before it. If you
1456 do provide some variable arguments in your macro invocation, GNU CPP
1457 does not complain about the paste operation and instead places the
1458 variable arguments after the comma. Just like any other pasted macro
1459 argument, these arguments are not macro expanded.
1461 @node Escaped Newlines
1462 @section Slightly Looser Rules for Escaped Newlines
1463 @cindex escaped newlines
1464 @cindex newlines (escaped)
1466 Recently, the preprocessor has relaxed its treatment of escaped
1467 newlines. Previously, the newline had to immediately follow a
1468 backslash. The current implementation allows whitespace in the form
1469 of spaces, horizontal and vertical tabs, and form feeds between the
1470 backslash and the subsequent newline. The preprocessor issues a
1471 warning, but treats it as a valid escaped newline and combines the two
1472 lines to form a single logical line. This works within comments and
1473 tokens, as well as between tokens. Comments are @emph{not} treated as
1474 whitespace for the purposes of this relaxation, since they have not
1475 yet been replaced with spaces.
1478 @section Non-Lvalue Arrays May Have Subscripts
1479 @cindex subscripting
1480 @cindex arrays, non-lvalue
1482 @cindex subscripting and function values
1483 In ISO C99, arrays that are not lvalues still decay to pointers, and
1484 may be subscripted, although they may not be modified or used after
1485 the next sequence point and the unary @samp{&} operator may not be
1486 applied to them. As an extension, GCC allows such arrays to be
1487 subscripted in C89 mode, though otherwise they do not decay to
1488 pointers outside C99 mode. For example,
1489 this is valid in GNU C though not valid in C89:
1493 struct foo @{int a[4];@};
1499 return f().a[index];
1505 @section Arithmetic on @code{void}- and Function-Pointers
1506 @cindex void pointers, arithmetic
1507 @cindex void, size of pointer to
1508 @cindex function pointers, arithmetic
1509 @cindex function, size of pointer to
1511 In GNU C, addition and subtraction operations are supported on pointers to
1512 @code{void} and on pointers to functions. This is done by treating the
1513 size of a @code{void} or of a function as 1.
1515 A consequence of this is that @code{sizeof} is also allowed on @code{void}
1516 and on function types, and returns 1.
1518 @opindex Wpointer-arith
1519 The option @option{-Wpointer-arith} requests a warning if these extensions
1523 @section Non-Constant Initializers
1524 @cindex initializers, non-constant
1525 @cindex non-constant initializers
1527 As in standard C++ and ISO C99, the elements of an aggregate initializer for an
1528 automatic variable are not required to be constant expressions in GNU C@.
1529 Here is an example of an initializer with run-time varying elements:
1532 foo (float f, float g)
1534 float beat_freqs[2] = @{ f-g, f+g @};
1539 @node Compound Literals
1540 @section Compound Literals
1541 @cindex constructor expressions
1542 @cindex initializations in expressions
1543 @cindex structures, constructor expression
1544 @cindex expressions, constructor
1545 @cindex compound literals
1546 @c The GNU C name for what C99 calls compound literals was "constructor expressions".
1548 ISO C99 supports compound literals. A compound literal looks like
1549 a cast containing an initializer. Its value is an object of the
1550 type specified in the cast, containing the elements specified in
1551 the initializer; it is an lvalue. As an extension, GCC supports
1552 compound literals in C89 mode and in C++.
1554 Usually, the specified type is a structure. Assume that
1555 @code{struct foo} and @code{structure} are declared as shown:
1558 struct foo @{int a; char b[2];@} structure;
1562 Here is an example of constructing a @code{struct foo} with a compound literal:
1565 structure = ((struct foo) @{x + y, 'a', 0@});
1569 This is equivalent to writing the following:
1573 struct foo temp = @{x + y, 'a', 0@};
1578 You can also construct an array. If all the elements of the compound literal
1579 are (made up of) simple constant expressions, suitable for use in
1580 initializers of objects of static storage duration, then the compound
1581 literal can be coerced to a pointer to its first element and used in
1582 such an initializer, as shown here:
1585 char **foo = (char *[]) @{ "x", "y", "z" @};
1588 Compound literals for scalar types and union types are is
1589 also allowed, but then the compound literal is equivalent
1592 As a GNU extension, GCC allows initialization of objects with static storage
1593 duration by compound literals (which is not possible in ISO C99, because
1594 the initializer is not a constant).
1595 It is handled as if the object was initialized only with the bracket
1596 enclosed list if the types of the compound literal and the object match.
1597 The initializer list of the compound literal must be constant.
1598 If the object being initialized has array type of unknown size, the size is
1599 determined by compound literal size.
1602 static struct foo x = (struct foo) @{1, 'a', 'b'@};
1603 static int y[] = (int []) @{1, 2, 3@};
1604 static int z[] = (int [3]) @{1@};
1608 The above lines are equivalent to the following:
1610 static struct foo x = @{1, 'a', 'b'@};
1611 static int y[] = @{1, 2, 3@};
1612 static int z[] = @{1, 0, 0@};
1615 @node Designated Inits
1616 @section Designated Initializers
1617 @cindex initializers with labeled elements
1618 @cindex labeled elements in initializers
1619 @cindex case labels in initializers
1620 @cindex designated initializers
1622 Standard C89 requires the elements of an initializer to appear in a fixed
1623 order, the same as the order of the elements in the array or structure
1626 In ISO C99 you can give the elements in any order, specifying the array
1627 indices or structure field names they apply to, and GNU C allows this as
1628 an extension in C89 mode as well. This extension is not
1629 implemented in GNU C++.
1631 To specify an array index, write
1632 @samp{[@var{index}] =} before the element value. For example,
1635 int a[6] = @{ [4] = 29, [2] = 15 @};
1642 int a[6] = @{ 0, 0, 15, 0, 29, 0 @};
1646 The index values must be constant expressions, even if the array being
1647 initialized is automatic.
1649 An alternative syntax for this which has been obsolete since GCC 2.5 but
1650 GCC still accepts is to write @samp{[@var{index}]} before the element
1651 value, with no @samp{=}.
1653 To initialize a range of elements to the same value, write
1654 @samp{[@var{first} ... @var{last}] = @var{value}}. This is a GNU
1655 extension. For example,
1658 int widths[] = @{ [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 @};
1662 If the value in it has side-effects, the side-effects will happen only once,
1663 not for each initialized field by the range initializer.
1666 Note that the length of the array is the highest value specified
1669 In a structure initializer, specify the name of a field to initialize
1670 with @samp{.@var{fieldname} =} before the element value. For example,
1671 given the following structure,
1674 struct point @{ int x, y; @};
1678 the following initialization
1681 struct point p = @{ .y = yvalue, .x = xvalue @};
1688 struct point p = @{ xvalue, yvalue @};
1691 Another syntax which has the same meaning, obsolete since GCC 2.5, is
1692 @samp{@var{fieldname}:}, as shown here:
1695 struct point p = @{ y: yvalue, x: xvalue @};
1699 The @samp{[@var{index}]} or @samp{.@var{fieldname}} is known as a
1700 @dfn{designator}. You can also use a designator (or the obsolete colon
1701 syntax) when initializing a union, to specify which element of the union
1702 should be used. For example,
1705 union foo @{ int i; double d; @};
1707 union foo f = @{ .d = 4 @};
1711 will convert 4 to a @code{double} to store it in the union using
1712 the second element. By contrast, casting 4 to type @code{union foo}
1713 would store it into the union as the integer @code{i}, since it is
1714 an integer. (@xref{Cast to Union}.)
1716 You can combine this technique of naming elements with ordinary C
1717 initialization of successive elements. Each initializer element that
1718 does not have a designator applies to the next consecutive element of the
1719 array or structure. For example,
1722 int a[6] = @{ [1] = v1, v2, [4] = v4 @};
1729 int a[6] = @{ 0, v1, v2, 0, v4, 0 @};
1732 Labeling the elements of an array initializer is especially useful
1733 when the indices are characters or belong to an @code{enum} type.
1738 = @{ [' '] = 1, ['\t'] = 1, ['\h'] = 1,
1739 ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 @};
1742 @cindex designator lists
1743 You can also write a series of @samp{.@var{fieldname}} and
1744 @samp{[@var{index}]} designators before an @samp{=} to specify a
1745 nested subobject to initialize; the list is taken relative to the
1746 subobject corresponding to the closest surrounding brace pair. For
1747 example, with the @samp{struct point} declaration above:
1750 struct point ptarray[10] = @{ [2].y = yv2, [2].x = xv2, [0].x = xv0 @};
1754 If the same field is initialized multiple times, it will have value from
1755 the last initialization. If any such overridden initialization has
1756 side-effect, it is unspecified whether the side-effect happens or not.
1757 Currently, GCC will discard them and issue a warning.
1760 @section Case Ranges
1762 @cindex ranges in case statements
1764 You can specify a range of consecutive values in a single @code{case} label,
1768 case @var{low} ... @var{high}:
1772 This has the same effect as the proper number of individual @code{case}
1773 labels, one for each integer value from @var{low} to @var{high}, inclusive.
1775 This feature is especially useful for ranges of ASCII character codes:
1781 @strong{Be careful:} Write spaces around the @code{...}, for otherwise
1782 it may be parsed wrong when you use it with integer values. For example,
1797 @section Cast to a Union Type
1798 @cindex cast to a union
1799 @cindex union, casting to a
1801 A cast to union type is similar to other casts, except that the type
1802 specified is a union type. You can specify the type either with
1803 @code{union @var{tag}} or with a typedef name. A cast to union is actually
1804 a constructor though, not a cast, and hence does not yield an lvalue like
1805 normal casts. (@xref{Compound Literals}.)
1807 The types that may be cast to the union type are those of the members
1808 of the union. Thus, given the following union and variables:
1811 union foo @{ int i; double d; @};
1817 both @code{x} and @code{y} can be cast to type @code{union foo}.
1819 Using the cast as the right-hand side of an assignment to a variable of
1820 union type is equivalent to storing in a member of the union:
1825 u = (union foo) x @equiv{} u.i = x
1826 u = (union foo) y @equiv{} u.d = y
1829 You can also use the union cast as a function argument:
1832 void hack (union foo);
1834 hack ((union foo) x);
1837 @node Mixed Declarations
1838 @section Mixed Declarations and Code
1839 @cindex mixed declarations and code
1840 @cindex declarations, mixed with code
1841 @cindex code, mixed with declarations
1843 ISO C99 and ISO C++ allow declarations and code to be freely mixed
1844 within compound statements. As an extension, GCC also allows this in
1845 C89 mode. For example, you could do:
1854 Each identifier is visible from where it is declared until the end of
1855 the enclosing block.
1857 @node Function Attributes
1858 @section Declaring Attributes of Functions
1859 @cindex function attributes
1860 @cindex declaring attributes of functions
1861 @cindex functions that never return
1862 @cindex functions that return more than once
1863 @cindex functions that have no side effects
1864 @cindex functions in arbitrary sections
1865 @cindex functions that behave like malloc
1866 @cindex @code{volatile} applied to function
1867 @cindex @code{const} applied to function
1868 @cindex functions with @code{printf}, @code{scanf}, @code{strftime} or @code{strfmon} style arguments
1869 @cindex functions with non-null pointer arguments
1870 @cindex functions that are passed arguments in registers on the 386
1871 @cindex functions that pop the argument stack on the 386
1872 @cindex functions that do not pop the argument stack on the 386
1873 @cindex functions that have different compilation options on the 386
1874 @cindex functions that have different optimization options
1876 In GNU C, you declare certain things about functions called in your program
1877 which help the compiler optimize function calls and check your code more
1880 The keyword @code{__attribute__} allows you to specify special
1881 attributes when making a declaration. This keyword is followed by an
1882 attribute specification inside double parentheses. The following
1883 attributes are currently defined for functions on all targets:
1884 @code{aligned}, @code{alloc_size}, @code{noreturn},
1885 @code{returns_twice}, @code{noinline}, @code{always_inline},
1886 @code{flatten}, @code{pure}, @code{const}, @code{nothrow},
1887 @code{sentinel}, @code{format}, @code{format_arg},
1888 @code{no_instrument_function}, @code{section}, @code{constructor},
1889 @code{destructor}, @code{used}, @code{unused}, @code{deprecated},
1890 @code{weak}, @code{malloc}, @code{alias}, @code{warn_unused_result},
1891 @code{nonnull}, @code{gnu_inline}, @code{externally_visible},
1892 @code{hot}, @code{cold}, @code{artificial}, @code{error}
1894 Several other attributes are defined for functions on particular
1895 target systems. Other attributes, including @code{section} are
1896 supported for variables declarations (@pxref{Variable Attributes}) and
1897 for types (@pxref{Type Attributes}).
1899 You may also specify attributes with @samp{__} preceding and following
1900 each keyword. This allows you to use them in header files without
1901 being concerned about a possible macro of the same name. For example,
1902 you may use @code{__noreturn__} instead of @code{noreturn}.
1904 @xref{Attribute Syntax}, for details of the exact syntax for using
1908 @c Keep this table alphabetized by attribute name. Treat _ as space.
1910 @item alias ("@var{target}")
1911 @cindex @code{alias} attribute
1912 The @code{alias} attribute causes the declaration to be emitted as an
1913 alias for another symbol, which must be specified. For instance,
1916 void __f () @{ /* @r{Do something.} */; @}
1917 void f () __attribute__ ((weak, alias ("__f")));
1920 defines @samp{f} to be a weak alias for @samp{__f}. In C++, the
1921 mangled name for the target must be used. It is an error if @samp{__f}
1922 is not defined in the same translation unit.
1924 Not all target machines support this attribute.
1926 @item aligned (@var{alignment})
1927 @cindex @code{aligned} attribute
1928 This attribute specifies a minimum alignment for the function,
1931 You cannot use this attribute to decrease the alignment of a function,
1932 only to increase it. However, when you explicitly specify a function
1933 alignment this will override the effect of the
1934 @option{-falign-functions} (@pxref{Optimize Options}) option for this
1937 Note that the effectiveness of @code{aligned} attributes may be
1938 limited by inherent limitations in your linker. On many systems, the
1939 linker is only able to arrange for functions to be aligned up to a
1940 certain maximum alignment. (For some linkers, the maximum supported
1941 alignment may be very very small.) See your linker documentation for
1942 further information.
1944 The @code{aligned} attribute can also be used for variables and fields
1945 (@pxref{Variable Attributes}.)
1948 @cindex @code{alloc_size} attribute
1949 The @code{alloc_size} attribute is used to tell the compiler that the
1950 function return value points to memory, where the size is given by
1951 one or two of the functions parameters. GCC uses this
1952 information to improve the correctness of @code{__builtin_object_size}.
1954 The function parameter(s) denoting the allocated size are specified by
1955 one or two integer arguments supplied to the attribute. The allocated size
1956 is either the value of the single function argument specified or the product
1957 of the two function arguments specified. Argument numbering starts at
1963 void* my_calloc(size_t, size_t) __attribute__((alloc_size(1,2)))
1964 void my_realloc(void*, size_t) __attribute__((alloc_size(2)))
1967 declares that my_calloc will return memory of the size given by
1968 the product of parameter 1 and 2 and that my_realloc will return memory
1969 of the size given by parameter 2.
1972 @cindex @code{always_inline} function attribute
1973 Generally, functions are not inlined unless optimization is specified.
1974 For functions declared inline, this attribute inlines the function even
1975 if no optimization level was specified.
1978 @cindex @code{gnu_inline} function attribute
1979 This attribute should be used with a function which is also declared
1980 with the @code{inline} keyword. It directs GCC to treat the function
1981 as if it were defined in gnu89 mode even when compiling in C99 or
1984 If the function is declared @code{extern}, then this definition of the
1985 function is used only for inlining. In no case is the function
1986 compiled as a standalone function, not even if you take its address
1987 explicitly. Such an address becomes an external reference, as if you
1988 had only declared the function, and had not defined it. This has
1989 almost the effect of a macro. The way to use this is to put a
1990 function definition in a header file with this attribute, and put
1991 another copy of the function, without @code{extern}, in a library
1992 file. The definition in the header file will cause most calls to the
1993 function to be inlined. If any uses of the function remain, they will
1994 refer to the single copy in the library. Note that the two
1995 definitions of the functions need not be precisely the same, although
1996 if they do not have the same effect your program may behave oddly.
1998 In C, if the function is neither @code{extern} nor @code{static}, then
1999 the function is compiled as a standalone function, as well as being
2000 inlined where possible.
2002 This is how GCC traditionally handled functions declared
2003 @code{inline}. Since ISO C99 specifies a different semantics for
2004 @code{inline}, this function attribute is provided as a transition
2005 measure and as a useful feature in its own right. This attribute is
2006 available in GCC 4.1.3 and later. It is available if either of the
2007 preprocessor macros @code{__GNUC_GNU_INLINE__} or
2008 @code{__GNUC_STDC_INLINE__} are defined. @xref{Inline,,An Inline
2009 Function is As Fast As a Macro}.
2011 In C++, this attribute does not depend on @code{extern} in any way,
2012 but it still requires the @code{inline} keyword to enable its special
2016 @cindex @code{artificial} function attribute
2017 This attribute is useful for small inline wrappers which if possible
2018 should appear during debugging as a unit, depending on the debug
2019 info format it will either mean marking the function as artificial
2020 or using the caller location for all instructions within the inlined
2024 @cindex @code{flatten} function attribute
2025 Generally, inlining into a function is limited. For a function marked with
2026 this attribute, every call inside this function will be inlined, if possible.
2027 Whether the function itself is considered for inlining depends on its size and
2028 the current inlining parameters.
2030 @item error ("@var{message}")
2031 @cindex @code{error} function attribute
2032 If this attribute is used on a function declaration and a call to such a function
2033 is not eliminated through dead code elimination or other optimizations, an error
2034 which will include @var{message} will be diagnosed. This is useful
2035 for compile time checking, especially together with @code{__builtin_constant_p}
2036 and inline functions where checking the inline function arguments is not
2037 possible through @code{extern char [(condition) ? 1 : -1];} tricks.
2038 While it is possible to leave the function undefined and thus invoke
2039 a link failure, when using this attribute the problem will be diagnosed
2040 earlier and with exact location of the call even in presence of inline
2041 functions or when not emitting debugging information.
2043 @item warning ("@var{message}")
2044 @cindex @code{warning} function attribute
2045 If this attribute is used on a function declaration and a call to such a function
2046 is not eliminated through dead code elimination or other optimizations, a warning
2047 which will include @var{message} will be diagnosed. This is useful
2048 for compile time checking, especially together with @code{__builtin_constant_p}
2049 and inline functions. While it is possible to define the function with
2050 a message in @code{.gnu.warning*} section, when using this attribute the problem
2051 will be diagnosed earlier and with exact location of the call even in presence
2052 of inline functions or when not emitting debugging information.
2055 @cindex functions that do pop the argument stack on the 386
2057 On the Intel 386, the @code{cdecl} attribute causes the compiler to
2058 assume that the calling function will pop off the stack space used to
2059 pass arguments. This is
2060 useful to override the effects of the @option{-mrtd} switch.
2063 @cindex @code{const} function attribute
2064 Many functions do not examine any values except their arguments, and
2065 have no effects except the return value. Basically this is just slightly
2066 more strict class than the @code{pure} attribute below, since function is not
2067 allowed to read global memory.
2069 @cindex pointer arguments
2070 Note that a function that has pointer arguments and examines the data
2071 pointed to must @emph{not} be declared @code{const}. Likewise, a
2072 function that calls a non-@code{const} function usually must not be
2073 @code{const}. It does not make sense for a @code{const} function to
2076 The attribute @code{const} is not implemented in GCC versions earlier
2077 than 2.5. An alternative way to declare that a function has no side
2078 effects, which works in the current version and in some older versions,
2082 typedef int intfn ();
2084 extern const intfn square;
2087 This approach does not work in GNU C++ from 2.6.0 on, since the language
2088 specifies that the @samp{const} must be attached to the return value.
2092 @itemx constructor (@var{priority})
2093 @itemx destructor (@var{priority})
2094 @cindex @code{constructor} function attribute
2095 @cindex @code{destructor} function attribute
2096 The @code{constructor} attribute causes the function to be called
2097 automatically before execution enters @code{main ()}. Similarly, the
2098 @code{destructor} attribute causes the function to be called
2099 automatically after @code{main ()} has completed or @code{exit ()} has
2100 been called. Functions with these attributes are useful for
2101 initializing data that will be used implicitly during the execution of
2104 You may provide an optional integer priority to control the order in
2105 which constructor and destructor functions are run. A constructor
2106 with a smaller priority number runs before a constructor with a larger
2107 priority number; the opposite relationship holds for destructors. So,
2108 if you have a constructor that allocates a resource and a destructor
2109 that deallocates the same resource, both functions typically have the
2110 same priority. The priorities for constructor and destructor
2111 functions are the same as those specified for namespace-scope C++
2112 objects (@pxref{C++ Attributes}).
2114 These attributes are not currently implemented for Objective-C@.
2117 @itemx deprecated (@var{msg})
2118 @cindex @code{deprecated} attribute.
2119 The @code{deprecated} attribute results in a warning if the function
2120 is used anywhere in the source file. This is useful when identifying
2121 functions that are expected to be removed in a future version of a
2122 program. The warning also includes the location of the declaration
2123 of the deprecated function, to enable users to easily find further
2124 information about why the function is deprecated, or what they should
2125 do instead. Note that the warnings only occurs for uses:
2128 int old_fn () __attribute__ ((deprecated));
2130 int (*fn_ptr)() = old_fn;
2133 results in a warning on line 3 but not line 2. The optional msg
2134 argument, which must be a string, will be printed in the warning if
2137 The @code{deprecated} attribute can also be used for variables and
2138 types (@pxref{Variable Attributes}, @pxref{Type Attributes}.)
2141 @cindex @code{__declspec(dllexport)}
2142 On Microsoft Windows targets and Symbian OS targets the
2143 @code{dllexport} attribute causes the compiler to provide a global
2144 pointer to a pointer in a DLL, so that it can be referenced with the
2145 @code{dllimport} attribute. On Microsoft Windows targets, the pointer
2146 name is formed by combining @code{_imp__} and the function or variable
2149 You can use @code{__declspec(dllexport)} as a synonym for
2150 @code{__attribute__ ((dllexport))} for compatibility with other
2153 On systems that support the @code{visibility} attribute, this
2154 attribute also implies ``default'' visibility. It is an error to
2155 explicitly specify any other visibility.
2157 Currently, the @code{dllexport} attribute is ignored for inlined
2158 functions, unless the @option{-fkeep-inline-functions} flag has been
2159 used. The attribute is also ignored for undefined symbols.
2161 When applied to C++ classes, the attribute marks defined non-inlined
2162 member functions and static data members as exports. Static consts
2163 initialized in-class are not marked unless they are also defined
2166 For Microsoft Windows targets there are alternative methods for
2167 including the symbol in the DLL's export table such as using a
2168 @file{.def} file with an @code{EXPORTS} section or, with GNU ld, using
2169 the @option{--export-all} linker flag.
2172 @cindex @code{__declspec(dllimport)}
2173 On Microsoft Windows and Symbian OS targets, the @code{dllimport}
2174 attribute causes the compiler to reference a function or variable via
2175 a global pointer to a pointer that is set up by the DLL exporting the
2176 symbol. The attribute implies @code{extern}. On Microsoft Windows
2177 targets, the pointer name is formed by combining @code{_imp__} and the
2178 function or variable name.
2180 You can use @code{__declspec(dllimport)} as a synonym for
2181 @code{__attribute__ ((dllimport))} for compatibility with other
2184 On systems that support the @code{visibility} attribute, this
2185 attribute also implies ``default'' visibility. It is an error to
2186 explicitly specify any other visibility.
2188 Currently, the attribute is ignored for inlined functions. If the
2189 attribute is applied to a symbol @emph{definition}, an error is reported.
2190 If a symbol previously declared @code{dllimport} is later defined, the
2191 attribute is ignored in subsequent references, and a warning is emitted.
2192 The attribute is also overridden by a subsequent declaration as
2195 When applied to C++ classes, the attribute marks non-inlined
2196 member functions and static data members as imports. However, the
2197 attribute is ignored for virtual methods to allow creation of vtables
2200 On the SH Symbian OS target the @code{dllimport} attribute also has
2201 another affect---it can cause the vtable and run-time type information
2202 for a class to be exported. This happens when the class has a
2203 dllimport'ed constructor or a non-inline, non-pure virtual function
2204 and, for either of those two conditions, the class also has an inline
2205 constructor or destructor and has a key function that is defined in
2206 the current translation unit.
2208 For Microsoft Windows based targets the use of the @code{dllimport}
2209 attribute on functions is not necessary, but provides a small
2210 performance benefit by eliminating a thunk in the DLL@. The use of the
2211 @code{dllimport} attribute on imported variables was required on older
2212 versions of the GNU linker, but can now be avoided by passing the
2213 @option{--enable-auto-import} switch to the GNU linker. As with
2214 functions, using the attribute for a variable eliminates a thunk in
2217 One drawback to using this attribute is that a pointer to a
2218 @emph{variable} marked as @code{dllimport} cannot be used as a constant
2219 address. However, a pointer to a @emph{function} with the
2220 @code{dllimport} attribute can be used as a constant initializer; in
2221 this case, the address of a stub function in the import lib is
2222 referenced. On Microsoft Windows targets, the attribute can be disabled
2223 for functions by setting the @option{-mnop-fun-dllimport} flag.
2226 @cindex eight bit data on the H8/300, H8/300H, and H8S
2227 Use this attribute on the H8/300, H8/300H, and H8S to indicate that the specified
2228 variable should be placed into the eight bit data section.
2229 The compiler will generate more efficient code for certain operations
2230 on data in the eight bit data area. Note the eight bit data area is limited to
2233 You must use GAS and GLD from GNU binutils version 2.7 or later for
2234 this attribute to work correctly.
2236 @item exception_handler
2237 @cindex exception handler functions on the Blackfin processor
2238 Use this attribute on the Blackfin to indicate that the specified function
2239 is an exception handler. The compiler will generate function entry and
2240 exit sequences suitable for use in an exception handler when this
2241 attribute is present.
2243 @item externally_visible
2244 @cindex @code{externally_visible} attribute.
2245 This attribute, attached to a global variable or function, nullifies
2246 the effect of the @option{-fwhole-program} command-line option, so the
2247 object remains visible outside the current compilation unit.
2250 @cindex functions which handle memory bank switching
2251 On 68HC11 and 68HC12 the @code{far} attribute causes the compiler to
2252 use a calling convention that takes care of switching memory banks when
2253 entering and leaving a function. This calling convention is also the
2254 default when using the @option{-mlong-calls} option.
2256 On 68HC12 the compiler will use the @code{call} and @code{rtc} instructions
2257 to call and return from a function.
2259 On 68HC11 the compiler will generate a sequence of instructions
2260 to invoke a board-specific routine to switch the memory bank and call the
2261 real function. The board-specific routine simulates a @code{call}.
2262 At the end of a function, it will jump to a board-specific routine
2263 instead of using @code{rts}. The board-specific return routine simulates
2267 @cindex functions that pop the argument stack on the 386
2268 On the Intel 386, the @code{fastcall} attribute causes the compiler to
2269 pass the first argument (if of integral type) in the register ECX and
2270 the second argument (if of integral type) in the register EDX@. Subsequent
2271 and other typed arguments are passed on the stack. The called function will
2272 pop the arguments off the stack. If the number of arguments is variable all
2273 arguments are pushed on the stack.
2275 @item format (@var{archetype}, @var{string-index}, @var{first-to-check})
2276 @cindex @code{format} function attribute
2278 The @code{format} attribute specifies that a function takes @code{printf},
2279 @code{scanf}, @code{strftime} or @code{strfmon} style arguments which
2280 should be type-checked against a format string. For example, the
2285 my_printf (void *my_object, const char *my_format, ...)
2286 __attribute__ ((format (printf, 2, 3)));
2290 causes the compiler to check the arguments in calls to @code{my_printf}
2291 for consistency with the @code{printf} style format string argument
2294 The parameter @var{archetype} determines how the format string is
2295 interpreted, and should be @code{printf}, @code{scanf}, @code{strftime},
2296 @code{gnu_printf}, @code{gnu_scanf}, @code{gnu_strftime} or
2297 @code{strfmon}. (You can also use @code{__printf__},
2298 @code{__scanf__}, @code{__strftime__} or @code{__strfmon__}.) On
2299 MinGW targets, @code{ms_printf}, @code{ms_scanf}, and
2300 @code{ms_strftime} are also present.
2301 @var{archtype} values such as @code{printf} refer to the formats accepted
2302 by the system's C run-time library, while @code{gnu_} values always refer
2303 to the formats accepted by the GNU C Library. On Microsoft Windows
2304 targets, @code{ms_} values refer to the formats accepted by the
2305 @file{msvcrt.dll} library.
2306 The parameter @var{string-index}
2307 specifies which argument is the format string argument (starting
2308 from 1), while @var{first-to-check} is the number of the first
2309 argument to check against the format string. For functions
2310 where the arguments are not available to be checked (such as
2311 @code{vprintf}), specify the third parameter as zero. In this case the
2312 compiler only checks the format string for consistency. For
2313 @code{strftime} formats, the third parameter is required to be zero.
2314 Since non-static C++ methods have an implicit @code{this} argument, the
2315 arguments of such methods should be counted from two, not one, when
2316 giving values for @var{string-index} and @var{first-to-check}.
2318 In the example above, the format string (@code{my_format}) is the second
2319 argument of the function @code{my_print}, and the arguments to check
2320 start with the third argument, so the correct parameters for the format
2321 attribute are 2 and 3.
2323 @opindex ffreestanding
2324 @opindex fno-builtin
2325 The @code{format} attribute allows you to identify your own functions
2326 which take format strings as arguments, so that GCC can check the
2327 calls to these functions for errors. The compiler always (unless
2328 @option{-ffreestanding} or @option{-fno-builtin} is used) checks formats
2329 for the standard library functions @code{printf}, @code{fprintf},
2330 @code{sprintf}, @code{scanf}, @code{fscanf}, @code{sscanf}, @code{strftime},
2331 @code{vprintf}, @code{vfprintf} and @code{vsprintf} whenever such
2332 warnings are requested (using @option{-Wformat}), so there is no need to
2333 modify the header file @file{stdio.h}. In C99 mode, the functions
2334 @code{snprintf}, @code{vsnprintf}, @code{vscanf}, @code{vfscanf} and
2335 @code{vsscanf} are also checked. Except in strictly conforming C
2336 standard modes, the X/Open function @code{strfmon} is also checked as
2337 are @code{printf_unlocked} and @code{fprintf_unlocked}.
2338 @xref{C Dialect Options,,Options Controlling C Dialect}.
2340 The target may provide additional types of format checks.
2341 @xref{Target Format Checks,,Format Checks Specific to Particular
2344 @item format_arg (@var{string-index})
2345 @cindex @code{format_arg} function attribute
2346 @opindex Wformat-nonliteral
2347 The @code{format_arg} attribute specifies that a function takes a format
2348 string for a @code{printf}, @code{scanf}, @code{strftime} or
2349 @code{strfmon} style function and modifies it (for example, to translate
2350 it into another language), so the result can be passed to a
2351 @code{printf}, @code{scanf}, @code{strftime} or @code{strfmon} style
2352 function (with the remaining arguments to the format function the same
2353 as they would have been for the unmodified string). For example, the
2358 my_dgettext (char *my_domain, const char *my_format)
2359 __attribute__ ((format_arg (2)));
2363 causes the compiler to check the arguments in calls to a @code{printf},
2364 @code{scanf}, @code{strftime} or @code{strfmon} type function, whose
2365 format string argument is a call to the @code{my_dgettext} function, for
2366 consistency with the format string argument @code{my_format}. If the
2367 @code{format_arg} attribute had not been specified, all the compiler
2368 could tell in such calls to format functions would be that the format
2369 string argument is not constant; this would generate a warning when
2370 @option{-Wformat-nonliteral} is used, but the calls could not be checked
2371 without the attribute.
2373 The parameter @var{string-index} specifies which argument is the format
2374 string argument (starting from one). Since non-static C++ methods have
2375 an implicit @code{this} argument, the arguments of such methods should
2376 be counted from two.
2378 The @code{format-arg} attribute allows you to identify your own
2379 functions which modify format strings, so that GCC can check the
2380 calls to @code{printf}, @code{scanf}, @code{strftime} or @code{strfmon}
2381 type function whose operands are a call to one of your own function.
2382 The compiler always treats @code{gettext}, @code{dgettext}, and
2383 @code{dcgettext} in this manner except when strict ISO C support is
2384 requested by @option{-ansi} or an appropriate @option{-std} option, or
2385 @option{-ffreestanding} or @option{-fno-builtin}
2386 is used. @xref{C Dialect Options,,Options
2387 Controlling C Dialect}.
2389 @item function_vector
2390 @cindex calling functions through the function vector on H8/300, M16C, M32C and SH2A processors
2391 Use this attribute on the H8/300, H8/300H, and H8S to indicate that the specified
2392 function should be called through the function vector. Calling a
2393 function through the function vector will reduce code size, however;
2394 the function vector has a limited size (maximum 128 entries on the H8/300
2395 and 64 entries on the H8/300H and H8S) and shares space with the interrupt vector.
2397 In SH2A target, this attribute declares a function to be called using the
2398 TBR relative addressing mode. The argument to this attribute is the entry
2399 number of the same function in a vector table containing all the TBR
2400 relative addressable functions. For the successful jump, register TBR
2401 should contain the start address of this TBR relative vector table.
2402 In the startup routine of the user application, user needs to care of this
2403 TBR register initialization. The TBR relative vector table can have at
2404 max 256 function entries. The jumps to these functions will be generated
2405 using a SH2A specific, non delayed branch instruction JSR/N @@(disp8,TBR).
2406 You must use GAS and GLD from GNU binutils version 2.7 or later for
2407 this attribute to work correctly.
2409 Please refer the example of M16C target, to see the use of this
2410 attribute while declaring a function,
2412 In an application, for a function being called once, this attribute will
2413 save at least 8 bytes of code; and if other successive calls are being
2414 made to the same function, it will save 2 bytes of code per each of these
2417 On M16C/M32C targets, the @code{function_vector} attribute declares a
2418 special page subroutine call function. Use of this attribute reduces
2419 the code size by 2 bytes for each call generated to the
2420 subroutine. The argument to the attribute is the vector number entry
2421 from the special page vector table which contains the 16 low-order
2422 bits of the subroutine's entry address. Each vector table has special
2423 page number (18 to 255) which are used in @code{jsrs} instruction.
2424 Jump addresses of the routines are generated by adding 0x0F0000 (in
2425 case of M16C targets) or 0xFF0000 (in case of M32C targets), to the 2
2426 byte addresses set in the vector table. Therefore you need to ensure
2427 that all the special page vector routines should get mapped within the
2428 address range 0x0F0000 to 0x0FFFFF (for M16C) and 0xFF0000 to 0xFFFFFF
2431 In the following example 2 bytes will be saved for each call to
2432 function @code{foo}.
2435 void foo (void) __attribute__((function_vector(0x18)));
2446 If functions are defined in one file and are called in another file,
2447 then be sure to write this declaration in both files.
2449 This attribute is ignored for R8C target.
2452 @cindex interrupt handler functions
2453 Use this attribute on the ARM, AVR, CRX, M32C, M32R/D, m68k, MIPS
2454 and Xstormy16 ports to indicate that the specified function is an
2455 interrupt handler. The compiler will generate function entry and exit
2456 sequences suitable for use in an interrupt handler when this attribute
2459 Note, interrupt handlers for the Blackfin, H8/300, H8/300H, H8S, and
2460 SH processors can be specified via the @code{interrupt_handler} attribute.
2462 Note, on the AVR, interrupts will be enabled inside the function.
2464 Note, for the ARM, you can specify the kind of interrupt to be handled by
2465 adding an optional parameter to the interrupt attribute like this:
2468 void f () __attribute__ ((interrupt ("IRQ")));
2471 Permissible values for this parameter are: IRQ, FIQ, SWI, ABORT and UNDEF@.
2473 On ARMv7-M the interrupt type is ignored, and the attribute means the function
2474 may be called with a word aligned stack pointer.
2476 On MIPS targets, you can use the following attributes to modify the behavior
2477 of an interrupt handler:
2479 @item use_shadow_register_set
2480 @cindex @code{use_shadow_register_set} attribute
2481 Assume that the handler uses a shadow register set, instead of
2482 the main general-purpose registers.
2484 @item keep_interrupts_masked
2485 @cindex @code{keep_interrupts_masked} attribute
2486 Keep interrupts masked for the whole function. Without this attribute,
2487 GCC tries to reenable interrupts for as much of the function as it can.
2489 @item use_debug_exception_return
2490 @cindex @code{use_debug_exception_return} attribute
2491 Return using the @code{deret} instruction. Interrupt handlers that don't
2492 have this attribute return using @code{eret} instead.
2495 You can use any combination of these attributes, as shown below:
2497 void __attribute__ ((interrupt)) v0 ();
2498 void __attribute__ ((interrupt, use_shadow_register_set)) v1 ();
2499 void __attribute__ ((interrupt, keep_interrupts_masked)) v2 ();
2500 void __attribute__ ((interrupt, use_debug_exception_return)) v3 ();
2501 void __attribute__ ((interrupt, use_shadow_register_set,
2502 keep_interrupts_masked)) v4 ();
2503 void __attribute__ ((interrupt, use_shadow_register_set,
2504 use_debug_exception_return)) v5 ();
2505 void __attribute__ ((interrupt, keep_interrupts_masked,
2506 use_debug_exception_return)) v6 ();
2507 void __attribute__ ((interrupt, use_shadow_register_set,
2508 keep_interrupts_masked,
2509 use_debug_exception_return)) v7 ();
2512 @item interrupt_handler
2513 @cindex interrupt handler functions on the Blackfin, m68k, H8/300 and SH processors
2514 Use this attribute on the Blackfin, m68k, H8/300, H8/300H, H8S, and SH to
2515 indicate that the specified function is an interrupt handler. The compiler
2516 will generate function entry and exit sequences suitable for use in an
2517 interrupt handler when this attribute is present.
2519 @item interrupt_thread
2520 @cindex interrupt thread functions on fido
2521 Use this attribute on fido, a subarchitecture of the m68k, to indicate
2522 that the specified function is an interrupt handler that is designed
2523 to run as a thread. The compiler omits generate prologue/epilogue
2524 sequences and replaces the return instruction with a @code{sleep}
2525 instruction. This attribute is available only on fido.
2528 @cindex interrupt service routines on ARM
2529 Use this attribute on ARM to write Interrupt Service Routines. This is an
2530 alias to the @code{interrupt} attribute above.
2533 @cindex User stack pointer in interrupts on the Blackfin
2534 When used together with @code{interrupt_handler}, @code{exception_handler}
2535 or @code{nmi_handler}, code will be generated to load the stack pointer
2536 from the USP register in the function prologue.
2539 @cindex @code{l1_text} function attribute
2540 This attribute specifies a function to be placed into L1 Instruction
2541 SRAM@. The function will be put into a specific section named @code{.l1.text}.
2542 With @option{-mfdpic}, function calls with a such function as the callee
2543 or caller will use inlined PLT.
2545 @item long_call/short_call
2546 @cindex indirect calls on ARM
2547 This attribute specifies how a particular function is called on
2548 ARM@. Both attributes override the @option{-mlong-calls} (@pxref{ARM Options})
2549 command line switch and @code{#pragma long_calls} settings. The
2550 @code{long_call} attribute indicates that the function might be far
2551 away from the call site and require a different (more expensive)
2552 calling sequence. The @code{short_call} attribute always places
2553 the offset to the function from the call site into the @samp{BL}
2554 instruction directly.
2556 @item longcall/shortcall
2557 @cindex functions called via pointer on the RS/6000 and PowerPC
2558 On the Blackfin, RS/6000 and PowerPC, the @code{longcall} attribute
2559 indicates that the function might be far away from the call site and
2560 require a different (more expensive) calling sequence. The
2561 @code{shortcall} attribute indicates that the function is always close
2562 enough for the shorter calling sequence to be used. These attributes
2563 override both the @option{-mlongcall} switch and, on the RS/6000 and
2564 PowerPC, the @code{#pragma longcall} setting.
2566 @xref{RS/6000 and PowerPC Options}, for more information on whether long
2567 calls are necessary.
2569 @item long_call/near/far
2570 @cindex indirect calls on MIPS
2571 These attributes specify how a particular function is called on MIPS@.
2572 The attributes override the @option{-mlong-calls} (@pxref{MIPS Options})
2573 command-line switch. The @code{long_call} and @code{far} attributes are
2574 synonyms, and cause the compiler to always call
2575 the function by first loading its address into a register, and then using
2576 the contents of that register. The @code{near} attribute has the opposite
2577 effect; it specifies that non-PIC calls should be made using the more
2578 efficient @code{jal} instruction.
2581 @cindex @code{malloc} attribute
2582 The @code{malloc} attribute is used to tell the compiler that a function
2583 may be treated as if any non-@code{NULL} pointer it returns cannot
2584 alias any other pointer valid when the function returns.
2585 This will often improve optimization.
2586 Standard functions with this property include @code{malloc} and
2587 @code{calloc}. @code{realloc}-like functions have this property as
2588 long as the old pointer is never referred to (including comparing it
2589 to the new pointer) after the function returns a non-@code{NULL}
2592 @item mips16/nomips16
2593 @cindex @code{mips16} attribute
2594 @cindex @code{nomips16} attribute
2596 On MIPS targets, you can use the @code{mips16} and @code{nomips16}
2597 function attributes to locally select or turn off MIPS16 code generation.
2598 A function with the @code{mips16} attribute is emitted as MIPS16 code,
2599 while MIPS16 code generation is disabled for functions with the
2600 @code{nomips16} attribute. These attributes override the
2601 @option{-mips16} and @option{-mno-mips16} options on the command line
2602 (@pxref{MIPS Options}).
2604 When compiling files containing mixed MIPS16 and non-MIPS16 code, the
2605 preprocessor symbol @code{__mips16} reflects the setting on the command line,
2606 not that within individual functions. Mixed MIPS16 and non-MIPS16 code
2607 may interact badly with some GCC extensions such as @code{__builtin_apply}
2608 (@pxref{Constructing Calls}).
2610 @item model (@var{model-name})
2611 @cindex function addressability on the M32R/D
2612 @cindex variable addressability on the IA-64
2614 On the M32R/D, use this attribute to set the addressability of an
2615 object, and of the code generated for a function. The identifier
2616 @var{model-name} is one of @code{small}, @code{medium}, or
2617 @code{large}, representing each of the code models.
2619 Small model objects live in the lower 16MB of memory (so that their
2620 addresses can be loaded with the @code{ld24} instruction), and are
2621 callable with the @code{bl} instruction.
2623 Medium model objects may live anywhere in the 32-bit address space (the
2624 compiler will generate @code{seth/add3} instructions to load their addresses),
2625 and are callable with the @code{bl} instruction.
2627 Large model objects may live anywhere in the 32-bit address space (the
2628 compiler will generate @code{seth/add3} instructions to load their addresses),
2629 and may not be reachable with the @code{bl} instruction (the compiler will
2630 generate the much slower @code{seth/add3/jl} instruction sequence).
2632 On IA-64, use this attribute to set the addressability of an object.
2633 At present, the only supported identifier for @var{model-name} is
2634 @code{small}, indicating addressability via ``small'' (22-bit)
2635 addresses (so that their addresses can be loaded with the @code{addl}
2636 instruction). Caveat: such addressing is by definition not position
2637 independent and hence this attribute must not be used for objects
2638 defined by shared libraries.
2640 @item ms_abi/sysv_abi
2641 @cindex @code{ms_abi} attribute
2642 @cindex @code{sysv_abi} attribute
2644 On 64-bit x86_64-*-* targets, you can use an ABI attribute to indicate
2645 which calling convention should be used for a function. The @code{ms_abi}
2646 attribute tells the compiler to use the Microsoft ABI, while the
2647 @code{sysv_abi} attribute tells the compiler to use the ABI used on
2648 GNU/Linux and other systems. The default is to use the Microsoft ABI
2649 when targeting Windows. On all other systems, the default is the AMD ABI.
2651 Note, This feature is currently sorried out for Windows targets trying to
2654 @cindex function without a prologue/epilogue code
2655 Use this attribute on the ARM, AVR, IP2K and SPU ports to indicate that
2656 the specified function does not need prologue/epilogue sequences generated by
2657 the compiler. It is up to the programmer to provide these sequences. The
2658 only statements that can be safely included in naked functions are
2659 @code{asm} statements that do not have operands. All other statements,
2660 including declarations of local variables, @code{if} statements, and so
2661 forth, should be avoided. Naked functions should be used to implement the
2662 body of an assembly function, while allowing the compiler to construct
2663 the requisite function declaration for the assembler.
2666 @cindex functions which do not handle memory bank switching on 68HC11/68HC12
2667 On 68HC11 and 68HC12 the @code{near} attribute causes the compiler to
2668 use the normal calling convention based on @code{jsr} and @code{rts}.
2669 This attribute can be used to cancel the effect of the @option{-mlong-calls}
2673 @cindex Allow nesting in an interrupt handler on the Blackfin processor.
2674 Use this attribute together with @code{interrupt_handler},
2675 @code{exception_handler} or @code{nmi_handler} to indicate that the function
2676 entry code should enable nested interrupts or exceptions.
2679 @cindex NMI handler functions on the Blackfin processor
2680 Use this attribute on the Blackfin to indicate that the specified function
2681 is an NMI handler. The compiler will generate function entry and
2682 exit sequences suitable for use in an NMI handler when this
2683 attribute is present.
2685 @item no_instrument_function
2686 @cindex @code{no_instrument_function} function attribute
2687 @opindex finstrument-functions
2688 If @option{-finstrument-functions} is given, profiling function calls will
2689 be generated at entry and exit of most user-compiled functions.
2690 Functions with this attribute will not be so instrumented.
2693 @cindex @code{noinline} function attribute
2694 This function attribute prevents a function from being considered for
2696 @c Don't enumerate the optimizations by name here; we try to be
2697 @c future-compatible with this mechanism.
2698 If the function does not have side-effects, there are optimizations
2699 other than inlining that causes function calls to be optimized away,
2700 although the function call is live. To keep such calls from being
2705 (@pxref{Extended Asm}) in the called function, to serve as a special
2708 @item nonnull (@var{arg-index}, @dots{})
2709 @cindex @code{nonnull} function attribute
2710 The @code{nonnull} attribute specifies that some function parameters should
2711 be non-null pointers. For instance, the declaration:
2715 my_memcpy (void *dest, const void *src, size_t len)
2716 __attribute__((nonnull (1, 2)));
2720 causes the compiler to check that, in calls to @code{my_memcpy},
2721 arguments @var{dest} and @var{src} are non-null. If the compiler
2722 determines that a null pointer is passed in an argument slot marked
2723 as non-null, and the @option{-Wnonnull} option is enabled, a warning
2724 is issued. The compiler may also choose to make optimizations based
2725 on the knowledge that certain function arguments will not be null.
2727 If no argument index list is given to the @code{nonnull} attribute,
2728 all pointer arguments are marked as non-null. To illustrate, the
2729 following declaration is equivalent to the previous example:
2733 my_memcpy (void *dest, const void *src, size_t len)
2734 __attribute__((nonnull));
2738 @cindex @code{noreturn} function attribute
2739 A few standard library functions, such as @code{abort} and @code{exit},
2740 cannot return. GCC knows this automatically. Some programs define
2741 their own functions that never return. You can declare them
2742 @code{noreturn} to tell the compiler this fact. For example,
2746 void fatal () __attribute__ ((noreturn));
2749 fatal (/* @r{@dots{}} */)
2751 /* @r{@dots{}} */ /* @r{Print error message.} */ /* @r{@dots{}} */
2757 The @code{noreturn} keyword tells the compiler to assume that
2758 @code{fatal} cannot return. It can then optimize without regard to what
2759 would happen if @code{fatal} ever did return. This makes slightly
2760 better code. More importantly, it helps avoid spurious warnings of
2761 uninitialized variables.
2763 The @code{noreturn} keyword does not affect the exceptional path when that
2764 applies: a @code{noreturn}-marked function may still return to the caller
2765 by throwing an exception or calling @code{longjmp}.
2767 Do not assume that registers saved by the calling function are
2768 restored before calling the @code{noreturn} function.
2770 It does not make sense for a @code{noreturn} function to have a return
2771 type other than @code{void}.
2773 The attribute @code{noreturn} is not implemented in GCC versions
2774 earlier than 2.5. An alternative way to declare that a function does
2775 not return, which works in the current version and in some older
2776 versions, is as follows:
2779 typedef void voidfn ();
2781 volatile voidfn fatal;
2784 This approach does not work in GNU C++.
2787 @cindex @code{nothrow} function attribute
2788 The @code{nothrow} attribute is used to inform the compiler that a
2789 function cannot throw an exception. For example, most functions in
2790 the standard C library can be guaranteed not to throw an exception
2791 with the notable exceptions of @code{qsort} and @code{bsearch} that
2792 take function pointer arguments. The @code{nothrow} attribute is not
2793 implemented in GCC versions earlier than 3.3.
2796 @cindex @code{optimize} function attribute
2797 The @code{optimize} attribute is used to specify that a function is to
2798 be compiled with different optimization options than specified on the
2799 command line. Arguments can either be numbers or strings. Numbers
2800 are assumed to be an optimization level. Strings that begin with
2801 @code{O} are assumed to be an optimization option, while other options
2802 are assumed to be used with a @code{-f} prefix. You can also use the
2803 @samp{#pragma GCC optimize} pragma to set the optimization options
2804 that affect more than one function.
2805 @xref{Function Specific Option Pragmas}, for details about the
2806 @samp{#pragma GCC optimize} pragma.
2808 This can be used for instance to have frequently executed functions
2809 compiled with more aggressive optimization options that produce faster
2810 and larger code, while other functions can be called with less
2814 @cindex @code{pure} function attribute
2815 Many functions have no effects except the return value and their
2816 return value depends only on the parameters and/or global variables.
2817 Such a function can be subject
2818 to common subexpression elimination and loop optimization just as an
2819 arithmetic operator would be. These functions should be declared
2820 with the attribute @code{pure}. For example,
2823 int square (int) __attribute__ ((pure));
2827 says that the hypothetical function @code{square} is safe to call
2828 fewer times than the program says.
2830 Some of common examples of pure functions are @code{strlen} or @code{memcmp}.
2831 Interesting non-pure functions are functions with infinite loops or those
2832 depending on volatile memory or other system resource, that may change between
2833 two consecutive calls (such as @code{feof} in a multithreading environment).
2835 The attribute @code{pure} is not implemented in GCC versions earlier
2839 @cindex @code{hot} function attribute
2840 The @code{hot} attribute is used to inform the compiler that a function is a
2841 hot spot of the compiled program. The function is optimized more aggressively
2842 and on many target it is placed into special subsection of the text section so
2843 all hot functions appears close together improving locality.
2845 When profile feedback is available, via @option{-fprofile-use}, hot functions
2846 are automatically detected and this attribute is ignored.
2848 The @code{hot} attribute is not implemented in GCC versions earlier
2852 @cindex @code{cold} function attribute
2853 The @code{cold} attribute is used to inform the compiler that a function is
2854 unlikely executed. The function is optimized for size rather than speed and on
2855 many targets it is placed into special subsection of the text section so all
2856 cold functions appears close together improving code locality of non-cold parts
2857 of program. The paths leading to call of cold functions within code are marked
2858 as unlikely by the branch prediction mechanism. It is thus useful to mark
2859 functions used to handle unlikely conditions, such as @code{perror}, as cold to
2860 improve optimization of hot functions that do call marked functions in rare
2863 When profile feedback is available, via @option{-fprofile-use}, hot functions
2864 are automatically detected and this attribute is ignored.
2866 The @code{cold} attribute is not implemented in GCC versions earlier than 4.3.
2868 @item regparm (@var{number})
2869 @cindex @code{regparm} attribute
2870 @cindex functions that are passed arguments in registers on the 386
2871 On the Intel 386, the @code{regparm} attribute causes the compiler to
2872 pass arguments number one to @var{number} if they are of integral type
2873 in registers EAX, EDX, and ECX instead of on the stack. Functions that
2874 take a variable number of arguments will continue to be passed all of their
2875 arguments on the stack.
2877 Beware that on some ELF systems this attribute is unsuitable for
2878 global functions in shared libraries with lazy binding (which is the
2879 default). Lazy binding will send the first call via resolving code in
2880 the loader, which might assume EAX, EDX and ECX can be clobbered, as
2881 per the standard calling conventions. Solaris 8 is affected by this.
2882 GNU systems with GLIBC 2.1 or higher, and FreeBSD, are believed to be
2883 safe since the loaders there save EAX, EDX and ECX. (Lazy binding can be
2884 disabled with the linker or the loader if desired, to avoid the
2888 @cindex @code{sseregparm} attribute
2889 On the Intel 386 with SSE support, the @code{sseregparm} attribute
2890 causes the compiler to pass up to 3 floating point arguments in
2891 SSE registers instead of on the stack. Functions that take a
2892 variable number of arguments will continue to pass all of their
2893 floating point arguments on the stack.
2895 @item force_align_arg_pointer
2896 @cindex @code{force_align_arg_pointer} attribute
2897 On the Intel x86, the @code{force_align_arg_pointer} attribute may be
2898 applied to individual function definitions, generating an alternate
2899 prologue and epilogue that realigns the runtime stack if necessary.
2900 This supports mixing legacy codes that run with a 4-byte aligned stack
2901 with modern codes that keep a 16-byte stack for SSE compatibility.
2904 @cindex @code{resbank} attribute
2905 On the SH2A target, this attribute enables the high-speed register
2906 saving and restoration using a register bank for @code{interrupt_handler}
2907 routines. Saving to the bank is performed automatically after the CPU
2908 accepts an interrupt that uses a register bank.
2910 The nineteen 32-bit registers comprising general register R0 to R14,
2911 control register GBR, and system registers MACH, MACL, and PR and the
2912 vector table address offset are saved into a register bank. Register
2913 banks are stacked in first-in last-out (FILO) sequence. Restoration
2914 from the bank is executed by issuing a RESBANK instruction.
2917 @cindex @code{returns_twice} attribute
2918 The @code{returns_twice} attribute tells the compiler that a function may
2919 return more than one time. The compiler will ensure that all registers
2920 are dead before calling such a function and will emit a warning about
2921 the variables that may be clobbered after the second return from the
2922 function. Examples of such functions are @code{setjmp} and @code{vfork}.
2923 The @code{longjmp}-like counterpart of such function, if any, might need
2924 to be marked with the @code{noreturn} attribute.
2927 @cindex save all registers on the Blackfin, H8/300, H8/300H, and H8S
2928 Use this attribute on the Blackfin, H8/300, H8/300H, and H8S to indicate that
2929 all registers except the stack pointer should be saved in the prologue
2930 regardless of whether they are used or not.
2932 @item section ("@var{section-name}")
2933 @cindex @code{section} function attribute
2934 Normally, the compiler places the code it generates in the @code{text} section.
2935 Sometimes, however, you need additional sections, or you need certain
2936 particular functions to appear in special sections. The @code{section}
2937 attribute specifies that a function lives in a particular section.
2938 For example, the declaration:
2941 extern void foobar (void) __attribute__ ((section ("bar")));
2945 puts the function @code{foobar} in the @code{bar} section.
2947 Some file formats do not support arbitrary sections so the @code{section}
2948 attribute is not available on all platforms.
2949 If you need to map the entire contents of a module to a particular
2950 section, consider using the facilities of the linker instead.
2953 @cindex @code{sentinel} function attribute
2954 This function attribute ensures that a parameter in a function call is
2955 an explicit @code{NULL}. The attribute is only valid on variadic
2956 functions. By default, the sentinel is located at position zero, the
2957 last parameter of the function call. If an optional integer position
2958 argument P is supplied to the attribute, the sentinel must be located at
2959 position P counting backwards from the end of the argument list.
2962 __attribute__ ((sentinel))
2964 __attribute__ ((sentinel(0)))
2967 The attribute is automatically set with a position of 0 for the built-in
2968 functions @code{execl} and @code{execlp}. The built-in function
2969 @code{execle} has the attribute set with a position of 1.
2971 A valid @code{NULL} in this context is defined as zero with any pointer
2972 type. If your system defines the @code{NULL} macro with an integer type
2973 then you need to add an explicit cast. GCC replaces @code{stddef.h}
2974 with a copy that redefines NULL appropriately.
2976 The warnings for missing or incorrect sentinels are enabled with
2980 See long_call/short_call.
2983 See longcall/shortcall.
2986 @cindex signal handler functions on the AVR processors
2987 Use this attribute on the AVR to indicate that the specified
2988 function is a signal handler. The compiler will generate function
2989 entry and exit sequences suitable for use in a signal handler when this
2990 attribute is present. Interrupts will be disabled inside the function.
2993 Use this attribute on the SH to indicate an @code{interrupt_handler}
2994 function should switch to an alternate stack. It expects a string
2995 argument that names a global variable holding the address of the
3000 void f () __attribute__ ((interrupt_handler,
3001 sp_switch ("alt_stack")));
3005 @cindex functions that pop the argument stack on the 386
3006 On the Intel 386, the @code{stdcall} attribute causes the compiler to
3007 assume that the called function will pop off the stack space used to
3008 pass arguments, unless it takes a variable number of arguments.
3010 @item syscall_linkage
3011 @cindex @code{syscall_linkage} attribute
3012 This attribute is used to modify the IA64 calling convention by marking
3013 all input registers as live at all function exits. This makes it possible
3014 to restart a system call after an interrupt without having to save/restore
3015 the input registers. This also prevents kernel data from leaking into
3019 @cindex @code{target} function attribute
3020 The @code{target} attribute is used to specify that a function is to
3021 be compiled with different target options than specified on the
3022 command line. This can be used for instance to have functions
3023 compiled with a different ISA (instruction set architecture) than the
3024 default. You can also use the @samp{#pragma GCC target} pragma to set
3025 more than one function to be compiled with specific target options.
3026 @xref{Function Specific Option Pragmas}, for details about the
3027 @samp{#pragma GCC target} pragma.
3029 For instance on a 386, you could compile one function with
3030 @code{target("sse4.1,arch=core2")} and another with
3031 @code{target("sse4a,arch=amdfam10")} that would be equivalent to
3032 compiling the first function with @option{-msse4.1} and
3033 @option{-march=core2} options, and the second function with
3034 @option{-msse4a} and @option{-march=amdfam10} options. It is up to the
3035 user to make sure that a function is only invoked on a machine that
3036 supports the particular ISA it was compiled for (for example by using
3037 @code{cpuid} on 386 to determine what feature bits and architecture
3041 int core2_func (void) __attribute__ ((__target__ ("arch=core2")));
3042 int sse3_func (void) __attribute__ ((__target__ ("sse3")));
3045 On the 386, the following options are allowed:
3050 @cindex @code{target("abm")} attribute
3051 Enable/disable the generation of the advanced bit instructions.
3055 @cindex @code{target("aes")} attribute
3056 Enable/disable the generation of the AES instructions.
3060 @cindex @code{target("mmx")} attribute
3061 Enable/disable the generation of the MMX instructions.
3065 @cindex @code{target("pclmul")} attribute
3066 Enable/disable the generation of the PCLMUL instructions.
3070 @cindex @code{target("popcnt")} attribute
3071 Enable/disable the generation of the POPCNT instruction.
3075 @cindex @code{target("sse")} attribute
3076 Enable/disable the generation of the SSE instructions.
3080 @cindex @code{target("sse2")} attribute
3081 Enable/disable the generation of the SSE2 instructions.
3085 @cindex @code{target("sse3")} attribute
3086 Enable/disable the generation of the SSE3 instructions.
3090 @cindex @code{target("sse4")} attribute
3091 Enable/disable the generation of the SSE4 instructions (both SSE4.1
3096 @cindex @code{target("sse4.1")} attribute
3097 Enable/disable the generation of the sse4.1 instructions.
3101 @cindex @code{target("sse4.2")} attribute
3102 Enable/disable the generation of the sse4.2 instructions.
3106 @cindex @code{target("sse4a")} attribute
3107 Enable/disable the generation of the SSE4A instructions.
3111 @cindex @code{target("sse5")} attribute
3112 Enable/disable the generation of the SSE5 instructions.
3116 @cindex @code{target("ssse3")} attribute
3117 Enable/disable the generation of the SSSE3 instructions.
3121 @cindex @code{target("cld")} attribute
3122 Enable/disable the generation of the CLD before string moves.
3124 @item fancy-math-387
3125 @itemx no-fancy-math-387
3126 @cindex @code{target("fancy-math-387")} attribute
3127 Enable/disable the generation of the @code{sin}, @code{cos}, and
3128 @code{sqrt} instructions on the 387 floating point unit.
3131 @itemx no-fused-madd
3132 @cindex @code{target("fused-madd")} attribute
3133 Enable/disable the generation of the fused multiply/add instructions.
3137 @cindex @code{target("ieee-fp")} attribute
3138 Enable/disable the generation of floating point that depends on IEEE arithmetic.
3140 @item inline-all-stringops
3141 @itemx no-inline-all-stringops
3142 @cindex @code{target("inline-all-stringops")} attribute
3143 Enable/disable inlining of string operations.
3145 @item inline-stringops-dynamically
3146 @itemx no-inline-stringops-dynamically
3147 @cindex @code{target("inline-stringops-dynamically")} attribute
3148 Enable/disable the generation of the inline code to do small string
3149 operations and calling the library routines for large operations.
3151 @item align-stringops
3152 @itemx no-align-stringops
3153 @cindex @code{target("align-stringops")} attribute
3154 Do/do not align destination of inlined string operations.
3158 @cindex @code{target("recip")} attribute
3159 Enable/disable the generation of RCPSS, RCPPS, RSQRTSS and RSQRTPS
3160 instructions followed an additional Newton-Raphson step instead of
3161 doing a floating point division.
3163 @item arch=@var{ARCH}
3164 @cindex @code{target("arch=@var{ARCH}")} attribute
3165 Specify the architecture to generate code for in compiling the function.
3167 @item tune=@var{TUNE}
3168 @cindex @code{target("tune=@var{TUNE}")} attribute
3169 Specify the architecture to tune for in compiling the function.
3171 @item fpmath=@var{FPMATH}
3172 @cindex @code{target("fpmath=@var{FPMATH}")} attribute
3173 Specify which floating point unit to use. The
3174 @code{target("fpmath=sse,387")} option must be specified as
3175 @code{target("fpmath=sse+387")} because the comma would separate
3179 On the 386, you can use either multiple strings to specify multiple
3180 options, or you can separate the option with a comma (@code{,}).
3182 On the 386, the inliner will not inline a function that has different
3183 target options than the caller, unless the callee has a subset of the
3184 target options of the caller. For example a function declared with
3185 @code{target("sse5")} can inline a function with
3186 @code{target("sse2")}, since @code{-msse5} implies @code{-msse2}.
3188 The @code{target} attribute is not implemented in GCC versions earlier
3189 than 4.4, and at present only the 386 uses it.
3192 @cindex tiny data section on the H8/300H and H8S
3193 Use this attribute on the H8/300H and H8S to indicate that the specified
3194 variable should be placed into the tiny data section.
3195 The compiler will generate more efficient code for loads and stores
3196 on data in the tiny data section. Note the tiny data area is limited to
3197 slightly under 32kbytes of data.
3200 Use this attribute on the SH for an @code{interrupt_handler} to return using
3201 @code{trapa} instead of @code{rte}. This attribute expects an integer
3202 argument specifying the trap number to be used.
3205 @cindex @code{unused} attribute.
3206 This attribute, attached to a function, means that the function is meant
3207 to be possibly unused. GCC will not produce a warning for this
3211 @cindex @code{used} attribute.
3212 This attribute, attached to a function, means that code must be emitted
3213 for the function even if it appears that the function is not referenced.
3214 This is useful, for example, when the function is referenced only in
3218 @cindex @code{version_id} attribute
3219 This IA64 HP-UX attribute, attached to a global variable or function, renames a
3220 symbol to contain a version string, thus allowing for function level
3221 versioning. HP-UX system header files may use version level functioning
3222 for some system calls.
3225 extern int foo () __attribute__((version_id ("20040821")));
3228 Calls to @var{foo} will be mapped to calls to @var{foo@{20040821@}}.
3230 @item visibility ("@var{visibility_type}")
3231 @cindex @code{visibility} attribute
3232 This attribute affects the linkage of the declaration to which it is attached.
3233 There are four supported @var{visibility_type} values: default,
3234 hidden, protected or internal visibility.
3237 void __attribute__ ((visibility ("protected")))
3238 f () @{ /* @r{Do something.} */; @}
3239 int i __attribute__ ((visibility ("hidden")));
3242 The possible values of @var{visibility_type} correspond to the
3243 visibility settings in the ELF gABI.
3246 @c keep this list of visibilities in alphabetical order.
3249 Default visibility is the normal case for the object file format.
3250 This value is available for the visibility attribute to override other
3251 options that may change the assumed visibility of entities.
3253 On ELF, default visibility means that the declaration is visible to other
3254 modules and, in shared libraries, means that the declared entity may be
3257 On Darwin, default visibility means that the declaration is visible to
3260 Default visibility corresponds to ``external linkage'' in the language.
3263 Hidden visibility indicates that the entity declared will have a new
3264 form of linkage, which we'll call ``hidden linkage''. Two
3265 declarations of an object with hidden linkage refer to the same object
3266 if they are in the same shared object.
3269 Internal visibility is like hidden visibility, but with additional
3270 processor specific semantics. Unless otherwise specified by the
3271 psABI, GCC defines internal visibility to mean that a function is
3272 @emph{never} called from another module. Compare this with hidden
3273 functions which, while they cannot be referenced directly by other
3274 modules, can be referenced indirectly via function pointers. By
3275 indicating that a function cannot be called from outside the module,
3276 GCC may for instance omit the load of a PIC register since it is known
3277 that the calling function loaded the correct value.
3280 Protected visibility is like default visibility except that it
3281 indicates that references within the defining module will bind to the
3282 definition in that module. That is, the declared entity cannot be
3283 overridden by another module.
3287 All visibilities are supported on many, but not all, ELF targets
3288 (supported when the assembler supports the @samp{.visibility}
3289 pseudo-op). Default visibility is supported everywhere. Hidden
3290 visibility is supported on Darwin targets.
3292 The visibility attribute should be applied only to declarations which
3293 would otherwise have external linkage. The attribute should be applied
3294 consistently, so that the same entity should not be declared with
3295 different settings of the attribute.
3297 In C++, the visibility attribute applies to types as well as functions
3298 and objects, because in C++ types have linkage. A class must not have
3299 greater visibility than its non-static data member types and bases,
3300 and class members default to the visibility of their class. Also, a
3301 declaration without explicit visibility is limited to the visibility
3304 In C++, you can mark member functions and static member variables of a
3305 class with the visibility attribute. This is useful if you know a
3306 particular method or static member variable should only be used from
3307 one shared object; then you can mark it hidden while the rest of the
3308 class has default visibility. Care must be taken to avoid breaking
3309 the One Definition Rule; for example, it is usually not useful to mark
3310 an inline method as hidden without marking the whole class as hidden.
3312 A C++ namespace declaration can also have the visibility attribute.
3313 This attribute applies only to the particular namespace body, not to
3314 other definitions of the same namespace; it is equivalent to using
3315 @samp{#pragma GCC visibility} before and after the namespace
3316 definition (@pxref{Visibility Pragmas}).
3318 In C++, if a template argument has limited visibility, this
3319 restriction is implicitly propagated to the template instantiation.
3320 Otherwise, template instantiations and specializations default to the
3321 visibility of their template.
3323 If both the template and enclosing class have explicit visibility, the
3324 visibility from the template is used.
3326 @item warn_unused_result
3327 @cindex @code{warn_unused_result} attribute
3328 The @code{warn_unused_result} attribute causes a warning to be emitted
3329 if a caller of the function with this attribute does not use its
3330 return value. This is useful for functions where not checking
3331 the result is either a security problem or always a bug, such as
3335 int fn () __attribute__ ((warn_unused_result));
3338 if (fn () < 0) return -1;
3344 results in warning on line 5.
3347 @cindex @code{weak} attribute
3348 The @code{weak} attribute causes the declaration to be emitted as a weak
3349 symbol rather than a global. This is primarily useful in defining
3350 library functions which can be overridden in user code, though it can
3351 also be used with non-function declarations. Weak symbols are supported
3352 for ELF targets, and also for a.out targets when using the GNU assembler
3356 @itemx weakref ("@var{target}")
3357 @cindex @code{weakref} attribute
3358 The @code{weakref} attribute marks a declaration as a weak reference.
3359 Without arguments, it should be accompanied by an @code{alias} attribute
3360 naming the target symbol. Optionally, the @var{target} may be given as
3361 an argument to @code{weakref} itself. In either case, @code{weakref}
3362 implicitly marks the declaration as @code{weak}. Without a
3363 @var{target}, given as an argument to @code{weakref} or to @code{alias},
3364 @code{weakref} is equivalent to @code{weak}.
3367 static int x() __attribute__ ((weakref ("y")));
3368 /* is equivalent to... */
3369 static int x() __attribute__ ((weak, weakref, alias ("y")));
3371 static int x() __attribute__ ((weakref));
3372 static int x() __attribute__ ((alias ("y")));
3375 A weak reference is an alias that does not by itself require a
3376 definition to be given for the target symbol. If the target symbol is
3377 only referenced through weak references, then the becomes a @code{weak}
3378 undefined symbol. If it is directly referenced, however, then such
3379 strong references prevail, and a definition will be required for the
3380 symbol, not necessarily in the same translation unit.
3382 The effect is equivalent to moving all references to the alias to a
3383 separate translation unit, renaming the alias to the aliased symbol,
3384 declaring it as weak, compiling the two separate translation units and
3385 performing a reloadable link on them.
3387 At present, a declaration to which @code{weakref} is attached can
3388 only be @code{static}.
3392 You can specify multiple attributes in a declaration by separating them
3393 by commas within the double parentheses or by immediately following an
3394 attribute declaration with another attribute declaration.
3396 @cindex @code{#pragma}, reason for not using
3397 @cindex pragma, reason for not using
3398 Some people object to the @code{__attribute__} feature, suggesting that
3399 ISO C's @code{#pragma} should be used instead. At the time
3400 @code{__attribute__} was designed, there were two reasons for not doing
3405 It is impossible to generate @code{#pragma} commands from a macro.
3408 There is no telling what the same @code{#pragma} might mean in another
3412 These two reasons applied to almost any application that might have been
3413 proposed for @code{#pragma}. It was basically a mistake to use
3414 @code{#pragma} for @emph{anything}.
3416 The ISO C99 standard includes @code{_Pragma}, which now allows pragmas
3417 to be generated from macros. In addition, a @code{#pragma GCC}
3418 namespace is now in use for GCC-specific pragmas. However, it has been
3419 found convenient to use @code{__attribute__} to achieve a natural
3420 attachment of attributes to their corresponding declarations, whereas
3421 @code{#pragma GCC} is of use for constructs that do not naturally form
3422 part of the grammar. @xref{Other Directives,,Miscellaneous
3423 Preprocessing Directives, cpp, The GNU C Preprocessor}.
3425 @node Attribute Syntax
3426 @section Attribute Syntax
3427 @cindex attribute syntax
3429 This section describes the syntax with which @code{__attribute__} may be
3430 used, and the constructs to which attribute specifiers bind, for the C
3431 language. Some details may vary for C++ and Objective-C@. Because of
3432 infelicities in the grammar for attributes, some forms described here
3433 may not be successfully parsed in all cases.
3435 There are some problems with the semantics of attributes in C++. For
3436 example, there are no manglings for attributes, although they may affect
3437 code generation, so problems may arise when attributed types are used in
3438 conjunction with templates or overloading. Similarly, @code{typeid}
3439 does not distinguish between types with different attributes. Support
3440 for attributes in C++ may be restricted in future to attributes on
3441 declarations only, but not on nested declarators.
3443 @xref{Function Attributes}, for details of the semantics of attributes
3444 applying to functions. @xref{Variable Attributes}, for details of the
3445 semantics of attributes applying to variables. @xref{Type Attributes},
3446 for details of the semantics of attributes applying to structure, union
3447 and enumerated types.
3449 An @dfn{attribute specifier} is of the form
3450 @code{__attribute__ ((@var{attribute-list}))}. An @dfn{attribute list}
3451 is a possibly empty comma-separated sequence of @dfn{attributes}, where
3452 each attribute is one of the following:
3456 Empty. Empty attributes are ignored.
3459 A word (which may be an identifier such as @code{unused}, or a reserved
3460 word such as @code{const}).
3463 A word, followed by, in parentheses, parameters for the attribute.
3464 These parameters take one of the following forms:
3468 An identifier. For example, @code{mode} attributes use this form.
3471 An identifier followed by a comma and a non-empty comma-separated list
3472 of expressions. For example, @code{format} attributes use this form.
3475 A possibly empty comma-separated list of expressions. For example,
3476 @code{format_arg} attributes use this form with the list being a single
3477 integer constant expression, and @code{alias} attributes use this form
3478 with the list being a single string constant.
3482 An @dfn{attribute specifier list} is a sequence of one or more attribute
3483 specifiers, not separated by any other tokens.
3485 In GNU C, an attribute specifier list may appear after the colon following a
3486 label, other than a @code{case} or @code{default} label. The only
3487 attribute it makes sense to use after a label is @code{unused}. This
3488 feature is intended for code generated by programs which contains labels
3489 that may be unused but which is compiled with @option{-Wall}. It would
3490 not normally be appropriate to use in it human-written code, though it
3491 could be useful in cases where the code that jumps to the label is
3492 contained within an @code{#ifdef} conditional. GNU C++ only permits
3493 attributes on labels if the attribute specifier is immediately
3494 followed by a semicolon (i.e., the label applies to an empty
3495 statement). If the semicolon is missing, C++ label attributes are
3496 ambiguous, as it is permissible for a declaration, which could begin
3497 with an attribute list, to be labelled in C++. Declarations cannot be
3498 labelled in C90 or C99, so the ambiguity does not arise there.
3500 An attribute specifier list may appear as part of a @code{struct},
3501 @code{union} or @code{enum} specifier. It may go either immediately
3502 after the @code{struct}, @code{union} or @code{enum} keyword, or after
3503 the closing brace. The former syntax is preferred.
3504 Where attribute specifiers follow the closing brace, they are considered
3505 to relate to the structure, union or enumerated type defined, not to any
3506 enclosing declaration the type specifier appears in, and the type
3507 defined is not complete until after the attribute specifiers.
3508 @c Otherwise, there would be the following problems: a shift/reduce
3509 @c conflict between attributes binding the struct/union/enum and
3510 @c binding to the list of specifiers/qualifiers; and "aligned"
3511 @c attributes could use sizeof for the structure, but the size could be
3512 @c changed later by "packed" attributes.
3514 Otherwise, an attribute specifier appears as part of a declaration,
3515 counting declarations of unnamed parameters and type names, and relates
3516 to that declaration (which may be nested in another declaration, for
3517 example in the case of a parameter declaration), or to a particular declarator
3518 within a declaration. Where an
3519 attribute specifier is applied to a parameter declared as a function or
3520 an array, it should apply to the function or array rather than the
3521 pointer to which the parameter is implicitly converted, but this is not
3522 yet correctly implemented.
3524 Any list of specifiers and qualifiers at the start of a declaration may
3525 contain attribute specifiers, whether or not such a list may in that
3526 context contain storage class specifiers. (Some attributes, however,
3527 are essentially in the nature of storage class specifiers, and only make
3528 sense where storage class specifiers may be used; for example,
3529 @code{section}.) There is one necessary limitation to this syntax: the
3530 first old-style parameter declaration in a function definition cannot
3531 begin with an attribute specifier, because such an attribute applies to
3532 the function instead by syntax described below (which, however, is not
3533 yet implemented in this case). In some other cases, attribute
3534 specifiers are permitted by this grammar but not yet supported by the
3535 compiler. All attribute specifiers in this place relate to the
3536 declaration as a whole. In the obsolescent usage where a type of
3537 @code{int} is implied by the absence of type specifiers, such a list of
3538 specifiers and qualifiers may be an attribute specifier list with no
3539 other specifiers or qualifiers.
3541 At present, the first parameter in a function prototype must have some
3542 type specifier which is not an attribute specifier; this resolves an
3543 ambiguity in the interpretation of @code{void f(int
3544 (__attribute__((foo)) x))}, but is subject to change. At present, if
3545 the parentheses of a function declarator contain only attributes then
3546 those attributes are ignored, rather than yielding an error or warning
3547 or implying a single parameter of type int, but this is subject to
3550 An attribute specifier list may appear immediately before a declarator
3551 (other than the first) in a comma-separated list of declarators in a
3552 declaration of more than one identifier using a single list of
3553 specifiers and qualifiers. Such attribute specifiers apply
3554 only to the identifier before whose declarator they appear. For
3558 __attribute__((noreturn)) void d0 (void),
3559 __attribute__((format(printf, 1, 2))) d1 (const char *, ...),
3564 the @code{noreturn} attribute applies to all the functions
3565 declared; the @code{format} attribute only applies to @code{d1}.
3567 An attribute specifier list may appear immediately before the comma,
3568 @code{=} or semicolon terminating the declaration of an identifier other
3569 than a function definition. Such attribute specifiers apply
3570 to the declared object or function. Where an
3571 assembler name for an object or function is specified (@pxref{Asm
3572 Labels}), the attribute must follow the @code{asm}
3575 An attribute specifier list may, in future, be permitted to appear after
3576 the declarator in a function definition (before any old-style parameter
3577 declarations or the function body).
3579 Attribute specifiers may be mixed with type qualifiers appearing inside
3580 the @code{[]} of a parameter array declarator, in the C99 construct by
3581 which such qualifiers are applied to the pointer to which the array is
3582 implicitly converted. Such attribute specifiers apply to the pointer,
3583 not to the array, but at present this is not implemented and they are