11

What are the considerations for including the former rather than the latter in a C++ program? I always include math.h, stdlib.h and never cmath, cstdlib etc. I don't understand the reason the latter even exist, could someone please enlighten me?

Jim McAdams
  • 1,034
  • 1
  • 14
  • 30
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • The marked duplicate precisely answers your question. – Alok Save Mar 27 '13 at 10:55
  • Possibly more pertinent duplicate than those already listed: http://stackoverflow.com/questions/8734230/math-interface-vs-cmath-in-c –  Dec 09 '13 at 02:14

1 Answers1

22

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 header name.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

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • So there is in fact no real reason to use ` ` headers? `std` namespace is an inconvenience. – Violet Giraffe Mar 27 '13 at 10:25
  • 5
    @VioletGiraffe Perhaps you misread. I said "Prefer to include the `` headers." The `std` namespace helps you avoid name pollution. The C++ headers also have some changes that make them more useful for C++. – Joseph Mansfield Mar 27 '13 at 10:27
  • I did not misread. I don't see a single reason to prefer ` ` header now. – Violet Giraffe Mar 27 '13 at 10:28
  • 2
    The 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 – jalf Mar 27 '13 at 10:32
  • 2
    "helps you avoid name pollution" -- Not when "It is unspecified whether these names are first declared within the global namespace scope" – Jim Balter Mar 27 '13 at 10:33
  • 5
    @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. – juanchopanza Mar 27 '13 at 10:33
  • There are a few substantive differences between the C specification for the C headers and the C++ specification. One example is `strstr`; the C definition discards `const` qualifiers. – Pete Becker Mar 27 '13 at 11:13
  • 1
    During an upgrade of a project from VC++6.0 to VS2015, I found that there is absolutely a good reason to prefer 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 in order to compile existing code. According to this link, https://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. – shawn1874 Aug 23 '17 at 23:22