liveBook · Manning

Binding

As explained in Modern C

A variable is a named C data object whose stored value can change during execution, including when its storage is local to a function invocation.

A value that changes

Consider `double x = 5.0; double y = 3.0; x = (x * 1.5) - y; printf("x is %g\n", x);`. The assignment changes x from 5.0 to 4.5; the later use of x supplies 4.5 to printf. The multiplication and subtraction also produce values, but those intermediate results are not stored in named variables. section 5.1

A variable is a named data object that stores a value. Keep four things distinct: the identifier (`x`), the object it names, the object’s type (`double`), and its current value. A declaration identifies the type and name; an initializer can give the object its first value. TAKEAWAY: the name is not the value, and assignment can change the value without changing the name or type. section 2.2 section 5.1

int main(void);
double A[5];
size_t i;


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":0,\"ch\":9},{\"line\":0,\"ch\":13}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":6}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}]]"}
!@%STYLE%@!

Compare `size_t i` and `double A[5]`, which declare variables, with `int main(void)`, which declares a function. The brackets make A an array of five double items. section 2.2

Three practical consequences follow:

  • Initialize variables before accessing them so their values are known. Scalar initializers may be expressions; `{0}` initializes object types other than variable-length arrays. section 5.5
  • Each function call has its own copy of local variables, including when the function calls itself recursively. section 7.4
  • Do not assume every source-level variable survives as storage in the executable. A compiler may remove computations and definitions when it preserves the observable result. section 5.1

How Modern C covers it

How deep it goes
Distinguishes identifier, object, type, and value; traces assignment and initialization, while deferring pointer-related exceptions.
Examples
Traces x changing from 5.0 to 4.5 and contrasts declarations of a function, an array, and a scalar variable.
What it assumes
Introduces types initially as specifications for numeric objects and postpones pointer-related initialization exceptions.
What you can do after
Practice/perform value tracing through assignments and initialize variables before accessing them.
How it connects
Connects variables to declarations, types, arrays, initializers, assignment, optimization, and local variables.

Also explained in

Compare how 11 books explain binding →

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.