getchar() and putchar() in C
Sometimes you only need one character: a yes/no answer (y or n), a grade (A, B, C), or a menu choice.
C has two simple functions for this: getchar() reads one character, and putchar() prints one character. In this lesson you will learn how they work and when to use them.
What You Will Learn
- What
getchar()andputchar()do. - Their syntax and return values.
- Why
getchar()returns anint. - How to convert a lowercase letter to uppercase.
- The difference between
getchar()andscanf("%c").
getchar() and putchar() in C Explained
Exam definition: getchar() reads a single character from the keyboard and returns it. putchar() prints a single character on the screen. Both are unformatted I/O functions declared in stdio.h.
getchar() | putchar() | |
|---|---|---|
| Job | Reads one character (input) | Prints one character (output) |
| Syntax | ch = getchar(); | putchar(ch); |
| Arguments | None | The character to print |
| Returns | The character read (as int), or EOF at end of input | The character printed, or EOF on error |
Memory trick: getchar = get (take) a char from the keyboard. putchar = put (place) a char on the screen.
Why is the result of getchar() stored in an int?
getchar() can return any character or a special value called EOF (End Of File, usually −1), which means "no more input". An int can hold both, so it is safest to store the result in an int.
Syntax of getchar() and putchar()
int ch = getchar(); /* read one character */
putchar(ch); /* print one character */
putchar('A'); /* print the letter A */
putchar('\n'); /* print a new line */
Example 1: Read a Character and Print It Back
#include <stdio.h>
int main(void)
{
int ch;
printf("Do you like C? (y/n): ");
ch = getchar();
printf("You typed: ");
putchar(ch);
putchar('\n');
return 0;
}
Output
Do you like C? (y/n): y You typed: y
Code Explanation
int ch;: stores the character (as anint, see above).ch = getchar();: waits for input and reads the first character typed. The user typedyand pressed Enter.putchar(ch);: prints that character.putchar('\n');: prints a new line, becauseputchardoes not add one by itself.
How getchar() Works
- What you type goes into the input buffer, and only after you press Enter is it available.
- If you type
yesand press Enter, the buffer holdsy,e,s,\n. - Each call to
getchar()takes the next one character from the buffer. The first call getsy; the rest wait for the next calls. - Even the Enter key is a character:
'\n'. This matters when you read several inputs.
Example 2: Convert a Lowercase Letter to Uppercase
#include <stdio.h>
int main(void)
{
int ch;
printf("Enter a lowercase letter: ");
ch = getchar();
printf("Uppercase: ");
putchar(ch - 32); /* 'a' is 97, 'A' is 65: difference 32 */
putchar('\n');
return 0;
}
Output
Enter a lowercase letter: a Uppercase: A
In ASCII, every lowercase letter is exactly 32 more than its uppercase letter. So 'a' - 32 = 97 − 32 = 65 = 'A'. (The library function toupper() does this safely; you will see it later.)
Important Points to Remember
getchar()reads one character;putchar()prints one character.- Both are unformatted I/O functions in
stdio.h. - Store getchar's result in an
int(it can returnEOF). putchar()does not add a new line.- Enter is also a character:
'\n'.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
putchar("A"); | Error / wrong output: that is a string | putchar('A'); with single quotes |
getchar(ch); | Error: getchar takes no arguments | ch = getchar(); |
| Calling getchar twice to read two answers | The second call gets the leftover '\n' | Read and ignore the newline, or use scanf(" %c") |
putchar(65) expecting "65" | Prints A | Use printf("%d", 65) to print the number |
getchar() and putchar() Practice Questions
- What do
getchar()andputchar()do? - Why is the return value of
getchar()stored in anint? - What is
EOF? - What does
putchar(66);print? - Write one difference between
getchar()andscanf().
Show answers
getchar()reads one character from the keyboard;putchar()prints one character on the screen.- Because it can return any character or the special value
EOF, and anintcan hold both. - End Of File: a special value (usually −1) meaning there is no more input.
B(ASCII 66).getchar()reads only one character and is unformatted;scanf()can read many values of different types and is formatted.
Coding Exercise
Read one character from the user and print it three times on one line, then print its ASCII code using printf.
Show solution
#include <stdio.h>
int main(void)
{
int ch;
printf("Enter a character: ");
ch = getchar();
putchar(ch);
putchar(ch);
putchar(ch);
putchar('\n');
printf("ASCII code: %d\n", ch);
return 0;
}
Enter a character: K KKK ASCII code: 75
Quick Quiz: getchar() and putchar() (MCQ)
-
getchar()reads:A) A word B) A line C) One character D) A number
-
Which prints the letter A?
A)
putchar("A");B)putchar('A');C)putchar(A);D)getchar('A'); -
getchar() and putchar() are:
A) Formatted I/O B) Unformatted I/O C) File functions D) Math functions
-
The best type to store the result of
getchar()is:A)
charB)intC)floatD)double -
What does
putchar('a' - 32);print?A) a B) A C) 65 D) Error
Show answers
- C: one character.
- B:
putchar('A'); - B: unformatted I/O.
- B:
int(so it can hold EOF). - B:
A
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| getchar() | Reads one character, no arguments |
|---|---|
| putchar(ch) | Prints one character |
| Header | stdio.h |
| Type | Unformatted I/O |
| Store result in | int (may be EOF) |
| EOF | End Of File, usually −1 |
| 'a' − 'A' | 32 |
Frequently asked questions
- Why does getchar() return int instead of char?
- So it can return every character and also EOF, which is outside the char range.
- Does putchar() add a new line?
- No. You must print
'\n'yourself. - Difference between getchar() and scanf("%c")?
- Both read one character, but getchar is simpler and unformatted; scanf is formatted and can read many types.
- How do you convert lowercase to uppercase manually?
- Subtract 32 from the ASCII value:
'a' - 32 = 'A'.
Remember
- get = take from keyboard; put = place on screen.
- Single quotes for one character:
putchar('A').
Next Topic
Next lesson: gets(), puts() and fgets() in C. You will learn how to read and print a full line of text, including spaces.