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.
<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 namespacestd
.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:The inclusion of these headers is stated as deprecated, meaning:
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 conformingC++
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 conformingC++
that just happens to be compatible with aC90
and aC99
conformingC
compiler. Presumably they do this because the page describing the math library gives information for bothC
andC++
languages following the standards forC90
,C99
,C++98
andC++11
.So to answer the specific questions:
It's for
C++
compatibility withC
. Presumably they use it so the code will also compile on aC90/C99
conformingC
compiler for which the page gives specifications.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.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 writingC
code simply because of your choice of library function is wrong. If it is legalC++
code being fed through aC++
compiler then it isC++
. It may be more procedural in character and less object oriented in philosophy but it is nevertheless fullyC++
. Many, many, manyC++
programs use libraries written in other languages, especiallyC
. That doesn't make those programs somehowC
.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
<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.