liveBook · Manning

Imperative programming

As explained in Modern C

Imperative programming is a programming paradigm in which programs specify operations and commands that change computational state to complete tasks.

Imperative programming organizes a program as orders for a computer to carry out. A small C program makes the idea visible: it declares an array, repeatedly computes each element's square, and prints the results. section 1.0 section 1.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%@!

Inspect the complete C example: declarations establish the data, the for statement performs repeated work, and printf produces terminal output. section 1.0

The example proceeds through explicit statements. A for loop visits five positions; each iteration passes the index, the selected array value, and its square to printf. The program therefore expresses its task as a sequence of commands rather than an abstract description of the desired result. section 1.1

TAKEAWAY

  • C is an imperative programming language. section 1.1
  • Statements act as instructions, including calls to functions such as printf. section 1.1

Observable result

Running the program prints five terminal lines, including “element 0 is 9, its square is 81” and “element 4 is 3e+25, its square is 9e+50.” section 1.1

How Modern C covers it

How deep it goes
Defines the paradigm through one complete C example, stopping short of broader paradigm comparisons or implementation internals.
Examples
Uses a C program that loops over an initialized array, squares each value, and prints formatted results.
What it assumes
Assumes no prior C knowledge and builds initial understanding from a small complete program.
What you can do after
Evaluate how explicit C statements, loops, and function calls produce observable terminal output.
How it connects
Connects imperative programming with C, statements, functions, loops, arrays, and terminal output.

Also explained in

Compare how 14 books explain imperative programming →

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.