C expresses orders for a computer as an imperative program. A small example combines declarations, an array, a loop, and output to print each value and its square. section 1.0 section 1.1
Listing 1.1 The first example of a C program
/* This may look like nonsense, but really is -*- mode: C -*- * 1 /
#include <stdlib.h> 2
#include <stdio.h>
3
/* The main thing that this program does. */ 4
int main(int argc, [[maybe_unused]] char* argv[argc+1]) {
// Declarations 5
double A[5] = { 6
[0] = 9.0, 7
[1] = 2.9,
[4] = 3.E+25, 8
[3] = .00007,
};
9
// Doing some work 10
for (size_t i = 0; i < 5; ++i) { 11
printf("element %zu is %g, \tits square is %g\n",
i,
A[i], 12
A[i]*A[i]); 13
}
14
return EXIT_SUCCESS; 15
}
The example initializes five array elements and prints each element with its square. section 1.0
A compiler translates C source into an executable for a particular platform. Correct C programs aim to be portable, but testing on one platform does not guarantee portability. Compiler warnings and errors can expose flaws such as an invalid main declaration or a missing declaration for printf. section 1.2 section 1.3
