Saturday, February 19, 2022

[SOLVED] Does __attribute__ apply to all the variables in a declaration?

Issue

Does the __attribute__ directive apply to all the members declared on one line?


int a, b, c;

Declares three int variables.


int *a, b, c;

Declares variable "a" as a pointer to int, and b and c as int.


int __attribute__((used)) a, b, c;

Does the used attribute apply to all variables or only to a?


Solution

From GCC: Attribute-Syntax:

An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers. Such attribute specifiers apply only to the identifier before whose declarator they appear. For example, in

__attribute__((noreturn)) void d0 (void),
     __attribute__((format(printf, 1, 2))) d1 (const char *, ...),
      d2 (void);

the noreturn attribute applies to all the functions declared; the format attribute only applies to d1.


Correction: As the comment points out, my previous conclusion is incorrect. I didn't notice the other than the first part.

Modified conclusion:

In both

int __attribute__((used)) a, b, c;

and

__attribute__((used)) int a, b, c;

The attribute applies to all a, b, and c.

But if it were:

int a, __attribute__((used)) b, c;

The attribute would apply to b only.



Answered By - Yu Hao
Answer Checked By - Timothy Miller (WPSolving Admin)