Reading Characters
getchar and putchar.
Reading Characters is a free C Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Reading Single Characters
C provides getchar() to read one character at a time from standard input, and putchar() to write one character to standard output.
Both live in <stdio.h>.
#include <stdio.h>
int main(void) {
putchar('H');
putchar('i');
putchar('\n');
return 0;
}putchar Basics
putchar(c) writes the character c and returns the character written, or EOF on error.
It is the simplest way to output a single byte.
#include <stdio.h>
int main(void) {
for (char c = 'A'; c <= 'E'; c++) {
putchar(c);
}
putchar('\n');
return 0;
}getchar Returns int
getchar() returns an int, not a char. This is important because it must be able to return EOF, a value distinct from any real character.
Always store the result in an int.
#include <stdio.h>
int main(void) {
int c = 'A';
printf("Stored value: %d\n", c);
return 0;
}What Is EOF
EOF means End Of File. getchar() returns it when there is no more input.
It is typically the value -1, which is why getchar uses int so it cannot be confused with byte 255.
#include <stdio.h>
int main(void) {
printf("EOF has value %d\n", EOF);
return 0;
}The Read Loop Pattern
The classic pattern reads characters until EOF. Because there is no interactive input here, the loop ends immediately, but this is the standard shape.
- Assign inside the condition.
- Compare against
EOF.
#include <stdio.h>
int main(void) {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
printf("Reached end of input\n");
return 0;
}Counting Characters
A common task is counting how many characters were read. Increment a counter inside the read loop.
#include <stdio.h>
int main(void) {
int c;
long count = 0;
while ((c = getchar()) != EOF) {
count++;
}
printf("Read %ld characters\n", count);
return 0;
}Echoing in Uppercase
You can transform each character before writing it. Here every character read is converted to uppercase using toupper.
#include <stdio.h>
#include <ctype.h>
int main(void) {
int c;
while ((c = getchar()) != EOF) {
putchar(toupper(c));
}
return 0;
}Counting Lines
Counting newline characters tells you how many lines were read. Test each character against '\n'.
#include <stdio.h>
int main(void) {
int c;
int lines = 0;
while ((c = getchar()) != EOF) {
if (c == '\n') lines++;
}
printf("Lines: %d\n", lines);
return 0;
}Pushing a Char Back
ungetc(c, stdin) pushes one character back onto the input stream so the next read returns it again.
This is useful when you read one character too many while scanning.
#include <stdio.h>
int main(void) {
ungetc('X', stdin);
int c = getchar();
printf("Got back: %c\n", c);
return 0;
}getchar vs scanf
getchar gives you precise byte-level control, while scanf parses formatted input.
For character-by-character processing such as filters and tokenizers, getchar is the right tool.
#include <stdio.h>
int main(void) {
int c = '5';
if (c >= '0' && c <= '9') {
printf("It is a digit: %c\n", c);
}
return 0;
}A Simple Filter Skeleton
Filters read from input, transform, and write to output. This skeleton drops all spaces from the input stream.
#include <stdio.h>
int main(void) {
int c;
while ((c = getchar()) != EOF) {
if (c != ' ') {
putchar(c);
}
}
return 0;
}Quick Check
Test your understanding of reading characters.
Recap
You learned to read and write characters:
getchar()reads one character and returns anint.putchar(c)writes one character.EOFsignals end of input; use thewhile ((c = getchar()) != EOF)loop.ungetccan push a character back onto the stream.
Frequently asked questions
Is the “Reading Characters” lesson free?
Yes — the full text of “Reading Characters” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading Characters”?
getchar and putchar. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading Characters” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Characters as Integers
- The ctype Library
- Reading Characters
- Building Char Utilities