stdio.h not standard in C++?

杀马特。学长 韩版系。学妹 提交于 2019-12-17 06:46:33

问题


I know most compilers allow both:

#include <stdio.h>

//and

#include <cstdio>

But someone argued that <stdio.h> is not actually C++ standard.

is that true?


回答1:


stdio.h is standard, but deprecated. Always prefer cstdio in C++.

[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: D.5/3]: [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace 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. It may also provide these names within the namespace std. —end example ]




回答2:


It's not true, because C++ main goal is backward compatibility with C. The only difference is that for

#include <cstdio>

all functions are in std namespace




回答3:


The C standard headers are included in the C++ standard library for compatibility.

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.

In addition, the <c...> headers add overloads for functions like abs, pow etc.

Also, C++ headers replace some C classification/comparison macros with overloaded functions.




回答4:


The C++ standard library explicitly contains the C standard library, so is an entirely legitimate part of 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



来源:https://stackoverflow.com/questions/7596406/stdio-h-not-standard-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!