LoginSignup
5
5

More than 5 years have passed since last update.

About stdio.h header in C++

Last updated at Posted at 2014-10-08

C++におけるstdio.hヘッダについて

I wondered why I can use cstdio API with including only iostream.

iostreamをインクルードしただけでcstdioが使用可能になる点について疑問に思ったので調べてみました。

OSX (BSD)

I browsed c++ headers and found following path:

clang++-6.0
iostream -> ostream -> ios -> iosfwd -> wchar.h -> stdio.h
llvm-g++-4.2
iostream -> ostream -> ios -> iosfwd -> bits/c++io.h -> cstdio -> stdio.h
g++-mp-4.8
iostream -> ostream -> ios -> iosfwd -> bits/postypes.h -> cwchar -> wchar.h -> stdio.h

Also I have found this very interesting path:

clang++-6.0
queue -> vector -> memory -> iterator -> iosfwd
llvm-g++-4.2
queue -> vector -> bits/stl_algobase.h -> iosfwd
iterator -> ostream
g++-mp-4.8
iterator -> ostream

So, including iostream, ios or iterator is enough for stdio...

Linux (glibc)

Since glibc's stdio.h is guarded, the path is little bit different.

glibcのstdio.hはガードされているので、経路は少し異なります。

# 3.3
iostream -> ostream -> ios -> iosfwd -> bits/fpos.h -> bits/c++io.h -> cstdio -> stdio.h
# 3.4,4.1,4.2
iostream -> ostream -> ios -> iosfwd -> bits/c++io.h -> cstdio -> stdio.h
# 4.3
iostream -> ostream -> ios -> bits/char_traits.h -> cstdio -> stdio.h

The path iosfwd -> bits/c++io.h and bits/char_traits.h -> cstdio has disappeared from g++ 4.4.
That's why we need to include cstdio separately.

iosfwd -> bits/c++io.hおよびbits/char_traits.h -> cstdioの経路はg++ 4.4でなくなりました。
このためcstdioを別途インクルードしなければならないのです。

On the other hand,

iostream -> ostream -> ios -> __locale -> string -> cstdio -> stdio.h

this path is available (also on OSX). Really funny...
上記経路はLinuxにもOSXにも存在します。

Digression

以下余談。

MinGW

iostream -> ostream -> ios -> bits/localefwd.h -> bits/c++locale.h -> clocale -> locale.h -> stdio.h

wchar.h does not include stdio.h, but locale.h does...

Android NDK

stdio.h exists in include-fixed and it is not guarded, so the path of g++-mp-4.8 should be available. Confirmed with 4.4.3.

newlib

So firm, the implementation looks the wisest.
しっかりした作り。一番賢い実装な感じ。

5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5