# Variables in C – How Programs Store Data

> <div data-node-type="callout">
> <div data-node-type="callout-emoji">💡</div>
> <div data-node-type="callout-text">I recently started teaching a course on C programming and as part of my preparation for class, I usually write out lesson notes that I will be teaching from. I have therefore decided to make these into blog posts so anyone else learning C can also benefit from them. Therefore, this lesson is part of the series I am calling: <strong>C Programming for Absolute Beginners. </strong>I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.</div>
> </div>

If programs were people, variables would be their memory.

Every program needs a way to store information. Numbers. Letters. Results. That’s where variables come in.

In this lesson, you’ll learn:

*   What a variable is
    
*   The basic data types in C
    
*   How to declare and initialize variables
    
*   How to print them
    

Let’s keep it simple.

## 1\. What Is a Variable?

A variable is a **named container that stores data in memory**.

Think of it like a labeled box.

If you write “age” on a box and put the number 20 inside, that box now stores 20.  
Later, you can open the box and use that number.

In C, we create that “box” by declaring a variable.

## 2\. Data Types in C

Unlike some other languages, C needs to know **what type of data** you want to store.

Why? Because different data types take up different amounts of memory.

Here are the most common ones:

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>Type</p></th><th colspan="1" rowspan="1"><p>Stores</p></th><th colspan="1" rowspan="1"><p>Example</p></th></tr><tr><td colspan="1" rowspan="1"><p><code>int</code></p></td><td colspan="1" rowspan="1"><p>Whole numbers</p></td><td colspan="1" rowspan="1"><p>10, -5, 200</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>float</code></p></td><td colspan="1" rowspan="1"><p>Decimal numbers</p></td><td colspan="1" rowspan="1"><p>3.14, 2.5</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>double</code></p></td><td colspan="1" rowspan="1"><p>Larger decimal numbers</p></td><td colspan="1" rowspan="1"><p>3.141592</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>char</code></p></td><td colspan="1" rowspan="1"><p>A single character</p></td><td colspan="1" rowspan="1"><p>'A', 'b'</p></td></tr></tbody></table>

Important:

*   `int` is for whole numbers.
    
*   `float` and `double` are for decimals.
    
*   `char` is for a single letter inside single quotes.
    

## 3\. Declaring a Variable

To create a variable in C, you must declare it.

The format is:

```plaintext
type variable_name;
```

Example:

```c
int age;
float price;
char grade;
```

This tells C:

*   Create a variable called `age` that stores whole numbers.
    
*   Create a variable called `price` that stores decimals.
    
*   Create a variable called `grade` that stores one character.
    

Nothing is stored yet. We just created empty containers.

## 4\. Initializing a Variable

Initializing means giving the variable a value.

Example:

```c
int age = 20;
float price = 19.99;
char grade = 'A';
```

Here’s what happens:

*   `age` stores 20
    
*   `price` stores 19.99
    
*   `grade` stores 'A'
    

The `=` symbol is called the assignment operator.  
It stores the value on the right into the variable on the left.

## 5\. Printing Variables

To display output in C, we use `printf`.

Example:

```c
#include <stdio.h>

int main() {
    int age = 20;
    printf("Age: %d\n", age);
    return 0;
}
```

Let’s break this down:

*   `#include <stdio.h>` allows us to use `printf`.
    
*   `main()` is where the program starts.
    
*   `%d` tells C we are printing an integer.
    
*   `\n` means move to a new line.
    

Different data types use different format specifiers:

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>Data Type</p></th><th colspan="1" rowspan="1"><p>Format Specifier</p></th></tr><tr><td colspan="1" rowspan="1"><p>int</p></td><td colspan="1" rowspan="1"><p>%d</p></td></tr><tr><td colspan="1" rowspan="1"><p>float</p></td><td colspan="1" rowspan="1"><p>%f</p></td></tr><tr><td colspan="1" rowspan="1"><p>double</p></td><td colspan="1" rowspan="1"><p>%lf</p></td></tr><tr><td colspan="1" rowspan="1"><p>char</p></td><td colspan="1" rowspan="1"><p>%c</p></td></tr></tbody></table>

Example with multiple variables:

```c
#include <stdio.h>

int main() {
    int age = 18;
    float height = 5.7;
    char grade = 'B';

    printf("Age: %d\n", age);
    printf("Height: %.1f\n", height);
    printf("Grade: %c\n", grade);

    return 0;
}
```

`%.1f` means show 1 decimal place.

## 6\. Rules for Naming Variables

There are some basic rules:

*   Must start with a letter or underscore
    
*   Cannot start with a number
    
*   No spaces allowed
    
*   Cannot use C keywords (like `int`, `return`, etc.)
    

Valid names:

```plaintext
age
studentScore
_price
```

Invalid names:

```plaintext
1age
student score
int
```

## 7\. Common Beginner Mistakes

Here are mistakes to avoid:

1.  Forgetting the semicolon `;`
    
2.  Using the wrong format specifier
    
3.  Forgetting single quotes for `char`
    
4.  Mixing up data types (like storing 3.5 in an int)
    

Example mistake:

```c
int number = 3.5;   // This will cut off the decimal
```

C does not automatically fix these for you. It expects precision.

## 8\. Practice Exercises

Try these on your own.

1.  Create a variable called `score` and store 85 in it. Print it.
    
2.  Create a float variable called `temperature` and print it with two decimal places.
    
3.  Create a char variable called `initial` and print it.
    

* * *

## Final Thoughts

Variables are the foundation of programming.

If you understand:

*   How to declare them
    
*   How to assign values
    
*   How to print them
    

You’ve taken your first real step in C.

In the next lesson, we’ll teach your program how to **make decisions** using `if` statements.

And that’s where things start getting interesting.
