0Pricing
C Academy · Lesson

Reading Characters

getchar and putchar.

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;
}

All lessons in this course

  1. Characters as Integers
  2. The ctype Library
  3. Reading Characters
  4. Building Char Utilities
← Back to C Academy