Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
817 views
in Technique[技术] by (71.8m points)

c - implicit declaration of function 'getch'

I have a simple program:

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

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

    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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!
");
    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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...