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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值