Tutorials  /  Linux Basics

Using the getch() function in C/C++

Ccentron Redaktion · January 2024 ·3 min read ·Linux Basics, Tutorial

In this article, we’ll take a look at using the getch() function in C/C++.

The getch() function is very useful if you want to read a character input from the keyboard.

While this is not a part of the C standard, this is still a POSIX C function. So, we can still use this function from Windows / Linux / Mac.

Let’s take a look at using this function, using a few examples.

Basic Syntax of getch() in C/C++

This function takes in a single character from the standard input (stdin), and returns an integer.

This is there as part of the header file, so you must include it in your program.

Code
#include <conio.h>
int getch();

The function does not take any parameters.

Here, getch() returns the ASCII value of the character read from stdin.

For example, if we give the character ‘0’ as input, it will return the ASCII value of ‘0’, which is 49.

Now, in C / C++, we can directly convert a character to an integer. So on typecasting, the ASCII value 49 will be cast to the char value of ‘0’!

Let’s now look at some examples.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Using getch() in C/C++ - Some Examples

As a simple example, let’s first look at reading a single character.

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

int main() {
    char ch = getch();
    printf("Received Input: %c\n", ch);
    return 0;
}
Sample Output

Received Input: a

I got this output, after I typed ‘a’ on my keyboard. Let’s now look at a program, which waits for 5 characters from the keyboard.

Note that getch() will NOT display the input from the keyboard. So, when you type the input, the cursor won’t show the input.

Let’s display the complete string only after we get all 5 characters

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

int main() {
    // Set op = {0, 0, 0, 0, 0, 0} = '\0\0\0\0\0\0' string
    char op[6] = {0};
    for (int i=0; i<5; i++) {
        op[i] = getch();
    }
    printf("Received 5 character Input: %s\n", op);
    return 0;
}
Output

Received 5 character Input: Hello

Indeed, when I typed “Hello”, I did get the output correctly.

Notice that I have 6 characters in my output string, since we need to reserve 1 byte for ‘\0’. So op is “Hello\0”.

Conclusion

In this article, we learned about using the getch() function in C / C++ to receive character input from the keyboard. In this article, we have learned about the use of the getch() function in C / C++ to receive character input from the keyboard. If you want to learn more about efficient programming and professional IT solutions, register now in the ccenter or contact our sales team for a personalized consultation.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren