31

I'm reading source code of nginx and find it's not initializing many of the numerical variables, including ngx_int_t ngx_last_process;,here ngx_int_t defined as long int

#if 0
    ngx_last_process = 0;
#endif

So here @Igor Sysoev think it unnecessary to do the initialization?

But in the programe it's assuming the default value is 0:

    for (s = 0; s < ngx_last_process; s++) {
        if (ngx_processes[s].pid == -1) {
            break;
        }
    }

Is it guranteed that un-initialized variable will have 0 value in c at all?

Shweta
  • 5,198
  • 11
  • 44
  • 58
cpuer
  • 7,413
  • 14
  • 35
  • 39

4 Answers4

35

External and static variables are initialized to zero by default, this is guaranteed. Automatic and register variables that do not have en explicit initializer will have an indeterminate value (either an unspecified value or a trap representation).

From The Standard:

C89

6.5.7:

If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant. If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

C99

6.2.4, §5:

For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in anyway.(Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively,anew instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

6.7.8, §10:

If an object that has automatic storage duration is not initialized explicitly,its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive orunsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules;

— if it is a union, the first named member is initialized (recursively) according to these rules.

3.17.2, §1:

indeterminate value: either an unspecified value or a trap representation

3.17.3, §1:

unspecified value: valid value of the relevant type where this International Standard imposes no requirements on which value is chosen in any instance. NOTE An unspecified value cannot be a trap representation.

Wiz
  • 2,145
  • 18
  • 15
  • "(some garbage value" ... or trap representation or anything ... ")" – pmg Jun 02 '11 at 10:04
  • @pmg,what's `trap representation `? – cpuer Jun 02 '11 at 10:04
  • 1
    "undefined value" should be "indeterminate value" to use the wording of the C99 standard. A trap representation is a bit pattern in memory that causes undefined behavior if you access it with an l-value of type other than `char`. – Pascal Cuoq Jun 02 '11 at 10:09
  • @cpuer: a `trap representation` is an arrangement of bits that does not represent a valid value. Most current computers use all bit representations for different and valid values, especially in integer types, but the language does not mandate that. **A trap representation is not a garbage value.** – pmg Jun 02 '11 at 10:14
7

automatic (local) variables are not guaranteed to be zero, can contain garbage.

global variables and static variables are guaranteed to be zero.

phoxis
  • 60,131
  • 14
  • 81
  • 117
5

Variables declared (as int)at file scope are initialized to 0.

Example

int i;
int main()
{
   int x;
   printf("%d",i); // prints 0
   printf("%d",x); // prints some unspecified value
}

Is it guranteed that un-initialized variable will have 0 value in c at all?

Nopes! That doesn't hold for variables declared at function scope.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 1
    The `printf("%d",x);` statement can also make your *NUM LOCK* light blink (especially if your architecture allows for trap representations in `int`s) – pmg Jun 02 '11 at 10:01
  • @pmg : Yes! After all the behavior is undefined. – Prasoon Saurav Jun 02 '11 at 10:02
  • 1
    Slightly wrong. You could *declare* `int x;` at file scope, and still have a definition `int x=1;` somewhere else. :) – R.. GitHub STOP HELPING ICE Jun 02 '11 at 12:44
  • I was convinced that this is how C works, but experienced something differnt. In a small program, __x was zero__. Since x was zero in consequent executions, I don't think it was zero by chance. Could it be that newer compilers result in a different behavior? – phylib Mar 04 '20 at 10:03
2

It depends on how the variable is allocated. Statically allocated variables are initialized to zero, whereas variables allocated on the stack or with malloc() are not.

hammar
  • 138,522
  • 17
  • 304
  • 385