<分区>
据我所知,cxyz
格式的 header 与 xyz.h
相同,唯一的区别是 cxyz
将所有命名空间 std
下的 xyz.h
的内容。为什么以下程序在 GCC 4.9 和 clang 6.0 上都能编译?
#include <cstdio>
int main() {
printf("Testing...");
return 0;
}
和第二个程序:
#include <cstdio>
int main() {
std::printf("Testing...");
return 0;
}
FILE 结构也是如此:
FILE* test = fopen("test.txt", "w");
和
std::FILE* test = std::fopen("test.txt", "w");
两者都有效。
直到现在,我一直认为最好使用 cstdio
、cstring
等,而不是它们的非命名空间对应物。但是,以下两个程序中哪个是更好的做法?
其他 C 函数也是如此,例如 memset(来自 cstring)、scanf(也来自 cstdio)等。
(我知道有些人会问为什么我在 C++ 程序中使用 C IO;这里的问题不是特定的 C IO,而是这段代码是否应该编译而不特别指定 std::
在调用命名空间 C 函数之前。)