liveBook · Manning

Naming convention

As explained in Modern C

A naming convention is an established pattern for representing words or semantic information in identifiers.

A naming standard gives identifiers a consistent pattern and separates two concerns: C's technical restrictions and the semantic conventions that make code readable. Apply one policy to every type, member, variable, enumeration, macro, and function. section 9.2

Use a naming standard to avoid collisions and clarify intent:

  • Keep every identifier visible in a header file conforming to C's restrictions. section 9.2
  • Treat reserved forms carefully: double underscores, an underscore followed by a capital letter, file-scope underscore names, predefined names, library names, and names ending in _t can conflict with the implementation. section 9.2
  • Expose only interface types and functions, limiting pollution of the global identifier space. section 9.2
  • Prefer names that remain recognizable, distinguishable, and quickly readable rather than merely legal. section 9.2 section 9.3

Reducing collisions in a function interface

Consider the declaration `double memory_sum(size_t N, size_t I, double strip[N][I]);`. `N` can invite a macro, `I` can conflict with the complex-number interface, `strip` can resemble an implementation name, and `memory_sum` could later acquire a standard meaning. A collision-resistant policy changes it to `double p99_memory_sum(size_t p00_n, size_t p00_i, double p00_strip[p00_n][p00_i]);`. The prefix limits exposure, while the longer names make the parameters distinguishable. section 9.2

1   double memory_sum(size_t N, size_t I, double strip[N][I]);
N is a capitalized identifier, and thus your collaborator could be tempted to define a macro N as a big number.
I is used for the root of – 1 as soon as someone includes complex.h.
The identifier strip might be used by a C implementation for a library function or macro.
The identifier memory_sum might be used by the C standard for a type name in the future.


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":2,\"ch\":28},{\"line\":2,\"ch\":29}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":0,\"ch\":22},{\"line\":0,\"ch\":28}],[{\"line\":0,\"ch\":22},{\"line\":0,\"ch\":28}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}]]"}
!@%STYLE%@!

Compare the risky declaration and its collision-resistant revision directly, including the identifiers that may conflict with macros, library names, or future standard names. section 9.2

Good names support literal implementation and obvious control flow: the structure of the C program should communicate what it does, without relying on excessive comments or obscure naming. section 10.2

How Modern C covers it

How deep it goes
Explains technical restrictions, reserved forms, namespace exposure, and distinguishability, but does not specify one universal semantic style.
Examples
Uses a colliding array declaration, a prefixed revision, and pairs such as myLineNumber versus myLimeNumber.
What it assumes
Assumes basic C identifiers and interfaces while teaching reserved-name risks and naming discipline explicitly.
What you can do after
Apply it via a consistent naming policy that limits global exposure and makes identifiers conforming and distinguishable.
How it connects
Connects identifiers with macros, header files, namespaces, APIs, library functions, and code readability.

Also explained in

Compare how 10 books explain naming convention →

Related concepts

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