Problem
Declare int and float variables without initializing and try to print their values in C language. Explain what will happen.
Solution
If a variable is declared but not initialized or uninitialized and if those variables are trying to print, then, it will return 0 or some garbage value.
Whenever we declare a variable, a location is allocated to that variable. The only thing is with the help of initialization, we are trying to occupy the memory location which is already allotted while declaration.
But in the below program, we are not initializing the values in the memory locations which are reserved. But, by default, the locations are occupied with 0 or garbage values. When we are trying to print it displays 0 or garbage value as output.
Example
Following is the C program for accessing variables in int and float −
#include<stdio.h> int main(){ float a,b,c; int x,y,z; printf("value of a:%f\n",a); printf("value of b:%f\n",b); printf("value of c:%f\n",c); printf("value of x:%d\n",x); printf("value of y:%d\n",y); printf("value of z:%d",z); return 0; }
Output
When the above program is executed, it produces the following result −
value of a:0.000000 value of b:0.000000 value of c:0.000000 value of x:1512368 value of y:0 value of z:27