简体   繁体   English

是 <math.h> 对于C或C ++?

[英]is <math.h> for C or C++?

Im needing the natural logarithm function for use in a .cpp (c++) source file. 我需要自然对数函数用于.cpp(c ++)源文件。 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 在cplusplus dot com网站的参考/ cmath / log /下他们有一个如何使用日志功能的例子,如下

/* 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 1)他们为什么使用

<stdio.h>

I thought this was for C and not really for C++ ? 我认为这是针对C而不是C ++?

2) Why are they using 2)他们为什么使用

<math.h>

I though the .h represented C header files rather than the .hpp C++ header files? 我虽然.h代表C头文件而不是.hpp C ++头文件?

Forgetting about the use of stdio (i'll use iostream anyway) but even so by using 忘记使用stdio(无论如何我会使用iostream),但即便如此

<math.h>

It feels like I'm writing C code and not C++ code. 感觉就像我在编写C代码而不是C ++代码。 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. 我通过教学课程学习C ++,教师在第一周学习了C,然后说我们不再使用C,但从现在开始将使用C ++。 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++". 如果老师问“你为什么使用C头文件?我应该用C ++工作”,我觉得自己无法解释自己。

Any explanations much appreciated. 任何解释都非常感激。

<math.h> is a header specified in the C standard. <math.h>是C标准中指定的标头。 Its usage is supported in C++, but formally deprecated (which means, approximately, slated for potential removal from a future standard) by all C++ standards. 它的用法在C ++中得到了支持,但是所有C ++标准都正式弃用(这意味着大约可以从未来的标准中删除)。 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. 我建议不要将其从未来的C ++标准中删除,因为只要向后兼容C被认为是重要或可取的。

<cmath> is a header specified in the C++ standard. <cmath>是C ++标准中指定的标头。 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 . 它提供了与C的<math.h>基本相同的功能,只是名称(除了几个宏)驻留在名称空间std

A similar story goes for <stdio.h> (C) and <cstdio> (C++), except that usage of stream I/O (eg <iostream> ) is encouraged in C++. 类似的故事适用于<stdio.h> (C)和<cstdio> (C ++),除了在C ++中鼓励使用流I / O(例如<iostream> )。

Standard C++ headers never have a .hpp extension. 标准C ++标头永远不会有.hpp扩展名。 That naming convention for headers is a convention encouraged by some, but is not formally required. 标题的命名约定是某些人鼓励的约定,但不是正式要求的。

The C++11 Standard says: C++11 Standard说:

D.5 C standard library headers D.5 C标准库头

1 For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, ... 1为了与C标准库和C Unicode TR兼容,C ++标准库提供了25个C头,...

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++ . 所以他们仍然(只是) 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. 提供兼容性是为了允许程序员使用标准符合C++编译器编译最初为C编写的程序,几乎不做任何修改。 That means things like not having to change the #include statements from <stdio.h> to <ctsdio> . 这意味着无需将#include语句从<stdio.h>更改为<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. 因此, cplusplus.com给出的示例实际上是符合C++标准,恰好与C90和符合C99 C编译器兼容。 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 . 想必他们这样做,因为描述的数学库页给出了两个信息CC++遵循标准语言C90C99C++98C++11

So to answer the specific questions: 那么回答具体问题:

1) Why are they using 1)他们为什么使用

 <stdio.h> 

I thought this was for C and not really for C++ ? 我认为这是针对C而不是C ++?

It's for C++ compatibility with C . 这是为了C++C 兼容性 Presumably they use it so the code will also compile on a C90/C99 conforming C compiler for which the page gives specifications. 据推测,他们使用它,因此代码也将在符合C90/C99标准的C编译器上编译,该编译器为页面提供了规范。

1) Why are they using 1)他们为什么使用

 <math.h> 

I though the .h represented C header files rather than the .hpp C++ header files? 我虽然.h代表C头文件而不是.hpp C ++头文件?

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. 实际上,许多C++项目使用.h作为其头文件的扩展。

I feel like I wont be able to explain myself if the teacher asks "why did you use a C header file? 如果老师问“你为什么使用C头文件?”我觉得我无法解释自己?

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. 鉴于C兼容性头文件已被弃用(尽管可能不会去任何地方),我建议最好使用<cstdio><cmath>版本。 However the idea that you are somehow writing C code simply because of your choice of library function is wrong. 但是,由于您选择库函数,您以某种方式编写C代码的想法是错误的。 If it is legal C++ code being fed through a C++ compiler then it is C++ . 如果它是通过C++编译器提供的合法C++代码,那么它就是C++ It may be more procedural in character and less object oriented in philosophy but it is nevertheless fully C++ . 它可能在哲学上更具程序性,在哲学中更少面向对象 ,但它完全是C++ Many, many, many C++ programs use libraries written in other languages, especially C . 许多很多C++程序使用其他语言编写的库, 尤其是 C语言。 That doesn't make those programs somehow C . 这不会使这些程序以某种方式C

About your first qustion, stdio.h is required for the printf function used 关于你的第一个qustion,使用的printf函数需要stdio.h
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 关于你的第二个问题,math.h可以被C和C ++使用,但是cmath将定义std命名空间中的方法,而math.h将定义全局命名空间中的方法。

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 一般来说,你可以在C ++代码中使用C代码, 通常不会有任何问题,特别是在处理像math.h这样的知名库时

<math.h> (for C) and <cmath> (for C++) are very similar in basic usage of functions. <math.h> (对于C)和<cmath> (对于C ++)在函数的基本用法上非常相似。 <cmath> begins to differ in more advanced stages, involving templates, STL, and Object-Oriented Programming in general. <cmath>在更高级的阶段开始有所不同,一般涉及模板,STL和面向对象的编程。

Whether you use C or C++, <math.h> can be used, but I don't think the opposite ( <cmath> for C) works. 无论您使用的是C还是C ++,都可以使用<math.h> ,但我不认为相反(C语言为<cmath> )有效。 I recommend <cmath> for C++ as it's the same as , but has more powerful OOP features. 我推荐C ++的<cmath> ,因为它与之相同,但具有更强大的OOP功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM