Implicit declaration of 'getch' function

I have a simple program:

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

Even if I get a warning

 implicit declaration of function 'getch' 

the program is working fine. Did I miss something? And if so, why is the program working fine?

+8
c
source share
3 answers

You will get a warning

 implicit declaration of function 'getch' 

because you do not have a header declaring getch . The standard headers <stdio.h> or <stdlib.h> do not declare such a function.

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

Prior to the C99 standard, the C language allowed function calls without a visible declaration. Such a call will actually create an implicit declaration of the function that returns an int , and will accept the arguments of any (advanced) type that you actually passed.

Depending on this, it was never a good idea. You should always have the correct #include directive for the header, which declares any library function that you use in your program.

C99 rejected the "implicit int rule" and made any function call without a visible declaration of a violation of the constraint that requires diagnostics (this diagnosis is allowed as a non-fatal error.)

If you compile on Windows, if I remember correctly, there getch() function is declared in <conio.h> . If you want to use this function, you need to add #include <conio.h> to your program.

I do not recommend doing this; using getch() not necessary and makes your program not portable. In some Windows development environments, it is difficult to run β€œconsole programs” (programs that print to standard output rather than creating a graphical interface); often starting such a program creates a temporary window that is destroyed as soon as the program ends. Calling the standard getchar() function getchar() another way to prevent a window from disappearing. Or you can execute the program from the command line, and its output will appear in your current command window.

If you are compiling into a UNIX-like system, there is 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 should not use this getch() function if you have not created a curses environment, and it is clear enough that you do not want to do this.

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

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

How you run it and allow you to see that the result depends on your environment (which you did not tell us about).

+10
source share

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; } 

On Windows, add this line:

 #include <conio.h> 
+5
source share

How can I fix this error?

 $gcc -o main *.c main.c: In function 'main: main.c:18:26: warning: implicit declaration of function 'getch; did you mean 'getc? [-Wimplicit-function-declaration] printf("%c",c[i]=getch()); ^~~~~ getc /tmp/ccSlfnfq.o: In function 'main': main.c:(.text+0x89): undefined reference to 'getch' main.c:(.text+0x297): undefined reference to 'getch' main.c:(.text+0x2aa): undefined reference to 'getch' collect2: error: ld returned 1 exit status 
0
source share

All Articles