You write a line of code. It calculates a number. Or maybe it grabs input from a user. Then what? The program forgets it immediately. Unless you tell it not to.
That’s where variables come in.
Think of a variable as a labeled box in your computer’s memory. You put something in it, stick a name on the outside, and decide what kind of thing fits inside. Later, you can open the box and use what’s inside. Without variables, your software would be useless. It couldn’t hold data. It couldn’t make decisions. It just… ran forward and forgot everything.
How to Create a Variable in C
In C, you don’t just grab a box. You have to specify what kind of box it is. This is called declaring a variable.
Take this line:
int b;
This does two things. First, it creates a space in memory. Second, it labels that space b. The int part tells the compiler, “Hey, this box is for integers only.” No decimals. No letters. Just whole numbers.
You’ve now got a variable named b of type int. It’s empty, though. Or rather, it’s full of garbage data until you clean it out.
Storing and Using Values
To put something in b, you use the equals sign. Not mathematical equality. Assignment.
b = 5;
Now b holds the value 5. You can change it later. You can use it now.
Want to see what’s in b? Use printf.
printf("%d", b);
The %d is a placeholder. It says, “Put an integer here.” The computer looks at b, sees 5, and prints it. Simple.
But wait. What if you need to store a letter? Or a decimal? An int won’t cut it. That’s why C gives you choices.
Which Data Types Should You Use?
C has a few standard types for variables. Pick the right one, or your code breaks.
- int – For whole numbers. Like 42, -7, or 0.
- float – For numbers with decimals. Like 3.14 or 0.001.
- char – For single characters. Like
'm'or'Z'.
Notice the quotes? In C, characters go in single quotes. Strings (like “hello”) go in double quotes. Don’t mix them up.
You’ll see these types everywhere. int for counters. float for money or measurements. char for flags or single-letter codes.
So, why does this matter? Because every app you use, every game you play, every website you visit, relies on variables to keep track of state. Your score. Your login status. The position of your mouse. All variables.
Get them wrong, and things crash. Get them right, and your program actually works.
Variables are the memory of your program. Without them, code is just noise.
Start small. Declare one. Assign a value. Print it out. See what happens. Then try a `float
















