In the memory of a computer, there are no integers or real numbers. Everything is stored in binary. Type is just a matter of interpreting these binary words. Typing is how to convert binary into integer or decimal numbers. The same data interpreted as an integer or as a float will give different values. Here is an example which is a classic mistake of the beginner developer:
float i=28;
printf ("%d", i);
This program does not display 28. We declare a variable of type float
, but at display time,
we use the %d
format code which will (at best) interpret the variable as an integer.
Note that some compilers will detect the problem by displaying a warning.
The same is true for the storage of characters. In the memory, there are only ones and zeros. It was thus necessary to associate each binary value with a character. It is for this purpose that the ASCII table was created.
The ASCII table is a table that associates a character to each binary value. Initially,
the ASCII table contained 128 characters, mainly english characters. It was then
extended to 256 to include foreign characters, such as the French cedilla ç
.
Different versions co-exist for each language, but normally the first 128
characters are universal. The image below illustrates this ASCII
table (source: Université de Lille 1)
Character encoding has continued to evolve with the introduction of UTF-8 and Unicode. Unicode is designed to encode all the characters of the universal character set, which includes Asian alphabets, emojis ... These modern encodings are not natively supported by C, but it is remarkable to note that these standards have been designed to be backwards compatible with the ASCII table. To know more about it, I strongly invite you to discover this wonderful video of the Computerphile channel:
The char
type, as its name indicates, is intended for storing characters.
A char
occupies 1 byte in memory. With these 8 bits, it is possible to encode
256 values, which allows to store a character from the extended ASCII table.
For example, the code below declares a variable letter
and assigns the character C
to it:
char letter = 'C';
Note the single quotation marks which state that it is a character.
The format code to display a character is %c
. As you may have already understood,
the char
type is frequently used to store small integers. For
displaying a char
in decimal form, the format code %d
must be used. The following example
shows how to display the character C as a character and as an integer:
char letter = 'C';
printf ("As a character : %c\n", letter);
printf ("As an integer : %d\n", letter);
We can see that the letter C
is displayed as a character with the
format code %c
. Moreover, when we display the variable as an integer (%d
),
we can see that the capital letter C has the ASCII code 67. You can check it
in the ASCII table above.
Assume that we already declared a variable i
and that we wish to assign
the letter i
in another variable. How will the compiler differentiate
the variable i
from the character i
?
In C, there is a syntax to differentiate variables from characters: a character is always written between single quotes:
char a = i; // This is variable i
char b = 'i' // This is letter i
char c = 8 // This is number 8
char c = '8' // This is character 8 with ASCII code 56
We will come back to this later, but note that strings (i.e. text) are enclosed in double quotation marks. Do not confuse
printf
function.How would you assign the character simple quote to a variable? You can easily understand that the syntax below is problematic:
char letter = ''';
To solve this problem, C defines special characters, these begin with a backslash. The correct solution for assigning a single quote is :
char letter = '\'';
Here are some of the special characters we will use in the following of this of this course:
Syntax | ASCII Code | Description |
---|---|---|
\t |
9 | tabulation |
\n |
10 | line feed |
\" |
34 | double quote |
\' |
39 | simple quote |
\\ |
92 | backslash |
\% or %% |
37 | percent |
Write a program that declares 3 variables of type char
:
tabulation
initialized with a tabline_feed
initialized with a line breakbackslash
initialized with a backslashFor each variable, display its content in decimal form. Here is the expected output :
Tabulation => 9
Line feed => 10
Backslash => 92
Write a program that displays the character '9' as a character and as an integer.
Here is the expected result:
As a character: 9
As an integer : 57
Check in the ASCII table that the code of the character '9' is 57.
Write a program that displays the character #9 of the ASCII table as a character and as an integer.
Here is the expected result:
As a character: .
As an integer : 9.
Explain this result.
How are the variables stored in the memory?
How much space does a char
need in memory?
char
occupies 1 byte (8 bits) in memory.
Try again...
What can a variable of type char
be used for?
char
type initially dedicated to characters is sometimes used for small integers.
Try again...
What is the ASCII table?
What does ASCII mean?
What does the following program display?
char c = 'P';
printf ("%c", c);
What does the following program display?
char c = 'P';
printf ("%d", c);
What does the following program display?
char c = 80;
printf ("%c", c);
What does the following program display?
char c = 80;
printf ("%d", c);
What does the following program display?
char c = 'w';
printf ("%c ", c);
printf ("%d ", c);
What does the following program display?
char c = 128;
printf ("%d ", c);
char
is between -128 and 127.
Try again...