getch() Function in C and C++ With Examples

The getch() function in C and C++ is used to read a single character from the keyboard without waiting for the Enter key. It is available in the <conio.h> header file and is mainly supported by older compilers such as Turbo C.

Unlike scanf(), the getch() function does not display the entered character on the screen. Because of this behavior, it is commonly used in password systems, menu-driven programs, and keyboard input applications.

Syntax of getch()

int getch();

Required Header File

#include <conio.h>

The <conio.h> header file is used for console input and output functions in older C programming environments.

Example Program Using getch()

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

int main() {
    char ch;

    printf("Press any key: ");

    ch = getch();

    printf("\nYou pressed: %c", ch);

    return 0;
}

Output

Press any key: A
You pressed: A

How getch() Works

The getch() function reads a single character directly from the keyboard without waiting for the Enter key. It also does not display the typed character on the console screen.

This function is mainly used when immediate keyboard input is required.

Advantages of getch()

  • Reads input instantly without pressing Enter
  • Useful for password input systems
  • Simple to use in keyboard-based applications
  • Commonly used in menu-driven programs

Disadvantages of getch()

  • Not part of the standard C library
  • Unsupported in many modern compilers
  • Mainly works in Turbo C and DOS-based environments
  • Not recommended for modern C programming projects

Difference Between getch() and getchar()

Featuregetch()getchar()
Requires Enter KeyNoYes
Displays CharacterNoYes
Standard C FunctionNoYes
Header Fileconio.hstdio.h

Important Note About getch() Function

The getch() function is a non-standard function in C and C++. It is available in the <conio.h> header file and is mainly supported by older DOS-based compilers such as Turbo C and some Windows compilers.

Modern compilers like GCC may not support getch() directly because it is not part of the official ANSI C standard library.

For modern C programming, developers usually prefer standard input methods such as getchar() or platform-specific alternatives.

Compiler Support for getch()

CompilerSupport Status
Turbo CSupported
GCCNot Supported
Code::Blocks (MinGW GCC)Usually Not Supported
Visual StudioPartial Support
DOS CompilersSupported

Frequently Asked Questions

What is getch() in C?

The getch() function is used to read a single character from the keyboard without waiting for the Enter key.

Why is getch() used?

It is mainly used for instant keyboard input and password-related programs.

Is getch() a standard C function?

No, getch() is not a standard C function. It belongs to the <conio.h> header file.

Which header file is required for getch()?

The <conio.h> header file is required for using getch().

Does getch() work in GCC compiler?

No, GCC compiler does not support getch() directly because it is not part of the standard C library.

Conclusion

The getch() function is a simple keyboard input function used in older C and C++ programming environments. Although it is useful for reading characters instantly without pressing Enter, it is not part of the standard C library and may not work in modern compilers.

Beginners can still learn getch() to understand basic console input handling, especially while studying older C programming concepts.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top