[英]stdio.h not standard in C++?
I know most compilers allow both:我知道大多数编译器都允许:
#include <stdio.h>
and和
#include <cstdio>
But someone argued that <stdio.h>
is not actually C++ standard.但有人认为
<stdio.h>
实际上并不是 C++ 标准。 Is that true?真的吗?
stdio.h
is standard, but deprecated. stdio.h
是标准的,但已弃用。 Always prefer cstdio
in C++.在 C++ 中总是更喜欢
cstdio
。
[n3290: C.3.1/1]:
For compatibility with the Standard C library, the C++ standard library provides the 18 C headers (D.5), but their use is deprecated in C++.[n3290: C.3.1/1]:
为了与标准 C 库兼容, C++ 标准库提供了 18 个 C 头文件 (D.5),但在 C++ 中不推荐使用它们。
[n3290: D.5/3]:
[ Example: The header<cstdlib>
assuredly provides its declarations and definitions within the namespacestd
.[n3290: D.5/3]:
[示例:头文件<cstdlib>
确实在命名空间std
提供了它的声明和定义。 It may also provide these names within the global namespace.它还可以在全局命名空间内提供这些名称。 The header
<stdlib.h>
assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard.头文件
<stdlib.h>
确实在全局命名空间中提供了相同的声明和定义,就像在 C 标准中一样。 It may also provide these names within the namespacestd
.它还可以在命名空间
std
提供这些名称。 —end example ]—结束示例]
It's not true, because C++ main goal is backward compatibility with C. The only difference is that for这不是真的,因为 C++ 的主要目标是与 C 向后兼容。唯一的区别是对于
#include <cstdio>
all functions are in std
namespace所有函数都在
std
命名空间中
The C standard headers are included in the C++ standard library for compatibility.为了兼容性,C 标准头文件包含在 C++ 标准库中。
The difference is that identifiers in corresponding C++ headers must (also) be in std namespace, whereas identifiers in C headers must (also) be available in global namespace.不同之处在于相应 C++ 头文件中的标识符必须(也)在 std 命名空间中,而 C 头文件中的标识符必须(也)在全局命名空间中可用。
In addition, the <c...>
headers add overloads for functions like abs
, pow
etc.此外,
<c...>
标头为abs
、 pow
等函数添加了重载。
Also, C++ headers replace some C classification/comparison macros with overloaded functions.此外,C++ 头文件用重载函数替换了一些 C 分类/比较宏。
The C++ standard library explicitly contains the C standard library, so is an entirely legitimate part of C++. C++ 标准库明确包含 C 标准库,因此是 C++ 的一个完全合法的部分。 And if you are talking about using
#include <stdio.h>
in C++ code, then you shouldn't do that, cause that's C syntax, in C++ code, you should use always cstdio如果您正在谈论在 C++ 代码中使用
#include <stdio.h>
,那么您不应该这样做,因为这是 C 语法,在 C++ 代码中,您应该始终使用 cstdio
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.