liveBook · Manning

Public visibility

As explained in Modern C

Visibility is the property determining where a C identifier can be referred to by name.

Visibility determines where a C identifier can be referred to by name. It begins at the declaration and ends with the identifier’s scope. A declaration inside a block therefore has block scope; a declaration outside such a block can have file scope. Visibility is not the same as access to the object: an object may remain usable through a pointer or another visible declaration even when its original name is hidden. section 2.2 section 13.2

Shadowing changes which name is visible

In Listing 13.1, `double x` becomes visible at its declaration and remains visible through `main`, except inside the nested block after `int x = 0`. The inner `x` shadows the outer identifier. `squareIt(&x)` before the declaration of the inner variable refers to the `double`; `squareIt(xp)` still modifies that same `double` through the pointer; and `squareIt(&x)` after the block again refers to the outer variable. section 13.2

TAKEAWAYS

  • Identifiers are visible only inside their scope, beginning at their declaration. section 13.2
  • An identifier of the same name in a subordinate scope can shadow an outer identifier. section 13.2
  • Each variable definition creates a new object, although an `extern` declaration can introduce a name for an existing object. section 13.2

How Modern C covers it

How deep it goes
Distinguishes scope, shadowing, object accessibility, definitions, and extern declarations through worked examples.
Examples
Uses nested local variables, a pointer, and an extern declaration to demonstrate shadowing and object identity.
What you can do after
Evaluate which declaration a name denotes at each point in a C program and distinguish names from objects.
How it connects
Connects visibility with scope, shadowing, declarations, object definitions, pointers, storage duration, and linkage.

Also explained in

Compare how 8 books explain public visibility →

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.