Home >Backend Development >C++ >In C language, access uninitialized integer and floating-point variables
Declaring uninitialized int and float variables in C and trying to print their values. Explain what will happen.
If a variable is declared but not initialized, or not initialized, and if these variables try to print, then it will return 0 or some garbage value.
Whenever we declare a variable, a location is assigned to the variable. The only problem is that, by initializing, we are trying to occupy a memory location that was already allocated at declaration time.
But in the following program, we have not initialized the value in the reserved memory location. However, by default these locations are occupied by 0 or garbage values. When we try to print it shows 0 or garbage value as output.
The following is a C program to access int and float variables-
Live Demo
#include<stdio.h> int main(){ float a,b,c; int x,y,z; printf("value of a:%f</p><p>",a); printf("value of b:%f</p><p>",b); printf("value of c:%f</p><p>",c); printf("value of x:%d</p><p>",x); printf("value of y:%d</p><p>",y); printf("value of z:%d",z); return 0; }
When the above program is executed, the following results will be produced-
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
The above is the detailed content of In C language, access uninitialized integer and floating-point variables. For more information, please follow other related articles on the PHP Chinese website!