Big Tuto: Program with the C language
From theory to practice
Chapter 5: Variables
Tutorial presented by: The Ludovyc and Jérémie F. Bellanger (Jay)
The Ludovyc
Publication: March 30, 2014
Last revision: March 9, 2025
In this chapter, we're going to cover variables! And as you'll see, it's going to be heavy stuff, and that's cool! ![]()
To begin with, you should know that a variable can be signed (i.e. negative or positive: for example -256 or +256) or unsigned (in this case, we will consider it absolute). ![]()
But what is the point, you might ask? ![]()
Well, quite simply, with the same number of bits, we can either handle as many negative numbers as positive numbers, or handle double the absolute numbers. If we take the example of char : a char takes a slot in memory, that is to say a byte or 8 bits. From a general point of view, the most significant bit (MSB) is used for the sign so with a char, we can count from -128 to 127 if it is signed or from 0 to 255 if it is unsigned.
Depending on usage, it will therefore be up to you to determine the most judicious choice, knowing that all variables are signed by default, and that it must be indicated if you want them not to be signed (= unsigned), like this:
char c = 127;
unsigned char c = 255;
I will now provide you with a short list of the different types of variables with their most commonly used printf() parameters:
Integer types
char |
|
| Description |
Basic variable for small numbers, booleans, and letters. Often referred to as "c" in code. |
| Size in byte(s) | 1 (size unchanged on all types of machine) |
| Declaration (code) | (unsigned) char c = 8; |
| Default values | -128 to 127 |
| Values, unsigned | 0 to 255 (unsigned) |
| Notation printf() |
%d for decimal value %c to display it as a letter |
short |
|
| Description |
Variable used for integer values. |
| Size in byte(s) | 2 (size unchanged on all types of machine) |
| Declaration (code) | (unsigned) short s = 16; |
| Default values | -32768 to 32767 |
| Values, unsigned | 0 to 65535 (unsigned) |
| Notation printf() |
%d for decimal value |
int |
|
| Description |
Most commonly used variable. For integer values. |
| Size in byte(s) | 2 or 4 |
| Declaration (code) | (unsigned) int i = 32; |
| Default values | -2147483648 to 2147483647 (on 4 bytes) |
| Values, unsigned | 0 to 4294967295 (unsigned, 4 bytes) |
| Notation printf() |
%d for decimal value %p for hexadecimal value |
long |
|
| Description |
Variable for very large integer values. |
| Size in byte(s) | 4 or 8 |
| Declaration (code) | (unsigned) long l = 64; |
| Default values | -4x10e16 to 3x10e16 (roughly, for 8 bytes) |
| Values, unsigned | 0 to 7x10e16 (unsigned, for 8 bytes) |
| Notation printf() |
%ld |

Rational types
float |
|
| Description |
Commonly used variable for floating point numbers. |
| Size in byte(s) | 4 |
| Declaration (code) | float f = 12.8; |
| Stayed | simple 6 |
| Values | 3.4x10e-38 to 3.4x10e38 |
| Notation printf() |
%f |
double |
|
| Description |
Less commonly used variable than float but more precise and with a wider domain. |
| Size in byte(s) | 8 |
| Declaration (code) | double d = 51.2; |
| Stayed | double 15 |
| Values | 1.7x10e-308 to 1.7x10e308 |
| Notation printf() |
%f |
Here we go!
Now to get you started with all these variables, let's move on to some practical exercises! ![]()
Exercise 1
Your mission, if you accept it (and I really hope so!
), will be to write a small program that will display all the variables we have just seen using the printf() function. Don't forget to put a carriage return (\n) after each value, except the last one, otherwise they will be clumped together! ![]()
The purpose of this exercise is to familiarize you with all these variables and to understand how to display them in the console. And don't worry, it's very quick to do! Here is the beginning of the code, to help you:
#include <stdio.h>
int main(){
char c = 8;
printf("%d\n", c);
printf("%c\n", c);
}
Don't worry if you don't see 'c' displayed in letters, this is normal.
Correction of the exercise
#include <stdio.h>
int main(){
char c = 8;
printf("%d\n", c);
printf("%c\n", c);
short s = 16;
printf("%d\n", s);
int i = 32;
printf("%d\n", i);
printf("%p\n", i);
long l = 64;
printf("%ld\n", l);
float f = 12.8;
printf("%f\n", f);
double d = 51.2;
printf("%f\n", d);
}
Challenge exercise
Write a program that displays the different sizes of the variable types seen in this chapter.
Challenge 1: Do not use a variable for size storage.
Challenge 2: Use the most suitable variable type for different storage variables, and use them for display.
Challenge 3: Use only one variable with the most suitable type for both storage and display.
Note: the sizeof() function returns the size of the variable type specified as a parameter. ![]()
Examples to help you:
//Without storage variable
printf ( "%d" , sizeof(char) ) ;
//With a storage variable
int i = sizeof(char) ;
printf("%d\n", i);
//Tips
char c = sizeof(char);
printf("%d\n", c);
c = sizeof(double);
printf("%d", c);
I will give you the correction of this exercise in the next chapter! See you soon! 😆

English
Français