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
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");
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);
0
means we complete with zeros;5
specify at least 5 characters.// 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);
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:
%x
displays the letters in lower case ; %X
displays the letters in upper case.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);
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)
Write a program that displays π with 7 digits after the decimal point. Use the
constant M_PI
from the math.h
library.
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
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
What is this code displaying?
printf ("%06d", 101);
What is this code displaying?
printf ("%03d", 156987);
What is this code displaying?
printf ("%6d", 101);
What is this code displaying?
printf ("%.2f", 123.456);
What is this code displaying?
printf ("%08.2f", 123.456);
Which instruction displays x
with 4 digits after the decimal point?
How to display the character %
in a printf?
What is this code displaying?
printf ("%06X", 0xacdc);
%06X
displays in hexadecimal (upper case), 6 characters minimum, completed with zeros.
Try again...