C56. C Initializers and Uninitialized Variables

For convenience, C allows us to specify initial values for variables as we're declar­ing them. To initialize a variable, we write the = symbol after its declarator, then follow that with an initializer. (Don't confuse the = symbol in a declaration with the assignment operator; initialization isn't the same as assignment.)

We've seen various kinds of initializers in previous chapters. The initializer for a simple variable is an expression of the same type as the variable:
int i = 5 / 2; /* i is initially 2 */

Uninitialized Variables
In previous chapters, we've implied thai uninitialized variables have undefined val­ues. That's not always true; the initial value of a variable depends on its storage duration:
Variables with automatic storage duration have no default initial value. The initial value of an automatic variable can't be predicted and may be different each time the variable comes into existence.
Variables with static storage duration have the value zero by default. Unlike memory allocated by calloc. which is simply set to zero bits, a static vari­able is correctly initialized based on its type: integer variables are initialized to 0, floating variables are initialized to 0.0, and pointer variables contain a null pointer.
As a matter of style, it's better to provide initializers for static variables rather than rely on the fact that they're guaranteed to be zero. If a program accesses a variable that hasn't been initialized explicitly, someone reading the program later can't easily determine whether the variable is assumed to be zero or whether it's initialized by an assignment somewhere in the program.

Program Design
It's obvious that real-world programs are larger than the examples in this book, but you may not realize just how much larger. Faster CPUs and larger main memories have made it possible to write programs that would have been impractical just a few years ago. The popularity of graphical user interfaces has added greatly to the average length of a program. Most full-featured programs today are at least 100,000 lines long. Million-line programs are commonplace, and it's not unheard- of for a program to have 10 million lines or more.

Although C wasn't designed for writing large programs, many large programs have in fact been written in C. It's tricky, and it requires a great deal of care, but it can be done. In this chapter. I'll discuss techniques that have proved to be helpful for writing large programs and show which C features (the static storage class, for example) are especially useful.

Writing large programs (often called "programming-in-the-large") is quite dif­ferent from writing small ones—it's like the difference between writing a term paper (10 pages double-spaced, of course) and a 1000-page book. A large program requires more attention to style, since many people will be working on it. It requires careful documentation. It requires planning for maintenance, since it will likely be modified many times.

No comments: