Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Keywords and Statements

C attributes: [[nodiscard]], [[maybe_unused]] and friends

Reference for C23 standard attributes: [[nodiscard]], [[maybe_unused]], [[deprecated]], [[fallthrough]], [[noreturn]] and [[unsequenced]], with examples.

An attribute is a piece of standard metadata you attach to a declaration, statement or type with double square brackets: [[name]]. Attributes do not change what a correct program computes. They tell the compiler and the reader something extra: "do not ignore this result", "this variable is unused on purpose", "this function is obsolete".

C23 brought the [[...]] syntax, borrowed from C++, into the language. Before that, you had to use compiler extensions such as __attribute__((unused)) in GCC and Clang, or __declspec in MSVC. A compiler must ignore attributes it does not recognize, so adding them is safe for older tools that accept the syntax.

C
[[nodiscard]] int parse(const char *text);
[[maybe_unused]] static int debug_level;

Try it

C nodiscard and maybe_unused

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C fallthrough and deprecated

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
[[attribute]]
[[attribute(argument)]]
[[vendor::attribute]]
[[attr1, attr2]]

An attribute list may appear at the start of a declaration, a statement or a block item, or after the name it applies to. The standard attributes in C23 are:

Attribute Applies to Meaning
[[nodiscard]] or [[nodiscard("reason")]] Functions, structs, unions, enums Warn when the result is ignored.
[[maybe_unused]] Variables, parameters, functions, types, labels Suppress "unused" warnings.
[[deprecated]] or [[deprecated("reason")]] Functions, variables, types, enumerators Warn when the name is used.
[[fallthrough]] Null statement before a case label A switch case intentionally runs into the next one.
[[noreturn]] Function declarations The function never returns (replaces _Noreturn).
[[unsequenced]], [[reproducible]] Function types Promise about side effects, allowing optimization.

Implementations may add their own attributes with a prefix, for example [[gnu::always_inline]]. Unknown attributes are ignored, usually with a warning.

Errors / undefined behavior: a misplaced standard attribute, or one with the wrong argument, makes the program ill-formed (a diagnostic is required). A function marked [[noreturn]] that does return is undefined behavior.

Usage notes

  • [[nodiscard]] is a warning, not an error. GCC reports it with -Wunused-result. You can silence it on purpose with a cast: (void) checked_divide(...).
  • On a type, [[nodiscard]] applies to every function that returns it. That is handy for error-code structs and enums.
  • [[maybe_unused]] is for code that is conditionally used, such as a variable only read inside assert (which disappears with NDEBUG) or a callback parameter you must accept but ignore.
  • [[fallthrough]] must be followed by a case or default label. It documents intent, and compilers using -Wimplicit-fallthrough (part of -Wextra in GCC) stay quiet.
  • [[deprecated]] takes an optional message string. Put the replacement in the message.
  • [[noreturn]] replaces _Noreturn and the <stdnoreturn.h> macro; both are obsolescent in C23. See noreturn.
  • __has_c_attribute(name) in #if tells you whether an attribute is supported; see feature tests.
  • Not everywhere in older modes. With -std=c17 or earlier, GCC still accepts [[...]] as an extension in recent versions, but portable pre-C23 code should use __attribute__ behind a macro.
  • Added in C23. [[nodiscard]], [[maybe_unused]], [[deprecated]] and [[fallthrough]] all arrived together with the syntax.