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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With