0

In visual studio c++ consider the following code

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    cout<<"Hello!";
    return 0;
    _getch();
}

In the above code in the #include<iostream> header file we dont use the .h extension because it produces an error.But in the header file #include<conio.h> the .h extension is added. Why does it produces error in case of #include<iostream> and not in case of #include<conio.h> If we write only conio it produces an error. And one more question why we use #include"iostream" header file when cout and cin are already included in namespace std

Varun Moghe
  • 74
  • 2
  • 3
  • 12
  • 2
    Very simple: one file is called `iostream`, and the other is called `conio.h`. Including a file that doesn't exist is an error. – juanchopanza Sep 17 '14 at 06:00
  • 1
    `iostream` is a standard header. `conio.h` is not – M.M Sep 17 '14 at 06:00
  • 1
    Also note that `` is a non-standard **C** header from Borland/Turbo C/C++, whereas `` is of course a standard **C++** header. – Paul R Sep 17 '14 at 06:02
  • 2
    `#include ` is necessary for `_getch()`, and `_getch()` is a programmer's way to say "My console window closes too quickly and I cannot see the 'hello world' my program is supposed to print. I therefore must copy this obscure incantation from an ancient scroll, and sacrifice a goat." – n. m. could be an AI Sep 17 '14 at 06:10
  • 1
    But the program will not be portable if you use `#include`, at least to Linux. – user1436187 Sep 17 '14 at 06:13
  • 2
    Prior to STL 98 standardization, most of the STL header files had (.h) file extensions. It was decided to remove to .h extension at some point in time. I'm not sure why this was decided, but possible to provide uniqueness. For more info, see http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library. is a Borland / MS specific header that contained console I/O functionality, and is not part of the standard. – Werner Erasmus Sep 17 '14 at 06:14
  • 2
    @n.m. if you put the `_getch()` after `return 0;` thou shall sacrifice two goats ! :P – 463035818_is_not_an_ai Sep 27 '18 at 14:02

1 Answers1

2

Prior to STL 98 standardization, most of the STL header files had (.h) file extensions. It was decided to remove to .h extension at some point in time. I'm not sure why this was decided, but possibly to provide uniqueness For more info, see.

conio.h is a Borland / MS specific header that contained console I/O functionality, and is not part of the standard.

Including standard files with .h extension is (still) allowed by some compilers, but is considered deprecated. The non extension version often forward include to extensions, and provide an extra layer that allows the "user" to be independent of the actual folder organization. This might be an additional reason.

Werner Erasmus
  • 3,988
  • 17
  • 31
  • 1
    [When can you omit the file extension in an #include directive?](http://stackoverflow.com/questions/441568/when-can-you-omit-the-file-extension-in-an-include-directive) provides a guess why the .h extension was dropped. – rknuus Sep 17 '14 at 10:54