Skip to content
StudyFame

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() and putchar() do.
  • Their syntax and return values.
  • Why getchar() returns an int.
  • How to convert a lowercase letter to uppercase.
  • The difference between getchar() and scanf("%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()
JobReads one character (input)Prints one character (output)
Syntaxch = getchar();putchar(ch);
ArgumentsNoneThe character to print
ReturnsThe character read (as int), or EOF at end of inputThe 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

  1. int ch;: stores the character (as an int, see above).
  2. ch = getchar();: waits for input and reads the first character typed. The user typed y and pressed Enter.
  3. putchar(ch);: prints that character.
  4. putchar('\n');: prints a new line, because putchar does 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 yes and press Enter, the buffer holds y, e, s, \n.
  • Each call to getchar() takes the next one character from the buffer. The first call gets y; 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 return EOF).
  • putchar() does not add a new line.
  • Enter is also a character: '\n'.

Common Mistakes Beginners Make

MistakeWhat happensFix
putchar("A");Error / wrong output: that is a stringputchar('A'); with single quotes
getchar(ch);Error: getchar takes no argumentsch = getchar();
Calling getchar twice to read two answersThe second call gets the leftover '\n'Read and ignore the newline, or use scanf(" %c")
putchar(65) expecting "65"Prints AUse printf("%d", 65) to print the number

getchar() and putchar() Practice Questions

  1. What do getchar() and putchar() do?
  2. Why is the return value of getchar() stored in an int?
  3. What is EOF?
  4. What does putchar(66); print?
  5. Write one difference between getchar() and scanf().
Show answers
  1. getchar() reads one character from the keyboard; putchar() prints one character on the screen.
  2. Because it can return any character or the special value EOF, and an int can hold both.
  3. End Of File: a special value (usually −1) meaning there is no more input.
  4. B (ASCII 66).
  5. 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)

  1. getchar() reads:

    A) A word   B) A line   C) One character   D) A number

  2. Which prints the letter A?

    A) putchar("A");   B) putchar('A');   C) putchar(A);   D) getchar('A');

  3. getchar() and putchar() are:

    A) Formatted I/O   B) Unformatted I/O   C) File functions   D) Math functions

  4. The best type to store the result of getchar() is:

    A) char   B) int   C) float   D) double

  5. What does putchar('a' - 32); print?

    A) a   B) A   C) 65   D) Error

Show answers
  1. C: one character.
  2. B: putchar('A');
  3. B: unformatted I/O.
  4. B: int (so it can hold EOF).
  5. 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
Headerstdio.h
TypeUnformatted I/O
Store result inint (may be EOF)
EOFEnd 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.