getc, getchar, getch, getche in c



In C, getc(), getchar(), getch(), and getche() are functions used to read a character from input and return it as an integer.

Where, the getc() and getChar() are declared in <stdio.h> and getch() and getche() are declared in <conio.h>. Let's discuss each function in-details.

getc() Function in C

In C, the getc() function is used to read a character from a specific file or stream. It returns the next character (as an unsigned char cast to int) from the given file stream, or EOF if the end of file or an error occurs.

Below is the declaration −

int getc(FILE *stream);

This function accepts a single parameter a pointer to a FILE object that specifies an input stream.

Example of getc() - Read a Character from a File

In this example we use the getc() function to display each character from file −

#include <stdio.h>

int main() {
   FILE *file = fopen("example.txt", "r");
   if (file == NULL) {
      printf("Unable to open file.\n");
      return 1;
   }

   char ch;
   printf("Contents of the file:\n");
   while ((ch = getc(file)) != EOF) {
      // Display each character
      putchar(ch);
   }

   fclose(file);
   return 0;
}

Read character one by one from a file until EOF. Following is the output −

Contents of the file:
Line 1: Hello tutorialspoint!
Line 2: Welcome to C programming.

getchar() Function in C

In C, the getchar() function reads the next character from the standard input (stdin) and returns it as an unsigned char cast to an int. It is mainly used for simple input tasks, such as reading characters one by one.

Below is the declaration −

int getchar(void);
If the end-of-file (EOF) is reached or if an error occurs, getchar returns EOF. The EOF is typically defined as -1 in the standard library.

Example of getchar() - Read a Character from Standard Input

In this example, we use a getchar function to read a entered character from standard input −

#include <stdio.h>
int main() {
   char ch;
   printf("Enter a character: ");
   // Reads from keyboard
   ch = getchar();
   printf("You entered: %c\n", ch);
   return 0;
}

Following is the output −

Enter a character: t
You entered: t

getch() Function in C

In C, the getch() is a non-standard function which comes under the <conio.h> header file and used by Turbo C compiler.

It is also used to read the single character from the keyboard. But it does not use any buffer. So the entered character is immediately returned without waiting for the enter key.

getch() is not part of the C standard library or ISO C, nor is it defined by POSIX.

Below is the declaration −

int getch(void);

Return the character read from the keyboard as an int.

Example of getch() - Read a Character without echo

In this following example, we use the getch function to display the hidden entered character immediately −

#include <stdio.h>
#include <conio.h>

int main() {
   char ch;
   printf("Press any key (hidden): ");
   
   // Reads immediately without displaying
   ch = getch();
   printf("\nYou pressed: %c\n", ch);
   return 0;
}

Following is the output −

Press any key (hidden):
You pressed: a

getche() Function in C

In C, the getche() is also a non-standard function which comes under the <conio.h> header file.

This function reads a single character from the keyboard and displays immediately on the output screen without waiting for enter key. Like getch().

Below is the declaration:

int getche(void);

Since the return type is int, the character is returned as an unsigned char cast to int.

On error, it may return EOF (though in practice, DOS-based <conio.h> rarely uses EOF).

Example of getche() - Read a Character with Echo

In this following example, we use the getche function to display the entered character immediately.

#include <stdio.h>
#include <conio.h>

int main() {
   char ch;
   printf("Press any key (visible): ");
   
   // Reads immediately and displays it
   ch = getche();
   printf("\nYou pressed: %c\n", ch);
   return 0;
}

Following is the output −

Press any key (visible): a
You pressed: a

Why We Use getc(), getchar(), getch(), and getche() in C

Following are the reasons to use these functions −

To Read Character as Input

  • getc() − Reads a character from a specific file or input stream. For example, if you are reading from a file, getc() allows you to process it character by character.
  • getchar() − Reads a character from the keyboard (standard input). It is commonly used when you want to take simple input from the user.
  • getch() / getche() − Reads a character immediately from the keyboard, without waiting for the Enter key. This is useful in interactive programs like menus, games, or password fields.

To Control Input Behaviour

  • getchar() / getc() − Buffered input, these functions wait until the user presses Enter. The input is stored in a buffer before the program reads it.
  • getch() − Unbuffered input, do not wait for Enter. They read the character immediately as soon as it is pressed. The character is not displayed on the screen (hidden input).
  • getche() − Unbuffered input, do not wait for Enter. They read the character immediately as soon as it is pressed. The character is displayed on the screen as soon as it is typed.

To Handle both Input and End Conditions

All return an int so they can give either a valid character (as unsigned char) or EOF for error/end of file.

Comparison Table

The below table lists the primary differences between the getc(), getchar(), getch() and getche() in C

Function Header File Purpose Input Source Buffering Return Type
getc() <stdio.h> Reads a character from a given file. File stream Buffered int
getchar() <stdio.h> Reads a character from standard input. Standard input Buffered int
getch() <conio.h> Reads a single character from keyboard. Keybord Unbuffered char
getche() <conio.h> Reads a single character from keyboard. Keybord Unbuffered char

Conclusion

In C, functions such as getc(), getchar(), getch(), and getche() help in reading characters from files or the keyboard. getc() works with files, getchar() accepts keyboard input, and getch() and getche() read keys immediately without waiting for Enter. They all return an integer, allowing them to properly handle both characters and the unusual EOF value.

Advertisements