Part 1 · Lesson 2
Variables and Data Types
On this page
What you'll learn: variables, the
int,doubleandchartypes, and printing values with format specifiers such as%d,%.2fand%c.Before you start: Lesson 1.
In the last lesson, we printed a fixed message. To write useful programs, we need a way to store and manipulate data that can change. This is where variables come in.
What Is a Variable?
A VARIABLE is a named storage location in the computer's memory. Think of it as a labeled box where you can store a piece of information. You give it a name, and you can change the contents of the box (the value) as your program runs.
What Is a Data Type?
A DATA TYPE specifies the type of data that a variable can hold. C is a "statically-typed" language, which means you must explicitly tell the compiler the data type of every variable you create. This helps the compiler allocate the correct amount of memory and ensures you use the variable correctly.
The process of creating a variable is called DECLARATION. The process of giving a variable its first value is called INITIALIZATION.
An INTEGER is a whole number (no decimal point). The int data type is
used to store them.
The lesson first DECLARES a variable named player_score of type int:
int player_score;
This tells the compiler: "Reserve a spot in memory for an integer,
and I will call it player_score."
Then it INITIALIZES the variable by assigning it a value using the
assignment operator =:
player_score = 1250;
You can also declare and initialize a variable in one step, which is what you will usually do:
int number_of_lives = 3;
To print the value of a variable, we use printf with a FORMAT SPECIFIER.
A FORMAT SPECIFIER is a placeholder in the string that tells printf
what type of data to expect and how to format it.
printf("Player score: %d\n", player_score);
%d is the format specifier for a signed decimal integer (int).
The variable player_score is passed as a second argument to printf,
and its value will replace %d in the output.
To store numbers with decimal points, we use FLOATING-POINT types.
The most common one is double, which stands for "double-precision".
It can store numbers with much greater precision than its counterpart, float.
For this course, we will prefer double for all decimal numbers.
double item_price = 19.99;
%f is the format specifier for a double (and float).
By default, %f might print many extra zeros. You can control the
number of decimal places by modifying the format specifier. For example,
%.2f tells printf to print only 2 digits after the decimal point:
printf("The price, formatted neatly, is: $%.2f\n", item_price);
(The $ here is just a character in the message; only the % starts a
format specifier.)
The char data type is used to store a single CHARACTER. This can be
a letter, a number, or a symbol.
A CHARACTER LITERAL is enclosed in SINGLE quotes (e.g., 'A'). This is different from a string literal, which uses double quotes.
char player_grade = 'A';
%c is the format specifier for a char.
You can print multiple variables in a single printf call. Just make
sure the format specifiers and the variables are listed in the same order:
printf("Summary: Player with %d lives and a grade of '%c' has a score of %d.\n",
number_of_lives, player_grade, player_score);
The first %d gets number_of_lives, the %c gets player_grade and the
second %d gets player_score.
Full Source
/**
* @file 2_variables_and_data_types.c
* @brief Part 1, Lesson 2: Variables and Data Types
* @author dunamismax
* @date 06-15-2025
*
* This file introduces the fundamental concepts of variables and data types.
* We learn how to store information in memory and print it to the console.
*/
/*
* =====================================================================================
* | - LESSON START - |
* =====================================================================================
*
* In the last lesson, we printed a fixed message. To write useful programs,
* we need a way to store and manipulate data that can change. This is where
* variables come in.
*
* WHAT IS A VARIABLE?
* A VARIABLE is a named storage location in the computer's memory. Think of it
* as a labeled box where you can store a piece of information. You give it a
* name, and you can change the contents of the box (the value) as your
* program runs.
*
* WHAT IS A DATA TYPE?
* A DATA TYPE specifies the type of data that a variable can hold. C is a
* "statically-typed" language, which means you must explicitly tell the
* compiler the data type of every variable you create. This helps the compiler
* allocate the correct amount of memory and ensures you use the variable correctly.
*
* The process of creating a variable is called DECLARATION.
* The process of giving a variable its first value is called INITIALIZATION.
*/
#include <stdio.h>
int main(void)
{
// The main function body is where we'll declare and use our variables.
// --- Part 1: The Integer (int) ---
/*
* An INTEGER is a whole number (no decimal point). The `int` data type is
* used to store them.
*
* Below, we DECLARE a variable named `player_score` of type `int`.
* This tells the compiler: "Reserve a spot in memory for an integer,
* and I will call it `player_score`."
*/
int player_score; // Declaration
/*
* Now we INITIALIZE the variable by assigning it a value using the
* assignment operator `=`.
*/
player_score = 1250; // Initialization
/*
* To print the value of a variable, we use `printf` with a FORMAT SPECIFIER.
* A FORMAT SPECIFIER is a placeholder in the string that tells `printf`
* what type of data to expect and how to format it.
*
* `%d` is the format specifier for a signed decimal integer (`int`).
* The variable `player_score` is passed as a second argument to `printf`,
* and its value will replace `%d` in the output.
*/
printf("Player score: %d\n", player_score);
// You can also declare and initialize a variable in a single statement.
int number_of_lives = 3;
printf("Number of lives: %d\n", number_of_lives);
printf("\n"); // Print a blank line for separation
// --- Part 2: The Double-Precision Floating-Point Number (double) ---
/*
* To store numbers with decimal points, we use FLOATING-POINT types.
* The most common one is `double`, which stands for "double-precision".
* It can store numbers with much greater precision than its counterpart, `float`.
* For this course, we will prefer `double` for all decimal numbers.
*
* `%f` is the format specifier for a `double` (and `float`).
*/
double item_price = 19.99; // Declaration and initialization
printf("The price of the magic sword is: $%f\n", item_price);
/*
* By default, `%f` might print many extra zeros. You can control the
* number of decimal places by modifying the format specifier. For example,
* `%.2f` tells `printf` to print only 2 digits after the decimal point.
*/
printf("The price, formatted neatly, is: $%.2f\n", item_price);
printf("\n");
// --- Part 3: The Character (char) ---
/*
* The `char` data type is used to store a single CHARACTER. This can be
* a letter, a number, or a symbol.
*
* A CHARACTER LITERAL is enclosed in SINGLE quotes (e.g., 'A'). This is
* different from a string literal, which uses double quotes.
*
* `%c` is the format specifier for a `char`.
*/
char player_grade = 'A'; // Declaration and initialization
printf("Player's performance grade: %c\n", player_grade);
// --- Part 4: Putting It All Together ---
/*
* You can print multiple variables in a single `printf` call. Just make
* sure the format specifiers and the variables are listed in the same order.
*/
printf("Summary: Player with %d lives and a grade of '%c' has a score of %d.\n",
number_of_lives, player_grade, player_score);
return 0; // Signal successful execution
}
/*
* =====================================================================================
* | - LESSON END - |
* =====================================================================================
*
* In this lesson, you learned about three fundamental data types:
* - `int` for whole numbers.
* - `double` for numbers with decimal points.
* - `char` for single characters.
*
* You also learned how to DECLARE variables, INITIALIZE them with values, and
* print them using `printf` with FORMAT SPECIFIERS (`%d`, `%f`, `%c`).
*
* HOW TO COMPILE AND RUN THIS CODE:
*
* 1. Open a terminal or command prompt.
* 2. Navigate to the directory where you saved this file.
* 3. Use the GCC compiler to create an executable file:
* `gcc -Wall -Wextra -std=c17 -o 2_variables_and_data_types 2_variables_and_data_types.c`
* 4. Run the executable:
* - On Linux/macOS: `./2_variables_and_data_types`
* - On Windows (WSL): `./2_variables_and_data_types`
*/How to Compile and Run
Runs the lesson in Compiler Explorer (GCC 16.2), nothing to install.
Once, before your first lesson
Install Apple's command line tools. They include the cc compiler: xcode-select --install
New to the terminal? The setup guide walks through it.
Build
$ cc -Wall -Wextra -std=c17 -o 2_variables_and_data_types 2_variables_and_data_types.c
Run
$ ./2_variables_and_data_types
- Run these commands in the folder that holds the lesson's file. Download it as a zip.
Try It Yourself
Practise what this lesson taught. Change a copy of the lesson file, or start a new one.
- Add a
doublevariable for the player's health, set it to 87.5, and print it with one decimal place. - Print
item_pricewith%.0f,%.1fand%.3f. What does the number after the dot control? - Print a
charvariable with%dinstead of%c. The number you see is the character's code.
Try each one yourself before you look at a worked solution. The solutions,
with the reasoning in their comments, are in
exercises/02-variables-and-data-types/
in the repository and
on this lesson's page on the website.
Solutions
One worked solution for each exercise above, with the reasoning in its comments. They stay closed until you open them: have a go first, then compare.
Show the solution to exercise 1 Hide the solution to exercise 1
/**
* @file 1_player_health.c
* @brief Lesson 2, Exercise 1: one possible solution
* @author dunamismax
* @date 11-20-2026
*
* THE EXERCISE:
* Add a `double` variable for the player's health, set it to 87.5, and print
* it with one decimal place.
*/
#include <stdio.h>
int main(void)
{
// 87.5 has a decimal point, so it needs a floating-point type. The
// course uses `double` for those.
double player_health = 87.5;
// `%f` alone would print 87.500000. The `.1` between `%` and `f` asks
// for exactly one digit after the decimal point.
printf("Player health: %.1f\n", player_health);
return 0;
}
/*
* HOW TO COMPILE AND RUN:
* gcc -Wall -Wextra -std=c17 -o 1_player_health 1_player_health.c
* ./1_player_health
*/Show the solution to exercise 2 Hide the solution to exercise 2
/**
* @file 2_decimal_places.c
* @brief Lesson 2, Exercise 2: one possible solution
* @author dunamismax
* @date 11-20-2026
*
* THE EXERCISE:
* Print `item_price` with `%.0f`, `%.1f` and `%.3f`. What does the number
* after the dot control?
*/
#include <stdio.h>
int main(void)
{
double item_price = 19.99;
printf("With %%.0f: %.0f\n", item_price); // 20
printf("With %%.1f: %.1f\n", item_price); // 20.0
printf("With %%.3f: %.3f\n", item_price); // 19.990
return 0;
}
/*
* THE ANSWER
*
* The number after the dot is the PRECISION: how many digits `printf` prints
* after the decimal point.
*
* - `%.0f` prints no decimals at all, not even the point: 20.
* - `%.1f` prints one: 20.0.
* - `%.3f` prints three, adding a zero to fill the space: 19.990.
*
* Notice that 19.99 became 20 and 20.0. `printf` ROUNDS to the nearest value
* it can show; it does not simply cut digits off. The variable itself is
* not changed: `item_price` still holds 19.99, and only the printed text is
* rounded.
*
* (`%%` in the format string prints one real percent sign. Lesson 4 uses it
* too.)
*
* HOW TO COMPILE AND RUN:
* gcc -Wall -Wextra -std=c17 -o 2_decimal_places 2_decimal_places.c
* ./2_decimal_places
*/Show the solution to exercise 3 Hide the solution to exercise 3
/**
* @file 3_character_codes.c
* @brief Lesson 2, Exercise 3: one possible solution
* @author dunamismax
* @date 11-20-2026
*
* THE EXERCISE:
* Print a `char` variable with `%d` instead of `%c`. The number you see is
* the character's code.
*/
#include <stdio.h>
int main(void)
{
char player_grade = 'A';
char digit = '7';
// The same variable, printed two ways. `%c` shows the character and
// `%d` shows the number that is really stored in the variable.
printf("As a character: %c\n", player_grade);
printf("As a number: %d\n", player_grade); // 65
// A digit character is not the number it looks like: '7' is stored as
// the code 55, not as 7.
printf("The character '%c' has the code %d.\n", digit, digit);
return 0;
}
/*
* WHAT IS GOING ON
*
* A computer stores only numbers. A `char` is a small integer, and each
* character is given a number by a table called ASCII: 'A' is 65, 'B' is 66,
* 'a' is 97, '0' is 48 and so on. The format specifier decides how `printf`
* shows that number: `%c` draws the character with that code, `%d` prints
* the code itself.
*
* This is why `'7'` and `7` are different things in C: the first is the
* character with code 55, the second is the integer seven.
*
* HOW TO COMPILE AND RUN:
* gcc -Wall -Wextra -std=c17 -o 3_character_codes 3_character_codes.c
* ./3_character_codes
*/Finished this lesson?
Mark it complete to track your progress through the course.