Variables in C

A variable in C is a named memory location used to store data. Its value can change during the execution of a program, which is why the name β€œvariable” fits perfectly. Understanding variables is essential because every program works with data.

What is a Variable?

A variable is a name given to a memory location that stores a value. The value can be updated anytime in the program.

int age = 20;
float marks = 89.5;
char grade = 'A';

Here, age, marks, and grade are variables.

How Variables Work in Memory?

Variable Name β†’ age  
Data Type     β†’ int  
Value         β†’ 20  
Memory Example β†’ stored at address 0xA5C7  
Variables are just names pointing to memory blocks.

Rules for Naming Variables

βœ” Valid: height, _value, student1 βœ– Invalid: 1age, total marks, char

Types of Variables

C supports several types of variables based on scope and storage:

Examples of Variable Types

1. Local Variable

void test() {
    int x = 10;   // local variable
    printf("%d", x);
} 

Accessible only inside test().

2. Global Variable

int count = 5;  // global variable

void show() {
    printf("%d", count);
}

Accessible in any function inside the file.

3. Static Variable

void example() {
    static int num = 0;
    num++;
    printf("%d ", num);
}

Even after the function ends, num will preserve its value.

4. Extern Variable

extern int score;  // variable exists somewhere else

Used when sharing data across files.

Variable Declaration vs Definition

Declaration β†’ Telling compiler the variable exists  
Definition β†’ Allocating memory for that variable  

Example:

extern int a;  // declaration
int a = 10;    // definition

Variable Initialization

Assigning a value at the time of declaration.

int a = 10;
float price = 50.75;
char grade = 'A';

Multiple Variable Declaration

int x, y, z;
float a = 2.5, b = 3.4;

Memory Size of Variables

Size depends on data type:

int β†’ 4 bytes  
float β†’ 4 bytes  
double β†’ 8 bytes  
char β†’ 1 byte  

Practical Example

#include 

int main() {

    int age = 21;
    float percentage = 89.50;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Percentage: %.2f\n", percentage);
    printf("Grade: %c\n", grade);

    return 0;
}

Practical Example 2 – Updating Values

#include 

int main() {

    int count = 5;
    float price = 99.99;

    printf("Initial Count: %d\n", count);
    printf("Initial Price: %.2f\n", price);

    // Updating values
    count = 10;
    price = 149.50;

    printf("Updated Count: %d\n", count);
    printf("Updated Price: %.2f\n", price);

    return 0;
}

Practical Example 3 – Mixed Data Types

#include 

int main() {

    int roll = 101;
    char section = 'B';
    float height = 5.9;

    printf("Roll Number: %d\n", roll);
    printf("Section: %c\n", section);
    printf("Height: %.1f feet\n", height);

    return 0;
}

Practical Example 4 – Local & Global Variables

#include 

int total = 50;   // Global variable

int main() {

    int total = 10;  // Local variable (shadows global)

    printf("Local total: %d\n", total);

    // C doesn't support :: to access global when shadowed.
    // Demonstration with another name:
    printf("Global total: %d\n", ::total); // Not valid in C, example for concept only

    return 0;
}

Note: C does NOT support :: like C++. This example is only to explain local vs global variable concept.

Practical Example 5 – Static Variables

#include 

void counter() {
    static int num = 1;   // static variable
    printf("Count: %d\n", num);
    num++;                // retains its value
}

int main() {
    counter();
    counter();
    counter();
    return 0;
}

Output:
1
2
3

Practice Questions

  1. What is a variable? Explain with examples.
  2. Write 10 valid and 10 invalid identifiers.
  3. Differentiate between local and global variables.
  4. What is the difference between declaration and definition?
  5. Explain static variables with an example.

Practice Task

Write a C program that uses: Print all their values.