[英]Why can I use printf() when including iostream?
This piece of codes works fine even with minGW compiler under C++11 standards:即使在 C++11 标准下使用 minGW 编译器,这段代码也能正常工作:
#include <iostream>
int main(int argc, char const *argv[])
{
printf("haha");
return 0;
}
Why should this work?为什么要这样做? I didn't include
stdio.h
but I can use functions like printf()
and rand()
.我没有包含
stdio.h
但我可以使用printf()
和rand()
类的函数。 Are they included in iostream
?它们是否包含在
iostream
中? At least I didn't find them included.至少我没有发现它们包括在内。 If you say that it's included in
iostream
, show me the evidence.如果您说它包含在
iostream
中,请给我看证据。
It's implementation defined if that works or not.它的实现定义是否有效。
The implementation may include additional headers it needs, but you as a developer should not rely on that and include cstdio
too which is the guaranteed way to get access to std::printf
.该实现可能包括它需要的其他头文件,但作为开发人员的您不应该依赖它并包括
cstdio
,这是访问std::printf
的保证方式。
Including stdio.h
puts printf
in the global namespace and that is usually not what one wants in C++, so stick with cstdio
.包括
stdio.h
将printf
放在全局命名空间中,这通常不是 C++ 中想要的,所以坚持使用cstdio
。
It appears your implementation puts printf
in the global namespace even though you've only included a C++ header.即使您只包含了 C++ header,您的实现似乎也将
printf
放在了全局命名空间中。 That's unfortunate, but that happens too.这很不幸,但这也发生了。
Evidence: My preprocessor is called cpp
and I can use it to list the included header files.证据:我的预处理器叫做
cpp
,我可以用它来列出包含的 header 文件。 I have this program that I've called std.cpp
:我有这个程序,我称之为
std.cpp
:
#include <iostream>
int main() {}
and if I use cpp
to list a small subset of the included headers如果我使用
cpp
列出包含的标题的一小部分
cpp -M std.cpp | tr -d '\\' | tr ' ' '\n' | \
grep -E '^/[^\.]+$' | awk -F/ '{print $NF}'
I get these C++ headers on my system :我在我的系统上获得了这些 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
and yes, cstdio
is in there which also includes stdio.h
.是的,
cstdio
在那里,其中还包括stdio.h
。
As @Ted Lyngmo stated it's implementation defined , usually <cstdio>
is included, as is <cstdlib>
.正如@Ted Lyngmo 所说,它的实现已定义,通常包括
<cstdio>
,就像<cstdlib>
一样。
My <iostream>
header file includes:我的
<iostream>
header 文件包括:
#include <bits/c++config.h>
Which in term includes:其中包括:
/* Define if C99 functions or macros in <stdio.h> should be imported in
<cstdio> in namespace std for C++11. */
#define _GLIBCXX11_USE_C99_STDIO 1
And和
/* Define if C99 functions or macros in <stdio.h> should be imported in
<cstdio> in namespace std for C++98. */
#define _GLIBCXX98_USE_C99_STDIO 1
The same for <stdlib.h>
. <stdlib.h>
也是如此。
Implementation info:实施信息:
Thread model: posix
gcc version 9.2.0 (tdm64-1)
You can open the include file and see the following chain of includes: iostream => istream => ostream => ios => xlocknum => cstdio您可以打开包含文件并查看以下包含链: iostream => istream => ostream => ios => xlocknum => cstdio
The cstdio is C++ wrapper of stdio.h cstdio是stdio.h的 C++ 包装器
All highlighted names are standard headers, the chain between ios and cstdio is compiler-dependent (in my case xlocknum is internal from VS2017 compiler)所有突出显示的名称都是标准标题, ios和cstdio之间的链依赖于编译器(在我的情况下, xlocknum是 VS2017 编译器的内部)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.