putchar()
is a function of the stdio.h
library. It is intended to
write a single character to the console.
Here are some examples of charcters displayed with putchar
:
putchar ('H'); // Upper-case H
putchar ('i'); // Lower-case i
putchar ('\t'); // Tabulation
putchar ('\\'); // backslash
putchar (65); // ASCII 65 = upper-case A
putchar('\n'); // Line feed
The printf()
function calls the putchar()
function. For example, when we display
Hello
, the function calls (in a simplified way) the following sequence:
putchar('H');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
On some embedded systems, in particular microcontrollers, we may have to write our own
putchar
function. This one will display the character on the output (LCD screen,
serial terminal, USB link, wireless transmission ...) programmed in the function.
It will then be possible to redirect the flow from the printf
function to putchar
, and thus to display on any support.
printf ("Hello World!");
displays after redirection:
What does the following code display?
putchar ('a');
putchar()
function displays as a character.
Try again...
What does the following code display?
putchar (a);
a
is displayed as a character.
Try again...
What does the following code display?
putchar (97);
How to display "Hello" with the function putchar
?