0

I know that function getch() is in conio.h but somehow my compiler seems to run getch() quite fine even if I didn't include conio.h. I'm using DevC++.

It's not like I want to write a code without including the library conio.h, but today I had a test in my class. One of the question asks for an output for a C program which contains the function getch() but no library conio.h. One of my friends just answer "Compilation Error" but the teacher said that using getch() without including conio.h is perfectly fine.

So is it fine to write the code without including the library, will it affect the program?

  • 1
    It's fine to write code without `getch()`. But if you _must_ use it, include the appropriate library header. – M Oehm Oct 08 '14 at 11:40
  • conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library, ISO C nor is it defined by POSIX. – Igor Pejic Oct 08 '14 at 11:40
  • possible duplicate of [How to implement getch() function of C in Linux?](http://stackoverflow.com/questions/3276546/how-to-implement-getch-function-of-c-in-linux) – Dr. Debasish Jana Oct 08 '14 at 11:51
  • 1
    possible duplicate of [What will happen if I don't include header files](http://stackoverflow.com/questions/16241313/what-will-happen-if-i-dont-include-header-files) – Mohit Jain Oct 08 '14 at 12:16
  • A `.h` file is not a library! In C, header files and libraries are two different kinds of files, though there is a natural relationship between those. – Erich Kitzmueller Oct 08 '14 at 12:32
  • I think this is indeed a duplicate of [What will happen if I don't include header files](http://stackoverflow.com/questions/16241313/what-will-happen-if-i-dont-include-header-files/) – Santi Santichaivekin Oct 08 '14 at 12:36

3 Answers3

1

It depends on the Compiler... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output.[1] It is not part of the C standard library, ISO C nor is it defined by POSIX. In modern compilers conio.h is not used.

Tushar Anand
  • 37
  • 10
1

There are two kinds of entities that read C source code: compilers (and I guess interpreters) and programmers.

If you don't include the appropriate headers, the compiler has to guess the prototype of any library functions your program uses. For simple functions like getch() the guess is likely to be correct, but that's not always the case & incorrect guesses can lead to mysterious bugs.

So if for some reason you don't want to include a particular header you should (at least) explicitly provide a correct prototype for the functions you use.

However, while compilers can tolerate all sorts of sloppy coding it's considered bad practice to inflict such sloppiness on human readers of your code. And that human reader may be yourself months or years later. So make it easy on yourself and other people who look at your code and make your code as easy to read as possible.

Most long-time programmers will agree that reading one's old source code can be an exercise in embarrassment. So develop good coding habits now and avoid the embarrassment later. :)

As others have noted, conio.h and getch() are non-standard, so including conio.h instantly lets anyone know who reads your code that it's targeting a Microsoft environment, and hence may require significant modification to run on any other type of system.

PS. It wouldn't surprise me in the least if some Microsoft compilers automagically include conio.h or even stdio.h or stdlib.h when required...

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0

A header file contains, among other things, function prototypes. Older C compilers, such as those supported by Microsoft Visual Studio, will default the function prototype if one is not supplied, to a function that returns an int. This is probably what is happening here.

You should get a warning to the effect that it has defaulted the prototype. If not, then you might be including another header file that includes conio.h, or you may be compiling in an environment that does not require it (as others have said).

cdarke
  • 42,728
  • 8
  • 80
  • 84