7.2.a. All variables shall be initialized before use.

7.2.b. It is preferable to define local variables as you need them, rather than all at the top of a function.

7.2.c. If project- or file-global variables are used, their definitions shall be grouped together and placed at the top of a source code file.

7.2.d. Any pointer variable lacking an initial address shall be initialized to NULL.


uint32_t  g_array[NUM_ROWS][NUM_COLS] = { ... };

    for (int col = 0; col < NUM_COLS; col++)
        g_array[row][col] = ...;

Reasoning: Too many programmers assume the C run-time will watch out for them, e.g., by zeroing the value of uninitialized variables on system startup. This is a bad assumption, which can prove dangerous in a mission-critical system. For readability reasons it is better to declare local variables as close as possible to their first use,12 which C99 makes possible by incorporating that earlier feature of C++.

Enforcement: An automated tool shall scan all of the source code prior to each build, to warn about variables used prior to initialization; static analysis tools can do this. The remainder of these rules shall be enforced during code reviews.


[12] [Uwano] describes back-and-forth code review eye movements that demonstrate the value of placing variable declarations as close as possible to the code that first references them.