Using Library Functions: #include, getch(), and the Input Buffer

Your addition program prints its welcome message and then rushes straight on to the first question. It would be friendlier to pause with “Press any key to continue…” and wait. C has no built-in command for that, but libraries do. In this lesson you try to call a function that does not exist, find out how C locates the functions you use, and meet a sneaky problem with the keyboard.

In this lesson you will

  • See what happens when you call a function that is not declared anywhere
  • Use #include to bring in a library’s declarations
  • Use getch() from conio.h to wait for a key press, and learn why it only works on Windows
  • Compare input that echoes with input that does not
  • Watch a held-down key leak through the input buffer into scanf

Try it: guess a function name

Add a “Press any key” line to WelcomeMessage, followed by a call to a function that sounds right, ReadKey():

#include <stdio.h>
#include <stdlib.h>

void WelcomeMessage(void);

int main()
{
    WelcomeMessage();

    float x = -9999;
    float y = -9999;

    printf("Please enter a value for x:\n");
    scanf("%f", &x);
    printf("Please enter a value for y:\n");
    scanf("%f", &y);

    printf("The sum of %.2f and %.2f is %.2f\n", x, y, x + y);
    return 0;
}

void WelcomeMessage(void)
{
    printf("Welcome to the addition program\n\n");
    printf("Press any key to continue...\n\n");
    ReadKey();
}

Break it on purpose: call a function that does not exist

Build it. With GCC 14 the build stops with:

main.c:26:5: error: implicit declaration of function 'ReadKey' [-Wimplicit-function-declaration]

Older compilers, including the GCC bundled with many Code::Blocks installs, may only warn about the implicit declaration. The build then fails one step later, when the linker cannot find the function’s code anywhere:

undefined reference to `ReadKey'
collect2: error: ld returned 1 exit status

ReadKey exists in some other languages, but not in standard C. (In Pascal and in C#, for example, there is a function called ReadKey.) Guessing names does not work. You need to know which library a function lives in.

Including a random header does not help either. Add #include <string.h> at the top and build again, and you get the same error. string.h declares functions for working with text, and none of them is called ReadKey.

Try it: getch() from conio.h

On Windows, the function you want is getch(). It waits for a single key press and returns it. It is declared in a header file called conio.h (“console input/output”). Add the #include line and replace ReadKey():

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

void WelcomeMessage(void);

int main()
{
    WelcomeMessage();

    float x = -9999;
    float y = -9999;

    printf("Please enter a value for x:\n");
    scanf("%f", &x);
    printf("Please enter a value for y:\n");
    scanf("%f", &y);

    printf("The sum of %.2f and %.2f is %.2f\n", x, y, x + y);
    return 0;
}

void WelcomeMessage(void)
{
    printf("Welcome to the addition program\n\n");
    printf("Press any key to continue...\n\n");
    getch();
}

Build and run. The program stops after the welcome message. Press any key, for example j, and it carries on to the first question:

Welcome to the addition program

Press any key to continue...

Please enter a value for x:

Notice that the j you pressed did not appear on the screen, and you did not have to press Enter. Compare that with scanf, where everything you type is shown (echoed) and nothing happens until you press Enter. getch() reads one key straight away, without echo. That makes the pause feel smooth.

Windows only. conio.h and getch() are not part of standard C. They come with Windows compilers such as the MinGW GCC that Code::Blocks installs. On macOS or Linux the build fails with fatal error: conio.h: No such file or directory. A portable alternative is the standard function getchar() from stdio.h. It echoes, and it waits for Enter, so change the message to “Press Enter to continue…”.

What just happened

Functions such as printf, scanf and getch are library functions. Someone else wrote and compiled them, and they are supplied with your compiler. To use one, the compiler needs its declaration, its prototype, exactly as with your own functions in the earlier lessons. Libraries supply their prototypes in header files (.h files). The line #include <conio.h> tells the preprocessor to paste the contents of that header into your file before compiling, so the compiler knows what getch is. Then the linker joins your code to the library’s compiled code.

That explains both errors with ReadKey. There was no declaration, so the compiler complained. On older compilers that let it through, there was no compiled code to link to, so the linker complained instead. It also explains why stdio.h has been at the top of every program since the first lesson: it declares printf and scanf.

Try it: hold the key down

Break it on purpose: a key press that leaks

Run the getch version again. This time, at the “Press any key” pause, press the 8 key and hold it down for a moment before letting go. Observe: the first question appears with 8s already typed in, and they become the start of the value for x:

Please enter a value for x:
88815

Here the user held down 8, then typed 15 and pressed Enter, so x became 88815.

Holding a key makes it repeat. getch() takes only the first 8. The repeats are stored in the input buffer, a waiting area for keyboard input that has not been read yet. When scanf runs next, it reads from that same buffer, finds the 8s waiting, and treats them as the start of the number.

The simplest fix is to tap the key rather than hold it. On Windows you can also throw away any extra key presses straight after the getch(), using kbhit() (also from conio.h), which reports whether a key is waiting:

    getch();
    while (kbhit())   /* while more key presses are waiting... */
    {
        getch();      /* ...read them and throw them away */
    }

Key terms

  • Library function: a ready-made function supplied with the compiler, such as printf or getch.
  • Header file (.h): a file of declarations (prototypes) for a library’s functions.
  • #include: a preprocessor instruction that inserts a header file into your code before compiling.
  • Preprocessor: the step before compiling that handles lines beginning with #.
  • Linker: the step after compiling that joins your code with library code to make the executable.
  • Echo: showing typed characters on the screen as they are entered.
  • Input buffer: where keyboard input waits until the program reads it.
  • Non-standard: not part of the C standard, so only available on some systems (like conio.h).

Quick check

  1. The build reports “implicit declaration of function” for a library function. What is the usual fix?

    Show answer

    #include the header that declares it, for example stdio.h for printf.

  2. Name two differences between getch() and scanf("%f", &x) from the user’s point of view.

    Show answer

    getch() does not echo the key, and it does not wait for Enter. scanf echoes what you type and waits for Enter.

  3. Your friend on a Mac gets fatal error: conio.h: No such file or directory. Why?

    Show answer

    conio.h is non-standard and Windows-only. Use a standard function such as getchar() instead.

  4. Why can holding down a key at a getch() pause change the value that the next scanf reads?

    Show answer

    The repeated key presses wait in the input buffer, and scanf reads them as the start of its input.

Optional: a recorded demonstration of these steps starts around 38:30. getch() and the input-buffer problem follow from about 42:30.

Part of the Introduction to C series. Next: Arrays in C: Storing Many Values Under One Name