liveBook · Manning

Data representation

As explained in Modern C

A representation is the concrete encoding used to express a value inside a program or in an external file.

A representation is the concrete encoding used to express a value inside a program or in an external file. C programs should primarily reason about values, not their representations, because encodings can vary with the architecture or the type assigned by the programmer. The compiler translates between values and representations, so portable code should avoid depending on concrete encodings unless necessary. section 5.0

Observable value versus hidden representation

Consider `double x = 5.0; double y = 3.0; x = (x * 1.5) - y; printf("x is %g\n", x);`. The expressions produce 5.0, 7.5, 3.0, 4.5, and finally output a string representation of 4.5. Intermediate results are not observable unless stored in addressable memory or sent to an output device. Because only observable states matter, an optimizing compiler may replace the computation with `printf("x is 4.5\n");`, provided the program's observable behavior remains unchanged. section 5.1

   double x = 5.0;
   double y = 3.0;
   ...
   x = (x * 1.5) - y;
   printf("x is \%g\n", x);


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

Use the complete floating-point example to trace values from declarations through assignment and output, then compare the visible behavior with permitted compiler transformations. section 5.1

How Modern C covers it

How deep it goes
Defines representation and explains observable states and optimization, but does not detail particular binary encodings.
Examples
Uses decimal and Roman numerals, architecture-dependent encodings, and a floating-point computation optimized into fixed output.
What you can do after
Evaluate C behavior in terms of values and observable states without relying unnecessarily on concrete representations.
How it connects
Connects representations with values, types, portability, the abstract state machine, compilers, and optimization.

Also explained in

Compare how 4 books explain data representation →

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.