Skip to main content

Command Palette

Search for a command to run...

Variables in C – How Programs Store Data

Published
5 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

💡
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: C Programming for Absolute Beginners. I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.

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:

Type

Stores

Example

int

Whole numbers

10, -5, 200

float

Decimal numbers

3.14, 2.5

double

Larger decimal numbers

3.141592

char

A single character

'A', 'b'

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:

type variable_name;

Example:

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:

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:

#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:

Data Type

Format Specifier

int

%d

float

%f

double

%lf

char

%c

Example with multiple variables:

#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:

age
studentScore
_price

Invalid names:

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:

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.

More from this blog

D

Dr. Ehoneah Obed

78 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.