#include <cmath> vs #include <math.h> in a C++ program
Prefer to include the <c ...>
headers. They are C++ standard library headers. The <... .h>
headers are headers defined by the C standard library:
The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.
The C++ headers, for the most part, have content identical to the corresponding C library headers except that the names are all defined in the std
namespace.
Except as noted in Clauses 18 through 30 and Annex D, the contents of each header
cname
shall be the same as that of the corresponding headername.h
, as specified in the C standard library (1.2) or the C Unicode TR, as appropriate, as if by inclusion. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations
Violet Giraffe
I'm from Kharkiv, Ukraine. My city is being destroyed every day. Help us defend: https://www.comebackalive.in.ua/donate PC enthusiast, C++ fan and C++ / Qt / Android software developer. Also <3 Arduino. Music and car addict. Reinventing various wheels in my spare time. A custom-made square wheel is like a tailored suit, wouldn't you agree? Why do I have more rep from questions than from answers? Because you don't learn by answering; you learn by asking questions :) Ongoing personal open-source projects: Dual-panel file manager for Windows / Linux / Mac
Updated on June 11, 2020Comments
-
Violet Giraffe almost 4 years
What are the considerations for including the former rather than the latter in a C++ program? I always include
math.h
,stdlib.h
and nevercmath
,cstdlib
etc. I don't understand the reason the latter even exist, could someone please enlighten me? -
Violet Giraffe about 11 yearsSo there is in fact no real reason to use
<c ...>
headers?std
namespace is an inconvenience. -
Joseph Mansfield about 11 years@VioletGiraffe Perhaps you misread. I said "Prefer to include the
<c ...>
headers." Thestd
namespace helps you avoid name pollution. The C++ headers also have some changes that make them more useful for C++. -
Violet Giraffe about 11 yearsI did not misread. I don't see a single reason to prefer
<c ...>
header now. -
josesuero about 11 yearsThe only reason to prefer it is stylistic, really. I generally prefer it because, well, I'm writing C++, so why not use the C++ headers. But it doesn't really make a difference, and you might as well use the C-style header, really
-
Jim Balter about 11 years"helps you avoid name pollution" -- Not when "It is unspecified whether these names are first declared within the global namespace scope"
-
juanchopanza about 11 years@VioletGiraffe There used to be good reasons in C++03 (except that vendors didn't implement the rules to the letter of the law). In C++11, the whole thing is messed up, in my opinion, because vendors are allowed to bring in the names outside of the
std::
namespace too. -
Pete Becker about 11 yearsThere are a few substantive differences between the C specification for the C headers and the C++ specification. One example is
strstr
; the C definition discardsconst
qualifiers. -
shawn1874 over 6 yearsDuring an upgrade of a project from VC++6.0 to VS2015, I found that there is absolutely a good reason to prefer <CMath> over Math.h. They aren't even close to being the same. math.h doesn't have any the same versions of the abs function. I was quite surprised that I have to change from math.h to <cmath> in order to compile existing code. According to this link, connect.microsoft.com/VisualStudio/feedbackdetail/view/1665241, MS purposefully removed many C++ overloads from math.h so if you want a std compliant header you need cmath now.