Advertisement

Variables in C

C Language Variables & Data Types 📅 May 2026 ⏱ 2 min read 🆓 Free

Variables in C

A variable is a named memory location that stores data. Think of computer memory as a row of numbered boxes — a variable is a labeled box where you store a value that can change.

Declaring and Using Variables

c
#include <stdio.h>

int main() {
    // ── Declaration ──────────────────────────────
    int    age;        // whole number
    float  price;      // decimal number (7 digit precision)
    char   grade;      // single character
    double pi;         // high-precision decimal (15 digits)
    
    // ── Initialization ───────────────────────────
    age   = 17;
    price = 199.99f;
    grade = 'A';
    pi    = 3.14159265358979;
    
    // ── Declare + Initialize in one line ─────────
    int marks    = 95;
    float weight = 65.5f;
    
    // ── Multiple variables, same type ────────────
    int x = 5, y = 10, z = 15;
    
    // ── Print ────────────────────────────────────
    printf("Age   : %d\n",   age);
    printf("Price : %.2f\n", price);
    printf("Grade : %c\n",   grade);
    printf("Pi    : %lf\n",  pi);
    printf("x+y+z : %d\n",   x+y+z);
    
    return 0;
}
▶ Output
Age   : 17
Price : 199.99
Grade : A
Pi    : 3.141593
x+y+z : 30

Variable Types — Quick Reference

TypeMemoryUse CaseFormat
int4 bytesAge, count, marks, roll no%d
float4 bytesPrice, weight, temperature%f
double8 bytesScientific calculations%lf
char1 byteGrade, gender, single letter%c
long int8 bytesLarge numbers%ld
unsigned int4 bytesOnly positive values%u

Variable Naming Rules

  • Start with letter or underscore: age, _count
  • Can contain letters, digits, underscore: student_marks2
  • No spaces: use camelCase studentMarks or snake_case student_marks
  • Case sensitive: Age and age are TWO different variables!
  • Cannot use keywords: int, float, for, if etc.
⚠ Warning: Uninitialized variables in C contain garbage values — random leftover data from previous programs. ALWAYS initialize variables before using them!
Advertisement
← Back to C Language Index