Basic character output with putchar
What you'll learn: How to display a single character on the screen using the simplest output function in C.
The character typewriter
In previous lessons, you might have used printf to display complex sentences. However, at its most fundamental level, a computer views text as a sequence of individual characters. The putchar function is the most direct way to talk to the console. Think of it like a single key-press on a manual typewriter: you provide one character, and the "hammer" strikes the paper once.
The name putchar stands for "put character." It takes exactly one character and sends it to the standard output (usually your monitor).
How to use putchar
To use putchar, you place the character you want to print inside the parentheses. In C, single characters must be wrapped in single quotes (like 'A'), not double quotes.
Here is how you would use it in a program:
#include <stdio.h>
int main() {
// Printing a single letter
putchar('H');
putchar('i');
// Printing a special character (newline) to move to the next line
putchar('\n');
// You can also use the character's numeric ASCII value
putchar(33); // 33 is the code for an exclamation mark '!'
return 0;
}
Why use it?
You might wonder why we bother with putchar when printf can do so much more. The answer is efficiency and intent.
Because putchar only does one thing—printing one byte—it is incredibly fast and uses very little memory. It doesn't need to "scan" your text for special formatting codes like %d or %s. If you are building a system where every byte of memory counts, or if you are writing a loop that processes text one letter at a time (like an animation or a custom text filter), putchar is your best friend.
It also makes your code’s intent clear: you are telling anyone reading your code, "I am specifically outputting a single character here, nothing more."
Key takeaway
Use putchar('x'); to output one single character to the console quickly and efficiently.