cstdio 与stdio.h的区别

I constantly see recommendations to #include <cstdio> instead of using stdio.h and the same for the other C headers. What most posters fail to mention is that this should put all the symbols into namespace std and NOT into the global namespace.
This you have to write std::printf(...). Simply writing printf alone will not work .
The same applies to the headers :
cassert ciso646 csetjmp cstdio ctime cctype climits csignal cstdlib cwchar cerrno clocale cstdarg cstring cwctype cfloat cmath cstddef.
This is clearly defined by ISO 14882 in footnote 160:
The ".h" headers dump all their names into the global namespace, whereas the newer forms keep their names in namespace
std. Therefore, the newer forms are the preferred forms for all uses except for C++ programs which are intended to be strictly compatible
with C.
Most compilers completely fail to implement this correctly and put all the names into both namespaces when using the new headers. As this is incorrect behaviour, it is quite likely to change in the future which means that code you write now may break on newer compilers as they get closer to the standard.
As an aside note that many compilers fail to give even a warning (and even on the highest warning level) when you write void main(), even though the standard clearly dictates that main MUST return int.

Nathan Myers, a member of the C++ standards committee wrote this paper on header strategy here:http://www.cantrip.org/cheaders.html
我经常看到建议#包括<< cstdio >代替stdio. h和其他C头一样。大多数的海报没有提到的是,这应该把所有的符号放在名称空间STD,而不是进入全局命名空间。
这个你要写std::printf(…)。只是写printf不会单独工作。
这同样适用于头文件:
cassert ciso646 csetjmp cstdio CTime cctype climits csignal cwchar cerrno clocale CString cwctype文件cstdlib cstdarg cfloat cmath cstddef。
ISO 14882在脚注160中明确规定了这一点:
“h”头将它们的名字全部转储到全局名称空间,而新的窗体将它们的名称保留在名称空间中。
因此,新形式的首选形式的用途除了C++程序要严格兼容,大多数编译器完全不能正确实现这一点,在使用新的头时将所有的名称放入两个命名空间中。由于这是不正确的行为,将来很可能会发生变化,这意味着现在编写的代码可能会在更接近标准的情况下出现在新的编译器上。
另外请注意,许多编译器失败甚至给警告(甚至在最高级别预警)当你写无效main(),虽然标准中明确规定,主要必须返回int。
Nathan Myers的C++标准委员会的成员写文章标题的策略:http://www.cantrip.org/cheaders.html
马库斯

Markus
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
stdio.c是一个C语言源文件,其中包含了与标准输入输出相关的函数的实现代码。而stdio.h是一个C语言头文件,其中包含了与标准输入输出相关的函数的声明。 在编写C语言程序时,我们可以通过包含<stdio.h>头文件来引入标准输入输出函数的声明,以便在程序中使用这些函数。这样可以提前告知编译器这些函数的存在,以便在编译期间进行语法检查和类型检查。而通过链接器,我们可以把stdio.c中的函数实现与我们的程序进行链接,使其能够正常运行。这样我们就可以在程序中使用诸如printf、scanf等函数来进行输入输出操作了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [stdio.h 库函数下载](https://download.csdn.net/download/jian8182/11122463)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [浅谈stdio.h及cstdio区别](https://blog.csdn.net/weixin_34320724/article/details/92895224)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [C/C++笔试必须熟悉掌握的头文件系列(一)——stdio.h/cstdio](https://blog.csdn.net/FX677588/article/details/52957601)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值