Search code examples
c++includeheader-filesfile-extension

Include header files without ".h" format


I know that to include headers you must type #include <header.h>.

Is there a way to include the header as #include <header>, like we do with iostream or cstdlib?


Solution

  • Just rename header.h to header. The files iostream, cstdlib, etc are files with exactly those names that don't have any extension. For example, here is a screenshot of Visual Studio's iostream file in Windows Explorer:

    enter image description here

    Usually when you create your own header file it has the extension .h (or sometimes .hpp), but nothing in the C++ standard forces them to have any specific extension. You can include a file with any you want as long as its contents is valid C++. Some libraries like the C++ standard library or Qt don't have any extension on their header files just to make your code look nice. Most IDEs automatically add a .h extension when you create a header file because .h is the extension that header files usually have, but that's just a convention, you can give them any extension you want.