IBM extension

Variable attributes

Variable attributes are language extensions provided to facilitate the compilation of programs developed with the GNU C/C++ compilers. These language features allow you to use named attributes to specify special properties of data objects. Variable attributes apply to the declarations of simple variables, aggregates, and member variables of aggregates.

A variable attribute is specified with the keyword __attribute__ followed by the attribute name and any additional arguments the attribute name requires. A variable __attribute__ specification is included in the declaration of a variable, and can be placed before or after the declarator. Although there are variations, the syntax generally takes either of the following forms:

Read syntax diagramSkip visual syntax diagramVariable attribute syntax: post-declarator
 
>>-declarator--__attribute__------------------------------------>
 
       .-,----------------------.
       V                        |
>--((----+-attribute name-----+-+--))--------------------------><
         '-__attribute name__-'
 

Read syntax diagramSkip visual syntax diagramVariable attribute syntax: pre-declarator
 
>>-type specifier--__attribute__-------------------------------->
 
       .-,----------------------.
       V                        |
>--((----+-attribute name-----+-+--))--declarator--------------->
         '-__attribute name__-'
 
>--+-------------+---------------------------------------------><
   '-initializer-'
 

The attribute name can be specified with or without leading and trailing double underscore characters; however, using the double underscore reduces the likelihood of a name conflict with a macro of the same name. For unsupported attribute names, the XL C/C++ compiler issues diagnostics and ignores the attribute specification. Multiple attribute names can be specified in the same attribute specification.

In a comma-separated list of declarators on a single declaration line, if a variable attribute appears before all the declarators, it applies to all declarators in the declaration. If the attribute appears after a declarator, it only applies to the immediately preceding declarator. For example:

struct A  {

	int b __attribute__((aligned));          /* typical placement of variable attribute */

	int __attribute__((aligned))__ c = 10;   /* variable attribute can also be placed here */

	int d, e, f __attribute__((aligned));    /*  attribute applies to f only  */

	int g __attribute__((aligned)), h, i;    /*  attribute applies to g only  */

	int __attribute__((aligned)) j, k, l;    /*  attribute applies to j, k, and l  */ 
 
 };

The following variable attributes are supported:

Related information