is for C or C++?
Sebastian Wright
Im needing the natural logarithm function for use in a .cpp (c++) source file. Now, of course I can do this with a quick google search and a simple library solution. But Im a bit confused...
On the cplusplus dot com website under reference/cmath/log/ they have an example of how to use the log function, as follows
/* log example */
#include <stdio.h> /* printf */
#include <math.h> /* log */
int main ()
{ double param, result; param = 5.5; result = log (param); printf ("log(%f) = %f\n", param, result ); return 0;
}some questions i have:
1) Why are they using
<stdio.h>I thought this was for C and not really for C++ ?
2) Why are they using
<math.h>I though the .h represented C header files rather than the .hpp C++ header files?
Forgetting about the use of stdio (i'll use iostream anyway) but even so by using
<math.h>It feels like I'm writing C code and not C++ code. Im learning C++ through a taught course and the instructor covered C in the first week and then said we wont be using C again but will be using C++ from now on. I feel like I wont be able to explain myself if the teacher asks "why did you use a C header file? You are supposed to be working in C++".
Any explanations much appreciated.
54 Answers
<math.h> is a header specified in the C standard. Its usage is supported in C++, but formally deprecated (which means, approximately, slated for potential removal from a future standard) by all C++ standards. I would suggest it is unlikely to be removed from a future C++ standard, for as long as backward compatibility to C is considered important or desirable.
<cmath> is a header specified in the C++ standard. It provides essentially the same functionality as in C's <math.h>, except that names (other than a couple of macros) reside in namespace std.
A similar story goes for <stdio.h> (C) and <cstdio> (C++), except that usage of stream I/O (e.g. <iostream>) is encouraged in C++.
Standard C++ headers never have a .hpp extension. That naming convention for headers is a convention encouraged by some, but is not formally required.
The C++11 Standard says:
D.5 C standard library headers
1 For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, ...
The inclusion of these headers is stated as deprecated, meaning:
Normative for the current edition of the Standard, but not guaranteed to be part of the Standard in future revisions.
So they are still (just) part of C++.
They are provided for compatibility which is to allow the programmer to compile programs originally written for C with a standard conforming C++ compiler with little or no modification. That means things like not having to change the #include statements from <stdio.h> to <ctsdio>.
So the example given in cplusplus.com is actually standards conforming C++ that just happens to be compatible with a C90 and a C99 conforming C compiler. Presumably they do this because the page describing the math library gives information for both C and C++ languages following the standards for C90, C99, C++98 and C++11.
So to answer the specific questions:
1) Why are they using
<stdio.h>I thought this was for C and not really for C++ ?
It's for C++ compatibility with C. Presumably they use it so the code will also compile on a C90/C99 conforming C compiler for which the page gives specifications.
1) Why are they using
<math.h>I though the .h represented C header files rather than the .hpp C++ header files?
No. The standard does not specify what extensions files should use. In practice many C++ projects use .h as an extension for their header files.
I feel like I wont be able to explain myself if the teacher asks "why did you use a C header file?
Given that the C compatibility headers are deprecated (though probably not going anywhere) I would suggest it better to use the <cstdio> and <cmath> versions. However the idea that you are somehow writing C code simply because of your choice of library function is wrong. If it is legal C++ code being fed through a C++ compiler then it is C++. It may be more procedural in character and less object oriented in philosophy but it is nevertheless fully C++. Many, many, many C++ programs use libraries written in other languages, especially C. That doesn't make those programs somehow C.
About your first qustion, stdio.h is required for the printf function used
About your second question, math.h can be used by both C and C++, but cmath will define the methods in std namespace while math.h will define those in the global namespace
Generally put, you can use C code within C++ code, there usually not going to be any problem with that, especially when dealing with well known libraries like math.h
4<math.h>(for C) and <cmath>(for C++) are very similar in basic usage of functions. <cmath> begins to differ in more advanced stages, involving templates, STL, and Object-Oriented Programming in general.
Whether you use C or C++, <math.h> can be used, but I don't think the opposite (<cmath> for C) works. I recommend <cmath> for C++ as it's the same as , but has more powerful OOP features.