Search code examples
cglobalpi

How can we be sure that value of 'b' is 0 at beginning in this code


This is a program which I came across on the net that calculates the value of 𝝅.

#include <stdlib.h>
#include <stdio.h>

long a=10000,b,c=2800,d,e,f[2801],g;//--------HERE the value of b---------//

int main()
{
    printf("\nValue of b: %ld", b);
    getch();
    for(;b-c;)
        f[b++]=a/5;
    for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
        for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}

The calculated value is correct. But how could the programmer be sure that the initial value of b would be 0?

It does not seem to be initialized at all!

Is there some specialty about initial value of global variables?


Solution

  • As pointed out in this question, global variables are initialized to 0 by default.

    In your code, b is declared as a global variable.