liveBook · Manning

Namespace

As explained in Modern C

An identifier namespace is a C naming domain in which identifiers are associated with distinct kinds of entities and may have separate collision rules.

In C, a namespace is a naming domain in which identifiers for different kinds of entities can follow separate collision rules. The language has tangled namespaces covering types, structure and union members, variables, enumerations, macros, functions, and function-like macros. section 9.2

A seemingly safe declaration

double memory_sum(size_t N, size_t I, double strip[N][I]); looks local and readable, but N may collide with a macro, I with a name from complex.h, strip with an implementation library name, and memory_sum with a future standard type name. section 9.2

TAKEAWAY: Keep the probability of naming conflicts low by exposing as few names as possible. Interfaces should expose only types and functions that belong to the application programming interface. section 9.2

How Modern C covers it

How deep it goes
Defines several C identifier categories and collision risks, but stops short of detailing each namespace's formal lookup rules.
Examples
Uses a memory_sum declaration, possible collisions involving N, I, and strip, and a prefixed rewrite.
What you can do after
Apply a consistent naming policy and reduce conflicts by exposing fewer identifiers and using distinctive prefixes.
How it connects
Connects namespaces with identifiers, header files, macros, reserved names, and application programming interfaces.

Also explained in

Compare how 20 books explain namespace →

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.