Skip to content
StudyFame

Variables in C

A program needs to remember things: your marks, a price, a name. It stores them in variables.

Think of a variable as a labelled box in the computer's memory. The label is the name, and the thing inside is the value. In this lesson you will learn how to create a variable, put a value in it, and change it.

What You Will Learn

  • What a variable is.
  • The difference between declaration and initialization.
  • Rules for naming variables.
  • How to change the value of a variable.
  • Types of variables: local, global and static.

What is a Variable in C?

Exam definition: A variable is a named memory location used to store data. Its value can change while the program runs.

  • Every variable has a name (like marks), a data type (like int) and a value (like 90).
  • The data type decides what kind of value it can store and how much memory it uses.
  • "Variable" means "can vary", that is, the value can change.

Memory trick: Variable = labelled box. Type = size of the box, name = label on the box, value = what is inside.

Declaration vs initialization

DeclarationInitialization
MeaningCreating the variable (making the box)Giving it its first value (filling the box)
Exampleint marks;int marks = 90;
MemoryMemory is reservedMemory is reserved and a value is stored

A local variable that is declared but not initialized holds a garbage value: some random leftover number. Always give a value before you use it.

Rules for naming variables

Variable names follow the identifier rules you learned earlier:

  • Only letters, digits and underscore _.
  • Must start with a letter or underscore.
  • No spaces, no special characters, no keywords.
  • Case-sensitive: total and Total are different.

Types of variables (by where they are declared)

TypeDeclaredUsed
LocalInside a function or blockOnly inside that function or block
GlobalOutside all functionsAnywhere in the program
StaticWith the static keywordKeeps its value between function calls

You will learn these in detail in the lessons on scope and storage classes.

Syntax of Variable Declaration in C

data_type variable_name;                  /* declaration */
data_type variable_name = value;          /* declaration + initialization */
data_type name1 = value1, name2, name3;   /* many variables of the same type */

Examples: int age = 19;, float price = 49.5f;, char grade = 'A';, int a = 1, b = 2, c;

Example 1: Declare, Initialize and Change a Variable

#include <stdio.h>

int main(void)
{
    int marks = 75;          /* declare and initialize */
    printf("Old marks: %d\n", marks);

    marks = 82;              /* change the value */
    printf("New marks: %d\n", marks);

    marks = marks + 5;       /* add 5 to the current value */
    printf("After bonus: %d\n", marks);

    return 0;
}

Output

Old marks: 75
New marks: 82
After bonus: 87

Code Explanation

  1. int marks = 75;: creates an integer variable named marks and stores 75 in it.
  2. printf("... %d\n", marks);: %d is replaced by the value of marks.
  3. marks = 82;: the old value 75 is replaced by 82. A variable holds only one value at a time.
  4. marks = marks + 5;: take the current value (82), add 5, and store the result (87) back in marks.

How Variables Work in Memory

  • When the program reaches int marks, the computer reserves a block of memory (usually 4 bytes for an int).
  • That block has an address (a number). The name marks is just an easy label for that address.
  • Writing marks = 82 stores 82 in that block, replacing whatever was there.
  • Reading marks fetches the current value from that block.

Example 2: C Program to Calculate Simple Interest

#include <stdio.h>

int main(void)
{
    float principal = 10000.0f;   /* amount in rupees */
    float rate = 7.5f;            /* interest rate per year (%) */
    int years = 2;

    float interest = (principal * rate * years) / 100;

    printf("Principal: %.2f\n", principal);
    printf("Simple Interest: %.2f\n", interest);
    printf("Total Amount: %.2f\n", principal + interest);

    return 0;
}

Output

Principal: 10000.00
Simple Interest: 1500.00
Total Amount: 11500.00

Simple Interest = (P × R × T) / 100 = (10000 × 7.5 × 2) / 100 = 1500.

Important Points to Remember

  • A variable is a named memory location.
  • Every variable must be declared before it is used.
  • Declaration creates it; initialization gives the first value.
  • An uninitialized local variable has a garbage value.
  • A variable holds one value at a time; a new value replaces the old one.

Common Mistakes Beginners Make

MistakeWhat happensFix
Using a variable before declaring itError: 'x' undeclaredDeclare it first: int x;
Using a variable without a valueGarbage value in the outputInitialize it: int sum = 0;
Declaring the same name twiceError: redefinition of 'x'Declare once, then just assign
90 = marks;Error: lvalue requiredThe variable goes on the left: marks = 90;

Variables in C Practice Questions

  1. What is a variable? Why is it called a variable?
  2. What is the difference between declaration and initialization?
  3. What is a garbage value?
  4. Write a statement to declare three integer variables a, b, c with a = 10.
  5. What is the value of x after: int x = 5; x = x * 2; x = x - 3;?
Show answers
  1. A named memory location that stores data. It is called a variable because its value can change.
  2. Declaration creates the variable (int a;). Initialization gives it its first value (int a = 5;).
  3. A random leftover value in an uninitialized local variable.
  4. int a = 10, b, c;
  5. 7 (5 × 2 = 10, then 10 − 3 = 7).

Coding Exercise

Write a program that stores the length (12) and width (8) of a rectangle in variables, calculates its area and perimeter in two more variables, and prints both.

Show solution
#include <stdio.h>

int main(void)
{
    int length = 12, width = 8;
    int area = length * width;
    int perimeter = 2 * (length + width);

    printf("Area = %d\n", area);
    printf("Perimeter = %d\n", perimeter);
    return 0;
}
Area = 96
Perimeter = 40

Quick Quiz: Variables in C (MCQ)

  1. A variable is a:

    A) Fixed value   B) Named memory location   C) Keyword   D) Function

  2. Which is a valid declaration with initialization?

    A) int = 5 x;   B) x int = 5;   C) int x = 5;   D) int 5 = x;

  3. An uninitialized local variable contains:

    A) 0   B) 1   C) A garbage value   D) An error

  4. After int a = 4; a = a + a;, the value of a is:

    A) 4   B) 8   C) 16   D) 44

  5. A variable declared outside all functions is:

    A) Local   B) Global   C) Static   D) Constant

Show answers
  1. B: a named memory location.
  2. C: int x = 5;
  3. C: a garbage value.
  4. B: 8.
  5. B: global.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

VariableNamed memory location; value can change
PartsData type + name + value
Declarationint x; (creates it)
Initializationint x = 10; (first value)
Uninitialized localGarbage value
Types (by place)Local, Global, Static
Size of intUsually 4 bytes

Frequently asked questions

What is the difference between declaration and initialization?
Declaration creates the variable; initialization gives it its first value.
What is a garbage value?
A random leftover value in a local variable that was not initialized.
Can a variable hold two values at once?
No. A new value replaces the old one.
Why must a variable be declared before use?
So the compiler knows its type and how much memory to reserve.

Remember

  • Variable = labelled box (type = size, name = label, value = inside).
  • Always initialize before use: int sum = 0;

Next Topic

Next lesson: Data Types in C. You will learn which types of data C can store and how much memory each one uses.