Is <math.h> for C or C ++?
I need a natural logarithm function for use in the source .cpp file (C ++). Now, of course, I can do this with a quick Google search and a simple library solution. But I'm a little confused ...
On the cplusplus dot com website at / cmath / log / they have an example of how to use the log function, as shown below.
/* 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; } I have a few questions:
1) Why do they use
<stdio.h> I thought it was for C and not for C ++?
2) Why do they use
<math.h> Although I .h represented the C header files, not the C ++ .hpp header files?
Forget about using stdio (I will use iostream anyway), but even when using
<math.h> It looks like I'm writing C code, not C ++ code. I learn C ++ through a taught course, and the teacher covered C in the first week, and then said that we would not use C again, but from now on we would use C ++. I feel that I canβt explain myself if the teacher asks: "Why did you use the C header file? You have to work in C ++."
Any explanation is greatly appreciated.
<math.h> is the title specified in the C standard. Its use is supported in C ++, but is officially outdated (which means, roughly, it is intended to be potentially removed from the future standard) by all C ++ standards. I would suggest that it is unlikely to be removed from the future C ++ standard, since backward compatibility with C is considered important or desirable.
<cmath> is the title specified in the C ++ standard. It provides almost the same functionality as in C <math.h> , except that names (except for a pair of macros) are in the std .
A similar story applies to <stdio.h> (C) and <cstdio> (C ++), except that using an I / O stream (e.g. <iostream> ) is recommended in C ++.
C ++ standard headers never have the extension .hpp . This naming convention for titles is a convention that is encouraged by some but not formally required.
C++11 Standard says:
D.5 C standard library headers
1 For compatibility with the Unicode TR standard C and C library, the C ++ standard library provides 25 C headers, ...
The inclusion of these headers is deprecated, which means:
The standard for the current edition of the Standard, but not guaranteed to be part of the Standard in future versions.
Thus, they remain (simply) part of C++ .
They are designed to ensure compatibility, which allows the programmer to compile programs originally written for C with a standard appropriate C++ compiler with little or no modification. This means that you do not need to change the #include statements from <stdio.h> to <ctsdio> .
Thus, the example given at cplusplus.com actually complies with the C++ , which is simply compatible with the C90 and C99 compatible C compiler. Presumably, they do this because the page describing the math library provides information for the C and C++ that follow the C90 , C99 , C++98 and C++11 .
So, to answer specific questions:
1) Why do they use
<stdio.h>I thought it was for C and not for C ++?
This is for C++ compatibility with C Presumably, they use it, so the code will also be compiled in the C90/C99 compiler corresponding to C , for which the page gives specifications.
1) Why do they use
<math.h>Although I .h represented the C header files, not the C ++ .hpp header files?
No. The standard does not specify which extension files should be used. In practice, many C++ projects use .h as an extension to their header files.
It seems to me that I canβt explain myself if the teacher asks: "Why did you use the C header file?
Given that the C compatibility headers are outdated (although they probably don't go anywhere), I would suggest using the <cstdio> and <cmath> versions better. However, the idea that you are somehow writing C code simply because of your choice of library function is incorrect. If this is legitimate C++ code passed through the C++ , then it is C++ . This may be more procedural in nature and less object oriented in philosophy, but nonetheless completely C++ . Many, many, many C++ programs use libraries written in other languages, especially C It does not make these 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 in either C or C ++, but cmath will define methods in the std namespace, while math.h will define them in the global namespace
As a rule, you can use C code in C ++ code, there will usually be no problem with it, especially when working with well-known libraries such as math.h
<math.h> (for C) and <cmath> (for C ++) are very similar in the main use of functions. <cmath> begins to differ in more advanced stages, including templates, STL, and object-oriented programming in general.
If you use C or C ++, you can use <math.h> , but I don't think the opposite ( <cmath> for C) works. I recommend <cmath> for C ++, as it is the same but has more powerful OOP features.