liveBook · Manning

Attribute

As explained in Modern C, Third Edition

An attribute is supplemental implementation or semantic information attached to a C program construct.

An attribute adds supplemental information to a C construct. It appears in double square brackets, as in [[maybe_unused]]. Attributes annotate the program’s structure; their particular meaning depends on the attribute. section 2.1

A promise in a function interface

A header can mark a function [[__unsequenced__]]. When application code sees only that header, the compiler can still use the stated property. If the function has no pointer parameters or pointer return, its return value depends only on its argument values. With pointer parameters or returns, possible interference is limited to objects visible through those pointers. section 10.2.3

C23 provides standard attributes for several purposes:

  • [[deprecated]] marks a feature as not meant for direct use; using it usually produces a diagnostic. It can take a string explaining why. section 10.2.3
  • [[nodiscard]] suggests a warning when a return value is unused. section 10.3
  • The standard set also includes fallthrough, maybe_unused, noreturn, reproducible, and unsequenced. Double-underscore variants, such as __unsequenced__, are provided for safer use with macros. section 10.2.3
#if __has_c_attribute(__gnu__::__format__)
  [[__gnu__::__format__(__printf__, 3, 4)]]
#endif
  int snprintf(char *buf, size_t size, const char *frmt, ...);

This declaration places a compiler-specific attribute before a function prototype only when __has_c_attribute reports that the attribute is available. section 10.2.3

 #ifndef __has_c_attribute
 # define __has_c_attribute(X) 0
 #endif

The fallback definition makes the availability check evaluate to zero when __has_c_attribute itself is unavailable. section 10.2.3

TAKEAWAY: Put useful annotations on interfaces as well as implementations. They can improve diagnostics, safety, security, and performance, but check availability before relying on a compiler-specific attribute. section 10.3 section 10.2.3

How Modern C, Third Edition covers it

How deep it goes
Defines the bracket syntax and explains selected C23 attributes, including their effect on diagnostics and compiler assumptions.
Examples
Uses [[maybe_unused]], [[deprecated]] with a message, [[__unsequenced__]] on functions, and a conditionally annotated snprintf declaration.
What it assumes
Assumes familiarity with C declarations, headers, function calls, and pointers while introducing attribute syntax directly.
What you can do after
Apply it via C23 attribute syntax and guard a compiler-specific annotation with __has_c_attribute.
How it connects
Connects attributes to function declarations, header files, compiler diagnostics, return values, and pointer parameters.

Also explained in

Compare how 26 books explain attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.