是< math.h>是C还是C ++? [英] is <math.h> for C or C++?
问题描述
我需要在.cpp(c ++)源文件中使用自然对数函数.现在,我当然可以通过快速的Google搜索和简单的图书馆解决方案来做到这一点.但是我有点困惑...
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...
在cplusplus dot com网站的reference/cmath/log/下,他们有一个有关如何使用日志功能的示例,如下所示:
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;
}
我有一些问题:
1)他们为什么使用
<stdio.h>
我以为这是C语言,而不是C ++语言?
I thought this was for C and not really for C++ ?
2)他们为什么使用
<math.h>
我虽然.h代表C头文件而不是.hpp C ++头文件?
I though the .h represented C header files rather than the .hpp C++ header files?
忘记使用stdio(无论如何我都将使用iostream),但即使如此,也要使用
Forgetting about the use of stdio (i'll use iostream anyway) but even so by using
<math.h>
感觉就像我在写C代码而不是C ++代码.我通过一门讲授的课程学习C ++,讲师在第一周讲了C,然后说我们不会再使用C,但是从现在开始将使用C ++.如果老师问为什么要使用C头文件?您应该使用C ++来工作",我感觉自己无法解释自己.
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++".
任何解释都值得赞赏.
推荐答案
<math.h>
是C标准中指定的标头.它的用法在C ++中受支持,但所有C ++标准都已正式弃用(这意味着大约有可能从将来的标准中删除).我建议,只要认为与C的向后兼容性很重要或可取,就不可能将其从将来的C ++标准中删除.
<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>
是C ++标准中指定的标头.它提供了与C的<math.h>
基本上相同的功能,除了名称(几个宏除外)驻留在名称空间std
中.
<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
.
<stdio.h>
(C)和<cstdio>
(C ++)也有类似的故事,只是在C ++中鼓励使用流I/O(例如<iostream>
).
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++.
标准C ++标头从不具有.hpp
扩展名.标头的命名约定是某些人所鼓励的约定,但并不是正式要求.
Standard C++ headers never have a .hpp
extension. That naming convention for headers is a convention encouraged by some, but is not formally required.
这篇关于是< math.h>是C还是C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!