The extension you use for a header file doesn't technically matter because the compiler never sees it. You could name it with the extension .this-is-a-header-file
and it would probably still work.
The compiler only handles the C/C++ source files, typically named .c
and .cpp
. However, before that happens, the preprocessor goes through and looks for any #include
lines. When it sees one, it basically copies the entire contents of the included file into the source file before it gets compiled.
This is normally used to include header files. However, the preprocessor doesn't know or care about different programming languages. All it does is process the raw text so there's nothing stopping you from including any text file you like, with any name or extension.
Choosing to use .h
or .hpp
is mostly just personal preference. You could certainly argue that it lets you (as a programmer) distinguish between C/C++ code more easily.
However, the reason why .h
is more common is probably that C and C++ code is mixed so freely. You can generally compile C code as C++ without any problems, in which case it's the context (rather than the file itself) that tells you if it's C or C++. It's also possible to write a library in C++ which only exposes a C interface, meaning it could be used by C programs.
You could definitely include a C++-specific header file in a C program by mistake. All that would happen is that the compiler wouldn't understand the contents of that header file. Sensible development environments don't link to a project/library when it sees a #include
directive (the Arduino IDE is the only one which does this, as far as I'm aware).