What is the function of getch(); and conio. h library?

Asked

Viewed 63,630 times

9

What is the function for getch();?

What’s the library for conio.h; what is the use?

For example in a code like this:

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

int main()
{
    printf("Hello world");
    getch();
    return 0;

}
  • 3

    This library is considered obsolete

  • 2

    At first, this function captures a character from the input and returns. However, it is mostly used even to "pause" the terminal. Normally, when you make a program that only prints something on the terminal, Windows opens a window of the cmd.exe, runs the program and then closes the window. getch conium. Thus, the system waits for a user input, which will be dropped (because the getch return value will not be used), and after that the program follows normally.

1 answer

7


The getch() as well as the getche() returns the typed key, widely used in menus with switch. (Difference between the two is that the getch() does not show the key you typed on the screen, already the getche() shows on the screen which key was typed)

Files with extension .h are not libraries, but header files where, among other things, are the prototypes of the functions used.

conio.h is for drawing screen, and is for dos/windows (conium functions are useful for manipulating characters on the screen, specifying character and background color.)

Now you must be wondering:
But what functions can I use with conio.h and what is the relationship with the getch()?

The typical functions used with the conio.h sane clrscr(to clean the screen), gotoxy(position the cursor on the screen at the x and y coordinates), getch(reads a keyboard character and does not display it on the screen), kbhit(test if any key has been pressed, it is necessary to use the command getch or getche before), delline(deletes the line containing the cursor and moves all lines below it one line up), textcolor(changes the text color), textbackground(changes the background color), cprintf (used to print colored text on the screen, it is necessary to specify the color using the textcolor function).

In the case of your code the use of conium was due to the employment of the getch() which is most responsible for using the library. The other commands you can usually use the command system() that will use DOS command for the same purposes.

  • 1

    I use C++ DEV and when I put conium. h some other functions (clrscr and gotoxy) don’t work, which could be?

  • I need to see the code to help you, ask a question on the site and I’ll answer =). Without seeing the code I don’t know what could be wrong. (but by suggestion if you are programming in windows you can use the system("cls");, depending on the application you can use it but it is not recommended if you are using in different types of OS. So it is better to ask a question on the site)

  • 1

    @Wellingtontellescunha there is also a community chat https://chat.stackexchange.com/rooms/11910/overflow

Browser other questions tagged

You are not signed in. Login or sign up in order to post.