(As far as I know) C++ doesn't accept the ".h" extension on header files (as it's usually not present in other include statements) so how does the include <bits/stdc++.h>
work in C++ and why does this have the ".h" extension?

- 9,146
- 4
- 29
- 50

- 186
- 1
- 1
- 16
-
8Please also read ["Why should I not #include
?"](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.). – G.M. Dec 04 '20 at 11:17 -
So you reckon `#include
` shouldn't compile? – Aykhan Hagverdili Dec 04 '20 at 11:28 -
i got the answer "its not a standard header file" that's all...i was just thinking why this one only has the ".h" extension.. – Pranshu_Taneja Dec 04 '20 at 11:33
-
4Rolled back the edit as the edit invalidates the below answers. – Ch3steR Dec 04 '20 at 11:45
-
Did you expect C++ to only work with ".hpp" files? .h is fine. – KYL3R Dec 04 '20 at 11:47
-
2the assumption is wrong. you can include files with any extension. Many of the std library headers have no extension, that's why you don't see them. – JHBonarius Dec 04 '20 at 12:08
-
Why is there a ".h" extension in "my_header.h"?
– Pete Becker Dec 04 '20 at 16:06
3 Answers
why there is “.h” extension in <bits/stdc++.h>
Because the developer - who created the file - chose that name.
It is conventional to name headers with the suffix .h and the developer presumably followed such convention.
(As far as I know)c++ doesn't accept the ".h" extension header files
Your knowledge is wrong. Any "extension", including no extension are accepted. There is no limitation to how a header file can be named.
You've tagged [c++-standard-library], so I'll add that C++ standard headers are all named without a suffix, except for those inherited from the C standard library which use the conventional .h suffix (and which do have non-suffixed aliases). bits/stdc++.h is not a standard header even though it may be part of a standard library implementation.

- 232,697
- 12
- 197
- 326
-
6@fly_high Good answers on Stackoverflow require explanations. This is a good answer. – Robert Andrzejuk Dec 04 '20 at 11:41
C++ does not use the .h
extension for standard library headers named in #include
statements.
But bits/stdc++.h
is not a standard header, so the standard library naming conventions wouldn't apply to it. It should never be used anyway.
There is no mandatory mapping, IIRC, from the name used in the include statement, to the filename. You can certainly use .h
extensions on your own headers if you want, although it may get confusing if you mix C and C++ in a project.

- 64,155
- 6
- 88
- 132
-
1Not only is there no mandatory mapping to a filename, there is no mandatory file. `#include
` is simply required to work. `#include "whatever"` does require a file named "whatever". – Pete Becker Dec 04 '20 at 16:07
(As far as I know)c++ doesn't accept the ".h" extension header files
That is an incorrect statement.
You can use any filename in that statement.
After the preprocessor has inserted all the files and the result is valid C++ code, then there will not be any errors during compilation.
The case with standard headers is that the C++ standard library developers have created the standard functionality in files without an extension. So the filename is "vector", so #include <vector>
is used.
If the developers add a file "vector.hpp", (which has a statement #include <vector>
inside the body of the file) then it has to be used as #include <vector.hpp>
.

- 5,076
- 2
- 22
- 31
-
Being hyper-technical, `#include
` does not mean that there is a file named `vector`. There doesn't have to be a file at all (the compiler can have an internal database, for example, although nobody does that), and if there is a file, its name can be anything that the compiler writer wants. You're describing the usual (as in, universal) implementation. Note that `#include "whatever"` is different: it always names a file. +1. – Pete Becker Dec 04 '20 at 16:05