Neo*_*Neo 4 c++ printf iostream
这段代码即使在 C++11 标准下使用 minGW 编译器也能正常工作:
#include <iostream>
int main(int argc, char const *argv[])
{
printf("haha");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么要这样做?我没有包括,stdio.h但我可以使用像printf()and 之类的函数rand()。他们包括在iostream? 至少我没有发现它们包括在内。如果你说它包含在 中iostream,请给我看证据。
它的实现定义了它是否有效。
该实现可能包含它需要的其他标头,但作为开发人员的您不应依赖它,并且 includecstdio也是访问std::printf.
包括stdio.hputsprintf在全局命名空间中,这在 C++ 中通常不是人们想要的,所以坚持使用cstdio.
printf即使您只包含一个 C++ 标头,您的实现似乎也放入了全局命名空间。这很不幸,但这种情况也会发生。
证据:我的预处理器被调用cpp,我可以用它来列出包含的头文件。我有这个我称之为的程序std.cpp:
#include <iostream>
int main() {}
Run Code Online (Sandbox Code Playgroud)
如果我cpp用来列出包含标题的一小部分
cpp -M std.cpp | tr -d '\\' | tr ' ' '\n' | \
grep -E '^/[^\.]+$' | awk -F/ '{print $NF}'
Run Code Online (Sandbox Code Playgroud)
我在我的系统上得到了这些 C++ 头文件:
iostream
ostream
ios
iosfwd
cwchar
exception
typeinfo
new
type_traits
cstdint
clocale
cctype
string
initializer_list
cstdlib
cstdio
cerrno
system_error
stdexcept
streambuf
cwctype
istream
Run Code Online (Sandbox Code Playgroud)
是的,cstdio在那里还包括stdio.h.