Lesson 2.4. Characters

Introduction

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.

ASCII table

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) ASCII table

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:

Char type

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.

Format code

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.

Character syntax

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

Special characters

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

Exercises

Exercise 1

Write a program that declares 3 variables of type char :

For each variable, display its content in decimal form. Here is the expected output :

Tabulation => 9
Line feed => 10
Backslash => 92

Exercise 2

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.

Exercise 3

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.

Quiz

How are the variables stored in the memory?

Check Bravo ! Memory is a huge succession of zeros and ones. Try again...

How much space does a char need in memory?

Check Bravo ! A char occupies 1 byte (8 bits) in memory. Try again...

What can a variable of type char be used for?

Check Bravo! The char type initially dedicated to characters is sometimes used for small integers. Try again...

What is the ASCII table?

Check Bravo! The ASCII table is a mapping table from characters to binary, decimal or hexadecimal. Try again...

What does ASCII mean?

Check Good job! I hope you got it right the first time! Try again...

What does the following program display?

char c = 'P';
printf ("%c", c);
Check Bravo! It displays the variable as a character. Try again...

What does the following program display?

char c = 'P';
printf ("%d", c);
Check Bravo ! The ASCII code of the character 'P' is 80. Try again...

What does the following program display?

char c = 80;
printf ("%c", c);
Check Bravo ! The character associated with the ASCII code 80 is 'P'. Try again...

What does the following program display?

char c = 80;
printf ("%d", c);
Check Bravo! 80 displayed as an integer => simply 80. Try again...

What does the following program display?

char c = 'w';
printf ("%c ", c);
printf ("%d ", c);
Check Bravo! The first is displayed as a character, the second as an integer. Try again...

What does the following program display?

char c = 128;
printf ("%d ", c);
Check Bravo! Keep in mind that a char is between -128 and 127. Try again...

See also


Last update : 09/15/2023