I've seen some information about differences between things like iostream
vs iostream.h
. From what I gathered from those the difference between them is that the version without the .h
extension will not populate the namespace while the version with the extension will.
Is this the same for cmath
vs math.h
? Why is cmath
(and many other files like it) prefixed with a c
instead of just being math
? Are there more differences between them?
Thank you for your time!
Maybe this would be helpful :
c-prefixed vs .h extension headers
[iostream.h] is not a standard header.
it is not an example of the issue you're raising.
[cmath] defines symbols in the
std
namespace, and may also define symbols in the global namespace. [math.h] defines symbols in the global namespace, and may also define symbols in thestd
namespace. if you include the former and use an unqualified symbol, it may compile with one compiler but not with another. therefore it's a good idea to use [math.h]. and in general, for such header pairs, to use the [.h] version.c++98 provided a formal guarantee of the c
xxx
header not polluting the global namespace. maybe that was why they were defined. however, that was a bit harder to implement than polluting ones, so in practice no standard library implementation that i know of followed the standard in this respect, and so it was finally changed to reflect reality in c++11.The headers whose names start with
c
are derived from the headers of the C standard library. The corresponding headers with thec
prefix removed and a.h
suffix added are identical (or very nearly identical) to the C standard library headers.<cmath>
defines the relevant symbols under thestd
namespace;<math.h>
defines them globally.(I just learned it's not quite that simple; see Alf's answer.)