Curriculum / C / Lesson 02

Variables in C

A variable is a named piece of memory that stores a value your program can read or change.

2 of 11
beginner 📖 4 min read 🎯 1 question

Variables

A variable is a named piece of memory that stores a value your program can read or change. In C, every variable must be declared with a type before you use it — the compiler needs to know how much memory to reserve.

You can declare a variable and give it a value in one line, or declare it first and assign a value later.

💡 Key insight C is statically typed: once you declare a variable's type, it can never hold a different type of value.

Syntax

syntax
type variableName = value;

// declare then assign
type variableName;
variableName = value;

Example

%d, %f and %c are format specifiers that tell printf what type of value to print.

c
#include <stdio.h>

int main() {
    int age = 20;
    float price = 49.99;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Price: %.2f\n", price);
    printf("Grade: %c\n", grade);
    return 0;
}
outputAge: 20 Price: 49.99 Grade: A

Try it yourself

Copy this example into the C compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What happens if you use a local variable in C without initializing it?

Back to curriculum