
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- C - Flexible Array Members in Structures
- C - Structures vs Unions
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Layout
- C - Memory Management
- C - Memory Address
- C - Dynamic Array Resizing
- C - Memory Leaks
- Preprocessors in C
- C - Working of Preprocessor
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- C - File Operations
- C - Formatted Output
- C - getc, getchar, getch, getche
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
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.