Lesson 4.1. printf

In this new chapter, we study input/output. It is not about inputs and outputs in the electronic sense of the term (GPIO). It is rather about inputs and outputs of the processor (keyboard, screen, read/write files).

The printf function which allows to display messages in the console is the first one we will study.

printf

printf is a function of the stdio.h library. The first parameter is necessarily a string :

printf ("Hello world");

It is possible to print variables by inserting as many format codes in the string :

printf ("1 variable  : %d", u);
printf ("2 variables : %d , %d", u, v);
printf ("3 variables : %d , %d , %d", u, v, w);

Since the % symbol is reserved for format code, to display a % in a printf, it must be doubled:

printf ("10 %% discount");

Integer formatting

The format codes are presented on this page.

It is possible to format the display by specifying the minimum number of characters just after the %.

// Display at least 5 characters and complete with zeros
// Display 00015
printf ("%05d", 15);
// Display on 2 characters minimum and complete with zeros (useless here)
// Display 126
printf ("%02d", 126);

If there is no zero, we complete with spaces:

// Display at least 5 characters and complete with spaces
// Display 3 spaces before 15 : ___15
printf ("%5d", 15);

Hexadecimal formatting

It is possible to display integers in hexadecimal using the %x format code. The formatting of the %x format code is similar to that of the %d code. Note however:

For example:

// Display in hexadecimal form with different formats
printf ("%%x   : %x\n", val);
printf ("%%X   : %X\n", val);
printf ("%%4x  : %4x\n", val);
printf ("%%04x : %04x\n", val);
printf ("%%08X : %08X\n", val);

Float formatting

For floats, the principle is similar. The point is counted in the character count. You can also specify the number of digits after the decimal point:

// 8 characters minimum and 2 digits after the decimal point (complete with zeros)
// Display 00000.12
printf ("%08.2f", 0.12345)

Also, by omitting the zero, it will be possible to complete with spaces:

// 8 characters minimum and 2 digits after the decimal point (completed with spaces)
// Display ____0.12
printf ("%8.2f", 0.12345)

Exercise 1

Write a program that displays π with 7 digits after the decimal point. Use the constant M_PI from the math.h library.

Exercise 2

Write a program that displays variables with 8 characters filled with spaces:

int a=1, b=1234, c=125;

// Displays a, b and c on 8 characters.
// COMPLETE HERE

Here is the expected display:

a =        1
b =     1234
c =      125

Exercise 3

Write a program that displays the variable x in 32-bit hexadecimal:

int x = 2737;

// Display x in hexadecimal on 32 bits
// COMPLETE HERE

Here is the expected display:

x = 0x00000AB1

Quiz

What is this code displaying?

printf ("%06d", 101);
Check Bravo! 6 characters minimum completed with zeros. Try again...

What is this code displaying?

printf ("%03d", 156987);
Check Bravo! The number to be displayed is composed of more than 3 characters, no zero is added. Try again...

What is this code displaying?

printf ("%6d", 101);
Check Bravo! 6 caractères minimum, complétés avec des espaces. Try again...

What is this code displaying?

printf ("%.2f", 123.456);
Check Bravo! Two digits after the decimal point, rounded to the nearest. Try again...

What is this code displaying?

printf ("%08.2f", 123.456);
Check Bravo! Two digits after the decimal point and 8 characters minimum completed with zeros. Try again...

Which instruction displays x with 4 digits after the decimal point?

Check Bravo! If it is a decimal number, it cannot be the format code %d as format code: %.4f. Try again...

How to display the character % in a printf?

Check Bravo! To display a %, it must be doubled to distinguish it from format codes: %% Try again...

What is this code displaying?

printf ("%06X", 0xacdc);
Check Bravo! %06X displays in hexadecimal (upper case), 6 characters minimum, completed with zeros. Try again...

See also


Last update : 10/04/2023