liveBook · Manning

Naming standard

As explained in Modern C, Third Edition

A naming convention is a systematic rule for choosing identifiers so that related entities are recognizable and less likely to collide.

A naming standard is a documented rule for choosing identifiers so related entities remain recognizable and collisions become less likely. Naming variables, functions, and types also affects code accessibility; accepted project or domain terminology may include non-Latin characters. section 9.4

Protecting macro-local names

A type-generic macro uses names such as swap_p1, swap_p2, and swap_tmp for its auxiliary variables. The swap-prefixed convention lowers the chance of clashing with surrounding user code, while the documented convention makes the protection intentional rather than accidental. section 18.3.1

 #define SWAP(X, Y)                                              \
   do {                                                          \
     /* These two variables have the role of function            \
        parameters. They ensure to evaluate the                  \
        expression X and Y once, since these could be            \
        complicated lvalue expressions with evaluated            \
        subexpressions that have side effects. */                \
     auto const swap_p1 = &(X);                                  \
     auto const swap_p2 = &(Y);                                  \
     static_assert_compatible(*swap_p1, *swap_p2,                \
                              "to exchange values, '"           \
                              #X "' and '" #Y                   \
                              "' must have compatible types");  \
     auto swap_tmp = *swap_p1;                                   \
     *swap_p1 = *swap_p2;                                        \
     *swap_p2 = swap_tmp;                                        \
   } while (false)

Inspect the complete swap macro to see the naming convention applied alongside single evaluation and compatible-type checks. section 18.3.1

A naming standard reduces risk but cannot guarantee uniqueness: an outer name may still match a generated local name, and macro expansion then uses the newly defined local variable. section 18.3.1

How Modern C, Third Edition covers it

How deep it goes
Defines documented naming as a safety practice, demonstrates swap-prefixed locals, and stops short of guaranteeing collision-free macro expansion.
Examples
Uses swap_p1, swap_p2, and swap_tmp as protected macro-local names and r, g, and b as type-generic member names.
What it assumes
Assumes familiarity with C identifiers, macros, and surrounding lexical names.
What you can do after
Apply it by documenting distinctive local-variable names inside macros and evaluating remaining collision risks.
How it connects
Connects naming standards to coding style, identifiers, macros, macro expansion, and type-generic programming.

Also explained in

Compare how 8 books explain naming standard →

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.