Modern C (C11/C17/C23)

C23 attributes ([ [deprecated]], [ [fallthrough]], [ [nodiscard]])

/ AT-rib-yoots /

Sometimes you want to attach a note to a function, variable, or statement that the COMPILER understands — 'this is obsolete, warn if used', 'ignoring this return value is probably a bug', 'yes, this case really does fall through on purpose'. C23 adopts a standard syntax for such notes, borrowed from C++: an attribute written inside double brackets, placed next to the thing it applies to.

Three common ones: a deprecated attribute on a function makes the compiler warn (optionally with a message) whenever that function is used, easing migration away from old APIs. A nodiscard attribute on a function makes the compiler warn if the caller ignores the returned value, which is valuable for functions whose result must be checked, like an allocation or an error code. A fallthrough attribute is a statement you place in a switch where one case is meant to fall into the next, telling the compiler the missing break is intentional so it suppresses the fall-through warning. Two more, maybe_unused and noreturn, round out the standard set. Because attributes are designed to be ignorable, a compiler that does not understand one may skip it without error.

They matter for code health: catching ignored error codes, guiding users off deprecated functions, and documenting deliberate switch fall-through so reviewers and tools agree it is on purpose. The caveat: an attribute generally produces a WARNING, not a hard error, so its benefit depends on you compiling with warnings enabled (for example gcc -Wall) and actually reading them.

[ [deprecated("use new_open")]] int old_open(const char *p); [ [nodiscard]] int parse(const char *s); // warn if result ignored switch (c) { case 'a': handle_a(); [ [fallthrough]]; // the missing break is intentional case 'b': handle_b(); break; }

Standard C23 attributes let the compiler warn on deprecated use, on ignored return values, and approve a deliberate switch fall-through.

Attributes usually emit a warning, not a hard error, and an unknown one may be silently ignored — so they only help if you compile with warnings on and read them.

Also called
standard attributesdouble-bracket attributes標準屬性