12

I have a simple program:

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

int main(void){
    printf("Hello world!\n");

    getch();

    return 0;
}

Even though I get the warning

implicit declaration of function 'getch'

the program runs fine. Do I miss something? And if I do, why does the program work OK?

5
  • 1
    getch() or getchar()? Apr 13, 2015 at 18:15
  • 9
    The non-standard getch() is in conio.h (also non-standard), which you didn't include. It "works" because the return type is compatible with the implicit return type assumed by the compiler: int. In short, you were (un)lucky. Consider using getchar() instead.
    – WhozCraig
    Apr 13, 2015 at 18:16
  • 2
    Thank you @WhozCraig for your comment (if it was an answer I whould gladly accept)
    – jim jim
    Apr 13, 2015 at 18:26
  • I scratch my head for the down vote. Is anything wrong with the question? please help me improve downvoter.
    – jim jim
    Apr 13, 2015 at 18:36
  • 1
    The tooltip hint for question voting includes the comment "...shows research effort", perhaps that was it (not me). MSVC compiler warning is more helpful: "warning C4013: 'getch' undefined; assuming extern returning int" Apr 13, 2015 at 18:48

3 Answers 3

15

You get the warning

implicit declaration of function 'getch'

because you have not include any header that declares getch. No such function is declared in the standard headers <stdio.h> or <stdlib.h>.

In fact, there is no function named getch in any standard C header.

Prior to the C99 standard, the C language permitted calls to functions with no visible declaration. Such a call would in effect create an implicit declaration of a function returning int and taking arguments of whatever (promoted) type you actually passed.

Depending on this has never been a good idea. You should always have a proper #include directive for the header that declares any library function you use in your program.

C99 dropped the "implicit int" rule and made any call to a function with no visible declaration a constraint violation, requiring a diagnostic (That diagnostic is permitted to be a non-fatal error.)

If you're compiling on Windows, if I recall correctly, there's a getch() function declared in <conio.h>. If you want to use that function, you need to add #include <conio.h> to your program.

I do not recommend doing this; using getch() is unnecessary and makes your program non-portable. Some Windows development environments make it difficult to run "console programs" (programs that print to standard output rather than creating a GUI); often running such a program creates a temporary window that's destroyed as soon as the program finishes. Calling the standard getchar() function is another way to keep the window from vanishing. Or you can execute the program from a command prompt, and its output will appear in your current command window.

If you're compiling on a UNIX-like system, there's another function called getch(), declared in <curses.h>. I can compile and execute your program on Linux if I add -lcurses to the compiler command line. But you shouldn't use that getch() function if you haven't first set up the curses environment, and it's fairly clear you don't want to do that.

Ideally, the classic "hello world" program should be just:

#include <stdio.h>
int main(void) {
    printf("Hello world!\n");
    return 0;
}

How you get that to run and let you see the output depends on your environment (which you haven't told us about).

3
  • I am on a windows 7 machine, code::blocks ide, gcc compiler. I just want the program to stop and press a key to exit. getchar() works fine but I have to press enter also. Thank you for your time and your detailed answer :)
    – jim jim
    Apr 13, 2015 at 19:32
  • 2
    @jimjim: You can just press <enter>, no need to type another character first. You can try something like printf("Type <return> to quit: "); fflush(stdout); getchar();. Or you can run the program from a command window and let it run to completion. (There may be a way to invoke the Windows getch() in your environment, but I don't know the details. It might be as simple as adding #include <conio.h>.) Apr 13, 2015 at 19:49
  • On gcc why do you need to halt the program? Mar 25, 2017 at 11:54
9

For linux, use this code:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int getch (void)
{
        int ch;
        struct termios oldt, newt;

        tcgetattr(STDIN_FILENO, &oldt);
        newt = oldt;
        newt.c_lflag &= ~(ICANON|ECHO);
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
        ch = getchar();
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

        return ch;
}

For Windows, add this line:

#include <conio.h>
4
  • 1
    Thank you for your answer. I am using windows 7.
    – jim jim
    Apr 13, 2015 at 18:33
  • Since there is no clarification in the question, this answer seems to be in reference to gcc... Mar 25, 2017 at 11:55
  • Maybe explain a bit, that this is switching the terminal from line mode to character mode, then back again.
    – Zan Lynx
    Aug 6, 2018 at 19:47
  • I am on Ubuntu and was thinking why conio.h was not helping
    – Meenohara
    Jun 7, 2022 at 10:58
-1

I got stuck in Mac, for use of getch(), as I wanted to stop screen on the certain condition so here how I figured it:

#include <stdlib.h>



int main()

{

 system( "read -n 1 -s -p \"Press any key to continue...\"" );

 return 0;

}

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.