Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I use printf() when including iostream?

This piece of codes works fine even with minGW compiler under C++11 standards:

#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(). Are they included in iostream? At least I didn't find them included. If you say that it's included in iostream, show me the evidence.

like image 444
Neo Avatar asked Jun 23 '20 11:06

Neo


People also ask

Can I use printf in C++?

The C++ printf() function is usually used in C programming but can also run in C++.

Why we dont use iostream h in C++?

Answers. iostream. h is deprecated and not a standard header. It was used in older programs before C++ was standardized, Functions like cout were defined inside iostream.

What does the #include iostream code do in C++?

So, #include is a preprocessor directive that tells the preprocessor to include header files in the program. < > indicate the start and end of the file name to be included. iostream is a header file that contains functions for input/output operations ( cin and cout ).

What can I use instead of iostream in C?

There is no analog to iostream in C -- it lacks objects and types. If you're using C++, it's the analog to <cstdio> . See also this fantastic question and its answer, 'printf' vs.


2 Answers

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.

Including stdio.h puts printf in the global namespace and that is usually not what one wants in C++, so stick with cstdio.

It appears your implementation puts printf in the global namespace even though you've only included a C++ header. That's unfortunate, but that happens too.


Evidence: My preprocessor is called cpp and I can use it to list the included header files. I have this program that I've called std.cpp:

#include <iostream>
int main() {}

and if I use cpp to list a small subset of the included headers

cpp -M std.cpp | tr -d '\\' | tr ' ' '\n' | \
grep -E '^/[^\.]+$' | awk -F/ '{print $NF}'

I get these C++ headers on my system:

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.

like image 166
Ted Lyngmo Avatar answered Oct 11 '22 00:10

Ted Lyngmo


As @Ted Lyngmo stated it's implementation defined, usually <cstdio> is included, as is <cstdlib>.

My <iostream> header file includes:

#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>.

Implementation info:

Thread model: posix
gcc version 9.2.0 (tdm64-1) 
like image 4
anastaciu Avatar answered Oct 11 '22 00:10

anastaciu