-1

What will happen when we try to print the uninitialized variable in c that may be global or local? In C also variables automatically initialized by compiler with default values? Here is my code

#include<stdio.h>
int b;
int main()
{
    float a;
    printf("%f %d",a,b);
}

I got the output as

0.000000 0

Please can any one explain this?

vasu
  • 1
  • `int b;` is a global variable, so it is initialized to `0`, but `float a;` is a local variable, which is not initialized. So you are unlucky to see `0.000000` instead of another garbage value. – mch Jun 25 '18 at 13:58

1 Answers1

0

Global variables, which has static storage duration, are implicitly initialized.

Local variables, having automatic storage duration are not initialized unless done explicitly. However, if local variable has static storage class, it is initialized.

Quoting C11,

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261