To initialize or not initialize variables

Steve Summit scs at adam.mit.edu
Thu Mar 28 18:36:59 AEST 1991


In article <67693 at eerie.acsu.Buffalo.EDU> v056ped5 at ubvmsd.cc.buffalo.edu writes:
>Also, why don't they write compilers so that numeric data is initialized
>to zero and character data is initialized to a single EOL?

Perhaps the simplest answer, though not terribly satisfying, is
because "it's not done that way."

Actually, statically-allocated data is guaranteed to be
initialized to 0.  (This translates to NULL pointers for
character pointers, and '\0' for character arrays.)

If you want consistent, predictable (though not necessarily
useful) behavior for dynamically-allocated memory, use calloc()
(but see the FAQ list for caveats, and more information about
initialization and allocation in general).

Given the realities of dynamic memory allocation, a better
question might be, "why don't they write compilers so that
numeric data is initialized to 2574 and character data is
initialized to '\007'?"

                                            Steve Summit
                                            scs at adam.mit.edu



More information about the Comp.lang.c mailing list