Lesson 4.3. putchar

putchar() is a function of the stdio.h library. It is intended to write a single character to the console.

Example

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

printf

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:

printf redirect to an LCD display

Quiz

What does the following code display?

putchar ('a');
Check Bravo! The putchar() function displays as a character. Try again...

What does the following code display?

putchar (a);
Check Bravo! The content of the variable a is displayed as a character. Try again...

What does the following code display?

putchar (97);
Check Bravo! 97 will be displayed as a character. The symbol with ASCII code 97 is the lowercase a. Try again...

How to display "Hello" with the function putchar?

Check Bravo! The putchar function can only display one character at a time. Try again...

See also


Last update : 11/20/2022