Accessing variables of Int and Float without initializing in C

When we declare variables in C without initializing them, their values are unpredictable. This article demonstrates what happens when we try to access uninitialized int and float variables and explains the underlying memory behavior.

Syntax

data_type variable_name;    // Declaration without initialization

Understanding Uninitialized Variables

When a variable is declared but not initialized, the following occurs −

  • Memory is allocated for the variable during declaration
  • The memory location contains whatever data was previously stored there
  • This results in garbage values − unpredictable leftover data from previous program executions
  • The actual values depend on the compiler, system, and previous memory usage

Example: Accessing Uninitialized Variables

The following program declares int and float variables without initialization and prints their values −

#include <stdio.h>

int main() {
    float a, b, c;
    int x, y, z;
    
    printf("value of a: %f<br>", a);
    printf("value of b: %f<br>", b);
    printf("value of c: %f<br>", c);
    printf("value of x: %d<br>", x);
    printf("value of y: %d<br>", y);
    printf("value of z: %d<br>", z);
    
    return 0;
}
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

Key Points

  • Unpredictable behavior: Uninitialized variables may contain any value − sometimes zero, sometimes garbage
  • Different data types: int variables often show large random numbers, while float variables may appear as 0.000000
  • Security risk: Accessing uninitialized memory can expose sensitive data from previous operations
  • Best practice: Always initialize variables before use to avoid undefined behavior

Proper Variable Initialization

#include <stdio.h>

int main() {
    float a = 0.0, b = 1.5, c = 2.7;
    int x = 0, y = 10, z = 25;
    
    printf("value of a: %f<br>", a);
    printf("value of b: %f<br>", b);
    printf("value of c: %f<br>", c);
    printf("value of x: %d<br>", x);
    printf("value of y: %d<br>", y);
    printf("value of z: %d<br>", z);
    
    return 0;
}
value of a: 0.000000
value of b: 1.500000
value of c: 2.700000
value of x: 0
value of y: 10
value of z: 25

Conclusion

Uninitialized variables in C contain garbage values that lead to unpredictable program behavior. Always initialize variables before use to ensure reliable and secure code execution.

Updated on: 2026-03-15T13:16:07+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements