参见英文答案 >
When using C headers in C++, should we use functions from std:: or the global namespace? 8个
据我所知,cxyz形式的标题与xyz.h完全相同,唯一的区别是cxyz将xyz.h的所有内容放在命名空间std下.为什么以下程序都在GCC 4.9和clang 6.0上编译?
#include
int main() {
printf("Testing...");
return 0;
}
和第二个程序:
#include
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,而是在调用命名空间C函数之前是否应该编译而没有专门指定std ::)