Back to C Programming
2026-07-125 min read

C - getc, getchar, getch, getche

Learn C - getc, getchar, getch, getche step by step with clear examples and exercises.

Why This Matters

In C programming, understanding the functions getc, getchar, getch, and getche is crucial for handling user input and reading characters from files or other sources. These functions are essential for interactive programs, where you need to take user inputs in real-time, or for reading data from a file line by line. They can also help debugging your code by allowing you to see the values that variables hold during runtime.

Prerequisites

Before diving into the functions getc, getchar, getch, and getche, make sure you have a good understanding of the following concepts:

  • Basic C syntax, including variables, operators, loops, and control structures
  • File handling in C, such as opening, reading, writing, and closing files
  • Understanding the difference between standard input/output streams (stdin, stdout, stderr)

Core Concept

getc Function

The getc function reads a single character from any stream (file or standard input). It returns an int, which represents the ASCII value of the read character if successful, or EOF (end-of-file indicator) if the end of the file is reached or an error occurs.

int getc(FILE *stream);

Here's a simple example using getc to read characters from standard input:

#include <stdio.h>

int main() {
int ch;
while ((ch = getc(stdin)) != EOF) {
putchar(ch); // print the character
}
return 0;
}

In this example, we read characters from standard input (stdin) using getc and store them in the variable ch. As long as the end of the file is not reached (ch != EOF), we print each character to the console using putchar(ch).

getchar Function

The getchar function reads a single character from standard input and returns it as an int. It's declared in the `` library:

int getchar(void);

Here's the same example using getchar instead of getc:

#include <stdio.h>

int main() {
int ch;
while ((ch = getchar()) != EOF) {
putchar(ch); // print the character
}
return 0;
}

getch and getche Functions

The getch and getche functions are not part of the standard C library, but they are commonly found in non-standard libraries like conio.h. These functions read a single character from the console (keyboard) without displaying it on the screen. This can be useful for reading characters without interfering with the program's output.

#include <conio.h>

int main() {
int ch;
while ((ch = getch()) != EOF_KEY) { // EOF_KEY is defined in conio.h
// ... process character here
}
return 0;
}

In the example above, we use getch to read characters from the console without displaying them on the screen. The loop continues until the user presses a key corresponding to EOF_KEY (usually Ctrl+Z or Ctrl+D).

getche Function

The getche function is similar to getch, but it displays the character on the console after reading it from the keyboard. This can be useful for debugging purposes, as you can see the characters being read in real-time:

#include <conio.h>

int main() {
int ch;
while ((ch = getche()) != EOF_KEY) { // EOF_KEY is defined in conio.h
printf("Read character: %c\n", ch); // print the character
}
return 0;
}

In this example, we use getche to read characters from the console and display them using printf. The loop continues until the user presses a key corresponding to EOF_KEY (usually Ctrl+Z or Ctrl+D).

Worked Example

Let's create a simple program that reads characters from standard input using getc and performs basic operations on them.

#include <stdio.h>

int main() {
int ch, sum = 0;
while ((ch = getc(stdin)) != EOF) {
if (ch >= '0' && ch <= '9') { // check if character is a digit
sum += ch - '0'; // add the digit value to the sum
} else {
printf("Invalid input: %c\n", ch);
}
}
printf("Sum of digits: %d\n", sum);
return 0;
}

In this example, we read characters from standard input using getc. If the character is a digit (ASCII values between '0' and '9'), we add its value to the sum variable. Otherwise, we print an error message indicating that the input is invalid. After processing all the input, we print the sum of the digits found.

Common Mistakes

  1. Not checking for EOF: It's essential to check if the end of the file or stream has been reached using ch != EOF. Failing to do so can lead to infinite loops or unexpected behavior.
  2. Incorrectly handling non-digits: When processing digits, remember to handle other characters (like letters, symbols, and whitespace) appropriately. In the example above, we print an error message for non-digit inputs.
  3. Forgetting to include the necessary header files: Make sure you include ` for getc, getchar, and related functions, as well as for getch and getche`.
  4. Using getch or getche in a console that doesn't support them: Some consoles (like Linux terminal) don't support the non-standard conio.h library, so using getch or getche may cause errors or unexpected behavior.

Practice Questions

  1. Write a program that reads characters from standard input using getchar and counts the number of vowels (a, e, i, o, u) found in the input.
  2. Modify the worked example to read characters from a file named numbers.txt, which contains only digits separated by spaces or newlines. The program should calculate the sum of all numbers found in the file.
  3. Write a program that uses getch to read a password (hidden from the screen) and checks if it matches a predefined password. If the passwords match, print "Access granted." Otherwise, print "Access denied."

FAQ

  1. What happens if I try to use getc or getchar on a file that doesn't exist?: Attempting to read from a non-existent file using getc or getchar will result in an error, as the function will reach the end of the file (EOF) immediately.
  2. Why don't I see the characters being read when using getch or getche?: The console doesn't display the characters being read by getch or getche, as they are read without being output to the screen. You can print them using other functions like printf.
  3. Can I use getc, getchar, getch, or getche with files other than standard input and output?: Yes, you can use these functions with any stream (file or device) by passing a pointer to the file's FILE structure as an argument.
  4. Why aren't getch and getche part of the standard C library?: These functions are not included in the standard C library because they rely on non-standard libraries like conio.h, which may not be available on all systems or platforms.