Search code examples
cturbo-c

Default values in turbo c


I'm getting the output as -28762.Why is it not 0(zero), which should be the default value of integer?

#include<stdio.h>
#include<conio.h>

void main(){
   int a;
   clrscr();
   printf("%d",a);
   getch();
}

Solution

  • While you've not initialized the variable, the variable does refer to come location in memory.

    This location's value when converted to integer would yield something , and in your case it's -28762

    Please note that when you declare any simple data types like int, float etc this happens.

    For complex types like User defined types, and structures this will not happen.

    The integer variables are not defaulted to zero, unless they are file scope or static. See reference link https://msdn.microsoft.com/en-us/library/y2xtdbay.aspx

    If the declaration of z was for an uninitialized static variable or was at file scope, it would receive an initial value of 0, and that value would be unmodifiable.