liveBook · Manning

C program

As explained in Modern C

A C program is a computer program expressed in the C programming language.

A C program gives a computer specific orders in an imperative style. It can be understood both as executable instructions and as a machine that manipulates typed values. section 1.0 section 5.1

Listing 1.1. A first example of a C program
 1   /* This may look like nonsense, but really is -*- mode: C -*- */
 2   #include <stdlib.h>
 3   #include <stdio.h>
 4
 5   /* The main thing that this program does. */
 6   int main(void) {
 7     // Declarations
 8     double A[5] = {
 9       [0] = 9.0,
10       [1] = 2.9,
11       [4] = 3.E+25,
12       [3] = .00007,
13     };
14
15     // Doing some work
16     for (size_t i = 0; i < 5; ++i) {
17         printf("element %zu is %g, \tits square is %g\n",
18                i,
19                A[i],
20                A[i]*A[i]);
21     }
22
23     return EXIT_SUCCESS;
24   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":1}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":1}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":1}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":1}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":1}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":1}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":1}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":1}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":1}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":1}],[{\"line\":20,\"ch\":1},{\"line\":20,\"ch\":2}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":9,\"ch\":15},{\"line\":9,\"ch\":16}],[{\"line\":10,\"ch\":19},{\"line\":10,\"ch\":20}],[{\"line\":11,\"ch\":1},{\"line\":11,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":1}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":1}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":1}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":1}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":1}],[{\"line\":1,\"ch\":5},{\"line\":1,\"ch\":13}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":13}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":10,\"ch\":15},{\"line\":10,\"ch\":16}],[{\"line\":11,\"ch\":10},{\"line\":11,\"ch\":11}],[{\"line\":12,\"ch\":1},{\"line\":12,\"ch\":2}],[{\"line\":22,\"ch\":1},{\"line\":22,\"ch\":2}],[{\"line\":1,\"ch\":5},{\"line\":1,\"ch\":13}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":13}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":10,\"ch\":10},{\"line\":10,\"ch\":11}],[{\"line\":13,\"ch\":1},{\"line\":13,\"ch\":2}],[{\"line\":23,\"ch\":1},{\"line\":23,\"ch\":2}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":7,\"ch\":16},{\"line\":7,\"ch\":17}],[{\"line\":10,\"ch\":20},{\"line\":10,\"ch\":21}],[{\"line\":14,\"ch\":1},{\"line\":14,\"ch\":2}],[{\"line\":15,\"ch\":30},{\"line\":15,\"ch\":31}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":2}],[{\"line\":15,\"ch\":1},{\"line\":15,\"ch\":2}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":8}],[{\"line\":16,\"ch\":13},{\"line\":16,\"ch\":16}],[{\"line\":4,\"ch\":12},{\"line\":4,\"ch\":16}],[{\"line\":5,\"ch\":9},{\"line\":5,\"ch\":13}],[{\"line\":5,\"ch\":14},{\"line\":5,\"ch\":18}],[{\"line\":6,\"ch\":1},{\"line\":6,\"ch\":2}],[{\"line\":11,\"ch\":20},{\"line\":11,\"ch\":21}],[{\"line\":16,\"ch\":1},{\"line\":16,\"ch\":2}],[{\"line\":7,\"ch\":1},{\"line\":7,\"ch\":2}],[{\"line\":17,\"ch\":1},{\"line\":17,\"ch\":2}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":13}],[{\"line\":8,\"ch\":1},{\"line\":8,\"ch\":2}],[{\"line\":9,\"ch\":17},{\"line\":9,\"ch\":18}],[{\"line\":18,\"ch\":1},{\"line\":18,\"ch\":2}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":2}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}],[{\"line\":15,\"ch\":7},{\"line\":15,\"ch\":10}],[{\"line\":15,\"ch\":12},{\"line\":15,\"ch\":18}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":2}],[{\"line\":16,\"ch\":11},{\"line\":16,\"ch\":17}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":2}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":2}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":2}],[{\"line\":22,\"ch\":7},{\"line\":22,\"ch\":13}],[{\"line\":22,\"ch\":14},{\"line\":22,\"ch\":26}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}]]"}
!@%STYLE%@!

Read the complete example from its declarations and loop to its formatted terminal output and successful return. section 1.0

TAKEAWAY:

  • Treat variables as holders of values whose contents change as expressions are evaluated and assignments occur. section 5.1
  • Distinguish observable effects, such as terminal output, from intermediate computations that are never stored or emitted. section 5.1
  • Expect a compiler to optimize away steps only when the program's end results remain unchanged. section 5.1

A small computation

With x initialized to 5.0 and y to 3.0, the assignment x = (x * 1.5) - y changes x to 4.5; printf then writes that value to the terminal. section 5.1

How Modern C covers it

How deep it goes
Explains declarations, expressions, assignments, observability, optimization, types, initialization, and numeric behavior without descending into machine-specific execution.
Examples
Uses a looping array program and a floating-point assignment that prints the resulting value.
What it assumes
Starts from programming fundamentals and teaches imperative instructions, values, types, and initialization through small examples.
What you can do after
Evaluate C code by tracing value changes, identifying observable effects, and recognizing semantics-preserving compiler transformations.
How it connects
Connects imperative programming, variables, expressions, types, abstract state machines, terminal output, and compiler optimization.

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.