Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 971
golmschenk Avatar asked May 22 '12 00:05

golmschenk


People also ask

Is cmath same as math H?

[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.

What is the cmath header?

The cmath header file contains definitions for C++ for computing common mathematical functions. Include the standard header into a C++ program to effectively include the standard header < math. h > within the std namespace.

What does #include cmath mean C++?

# is a preprocessor that includes cmath library means all the definitions regarding math library are included in it and can be used readily. In short "#include<cmath>" means before proceeding to programme once look at these library.

What library is cmath in?

Conclusion. Cmath library is an essential part of the C++ programming language as it provides many operations and functions of mathematics when we use the programs to execute source codes. All of these functions are built-in functions.


2 Answers

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.

like image 185
Cheers and hth. - Alf Avatar answered Oct 02 '22 16:10

Cheers and hth. - Alf


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

like image 37
Hani Shams Avatar answered Oct 02 '22 17:10

Hani Shams