Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. printf without stdio.h

printf without stdio.h

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studioquestion
7 Posts 5 Posters 37 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    I am in Microsoft Visual Studio Community 2022 (64-bit). I noticed that I can use printf even without including stdio.h. It works if I do this:

    #include

    Any idea why this works? Thanks.

    Mircea NeacsuM V K 3 Replies Last reply
    0
    • M mike7411

      I am in Microsoft Visual Studio Community 2022 (64-bit). I noticed that I can use printf even without including stdio.h. It works if I do this:

      #include

      Any idea why this works? Thanks.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      The chain of include files can be long and tortuous. In this particular case: iostream -> istream -> ostream -> ios -> cstdio -> stdio.h Should you rely on compiler include chains? Depends if you expect your code to be compiled with a different compiler and how long the chain is. In this particular case, there is a strong probability that all C++ iostream operations will end up like C function calls so you are pretty safe.

      Mircea

      M 1 Reply Last reply
      0
      • M mike7411

        I am in Microsoft Visual Studio Community 2022 (64-bit). I noticed that I can use printf even without including stdio.h. It works if I do this:

        #include

        Any idea why this works? Thanks.

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        mike7411 wrote:

        It works if I do this: C++ #include <iostream> Any idea why this works?

        I guess, because there is a declaration of printf or or this is #included

        1 Reply Last reply
        0
        • M mike7411

          I am in Microsoft Visual Studio Community 2022 (64-bit). I noticed that I can use printf even without including stdio.h. It works if I do this:

          #include

          Any idea why this works? Thanks.

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          iostream provides very complex facilities to C++, and requires a great deal of supporting infrastructure. You can get a list of all the include files that a compilation unit needs by using the /showIncludes flag in your compiles (-H for gcc/clang). Using [Compiler Explorer](https://godbolt.org/), I see over 100 additional files included, including new, string, istream, ostream, atomic and cstdio (which gets you printf), just to name a few. As others have pointed out, you can just go ahead and use printf for example, and not add #include to your source file. However, as compilers evolve, you may find that this is no longer the case, at which point your code will fail to compile. Its good practice to #include all the relevant headers. System headers, at least, will have guards against multiple inclusions, so there's no harm in being explicit about what headers your program, or module, requires.

          "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

          J 1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            The chain of include files can be long and tortuous. In this particular case: iostream -> istream -> ostream -> ios -> cstdio -> stdio.h Should you rely on compiler include chains? Depends if you expect your code to be compiled with a different compiler and how long the chain is. In this particular case, there is a strong probability that all C++ iostream operations will end up like C function calls so you are pretty safe.

            Mircea

            M Offline
            M Offline
            mike7411
            wrote on last edited by
            #5

            I'm looking at the ios file, and I don't see where cstdio gets included. I did a search in the file for cstdio, and it doesn't seem to be there. Thanks.

            Mircea NeacsuM 1 Reply Last reply
            0
            • M mike7411

              I'm looking at the ios file, and I don't see where cstdio gets included. I did a search in the file for cstdio, and it doesn't seem to be there. Thanks.

              Mircea NeacsuM Offline
              Mircea NeacsuM Offline
              Mircea Neacsu
              wrote on last edited by
              #6

              I skipped a step: ios -> xlocnum -> cstdio.

              Mircea

              1 Reply Last reply
              0
              • K k5054

                iostream provides very complex facilities to C++, and requires a great deal of supporting infrastructure. You can get a list of all the include files that a compilation unit needs by using the /showIncludes flag in your compiles (-H for gcc/clang). Using [Compiler Explorer](https://godbolt.org/), I see over 100 additional files included, including new, string, istream, ostream, atomic and cstdio (which gets you printf), just to name a few. As others have pointed out, you can just go ahead and use printf for example, and not add #include to your source file. However, as compilers evolve, you may find that this is no longer the case, at which point your code will fail to compile. Its good practice to #include all the relevant headers. System headers, at least, will have guards against multiple inclusions, so there's no harm in being explicit about what headers your program, or module, requires.

                "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                k5054 wrote:

                Its good practice to #include all the relevant headers

                I agree. This specific case is rather simplistic but in larger applications with company code all over the place relying on chains is just a maintenance (preventable) problem waiting to happen.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups