cmath vs math.h (And similar c-prefixed vs .h extension headers)

C++HeaderInclude

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

> I've seen some information about differences between things like iostream vs iostream.h.

[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 the std 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 cxxx 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.

Solution 2 - C++

Maybe this would be helpful :

> The C++ library includes the same definitions as the C language > library organized in the same structure of header files, with the > following differences: > > 1 - Each header file has the same name as the C > language version but with a "c" prefix and no extension. For example, > the C++ equivalent for the C language header file < stdlib.h > is > < cstdlib>. > > 2 - Every element of the library is defined within the std namespace.

c-prefixed vs .h extension headers

Solution 3 - C++

The headers whose names start with c are derived from the headers of the C standard library. The corresponding headers with the c 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 the std namespace; <math.h> defines them globally.

(I just learned it's not quite that simple; see Alf's answer.)

Solution 4 - C++

<cmath> and any <cxxx> header are standard C++, meaning you have strong guarantees of what is supported in those headers and how the functions in them work, as outlined in the C++ Standard. They define a series of functions in the std namespace, and that's it.

<math.h> and any <xxx.h> headers are not standard C++, despite being supported on every major implementation. However, since they are deprecated, there is no guarantee of what is inside those headers when you include them on your implementation. In fact, it has been observed on certain implementations that they provide functions that behave differently to the <cxxx> versions.

Therefore, you should always use <cxxx> when writing C++, and qualify the names of the functions with std::, for example std::malloc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongolmschenkView Question on Stackoverflow
Solution 1 - C++Cheers and hth. - AlfView Answer on Stackoverflow
Solution 2 - C++Hani ShamsView Answer on Stackoverflow
Solution 3 - C++Keith ThompsonView Answer on Stackoverflow
Solution 4 - C++themeemanView Answer on Stackoverflow