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. The Lounge
  3. not possible for printf to avoid scanning the format string - why?

not possible for printf to avoid scanning the format string - why?

Scheduled Pinned Locked Moved The Lounge
question
10 Posts 6 Posters 0 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
    mrchief_2000
    wrote on last edited by
    #1

    So I saw this via today's Insider: http://boredzo.org/helloworld/[^] Besides the fact that the author is incredibly shy of accepting user comments (he leaves no way of contacting him), I was amused by his comment: (For highly technical reasons, it is not possible for printf to avoid scanning the format string, even if no values were passed. Trust me.) I wanted to ask him, why is it so hard for printf to look for (args.length == 1) and avoid scanning the string? I haven't done any C programming for years now so my knowledge is rusty. I still remember (vaguely) that you can check for args or whatever the equivalent of it is in C. Is that not the case? So my fellow CPians, what is it about printf that makes it so hard?

    P L J 3 Replies Last reply
    0
    • M mrchief_2000

      So I saw this via today's Insider: http://boredzo.org/helloworld/[^] Besides the fact that the author is incredibly shy of accepting user comments (he leaves no way of contacting him), I was amused by his comment: (For highly technical reasons, it is not possible for printf to avoid scanning the format string, even if no values were passed. Trust me.) I wanted to ask him, why is it so hard for printf to look for (args.length == 1) and avoid scanning the string? I haven't done any C programming for years now so my knowledge is rusty. I still remember (vaguely) that you can check for args or whatever the equivalent of it is in C. Is that not the case? So my fellow CPians, what is it about printf that makes it so hard?

      P Offline
      P Offline
      peterchen
      wrote on last edited by
      #2

      It's not "deeply technical", but simple: printf is implemented using a variable argument list (indicated by ellipsis, ...). For this, the implementation needs to determine number and type of arguments from the first parameter - which requries scanning the format string. (technically, it could determine that by any other method - e.g. a global variable, or assume a fixed argument list, or scan until an argument is zero, or whaever, none of these would work for printf, though) BTW. That article wasn't that great, I must say. Some interesting points, but somewhat random.

      FILETIME to time_t
      | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

      M 1 Reply Last reply
      0
      • M mrchief_2000

        So I saw this via today's Insider: http://boredzo.org/helloworld/[^] Besides the fact that the author is incredibly shy of accepting user comments (he leaves no way of contacting him), I was amused by his comment: (For highly technical reasons, it is not possible for printf to avoid scanning the format string, even if no values were passed. Trust me.) I wanted to ask him, why is it so hard for printf to look for (args.length == 1) and avoid scanning the string? I haven't done any C programming for years now so my knowledge is rusty. I still remember (vaguely) that you can check for args or whatever the equivalent of it is in C. Is that not the case? So my fellow CPians, what is it about printf that makes it so hard?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        mrchief_2000 wrote:

        what is it about printf that makes it so hard?

        nothing really, printf is a powerful function which obviously reads and interprets its very first parameter. And the whole article is crap. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        P P S 3 Replies Last reply
        0
        • L Luc Pattyn

          mrchief_2000 wrote:

          what is it about printf that makes it so hard?

          nothing really, printf is a powerful function which obviously reads and interprets its very first parameter. And the whole article is crap. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          P Offline
          P Offline
          peterchen
          wrote on last edited by
          #4

          I particulary like the approach of "if stdout isn't working, we hope for stderr".

          FILETIME to time_t
          | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

          1 Reply Last reply
          0
          • L Luc Pattyn

            mrchief_2000 wrote:

            what is it about printf that makes it so hard?

            nothing really, printf is a powerful function which obviously reads and interprets its very first parameter. And the whole article is crap. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            It's ironic, given that he's supposed to be attacking bad code.

            I'm not a stalker, I just know things. Oh by the way, you're out of milk.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            M 1 Reply Last reply
            0
            • P Pete OHanlon

              It's ironic, given that he's supposed to be attacking bad code.

              I'm not a stalker, I just know things. Oh by the way, you're out of milk.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              M Offline
              M Offline
              mrchief_2000
              wrote on last edited by
              #6

              Glad that I'm not the only one thinks the article was random or sub-par. Sanity restored!

              1 Reply Last reply
              0
              • P peterchen

                It's not "deeply technical", but simple: printf is implemented using a variable argument list (indicated by ellipsis, ...). For this, the implementation needs to determine number and type of arguments from the first parameter - which requries scanning the format string. (technically, it could determine that by any other method - e.g. a global variable, or assume a fixed argument list, or scan until an argument is zero, or whaever, none of these would work for printf, though) BTW. That article wasn't that great, I must say. Some interesting points, but somewhat random.

                FILETIME to time_t
                | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

                M Offline
                M Offline
                mrchief_2000
                wrote on last edited by
                #7

                Thanks for shedding some light. But doesn't C have a way of supporting variable args in any other way (I'm thinking equivalent to C# 'params')?

                P 1 Reply Last reply
                0
                • M mrchief_2000

                  So I saw this via today's Insider: http://boredzo.org/helloworld/[^] Besides the fact that the author is incredibly shy of accepting user comments (he leaves no way of contacting him), I was amused by his comment: (For highly technical reasons, it is not possible for printf to avoid scanning the format string, even if no values were passed. Trust me.) I wanted to ask him, why is it so hard for printf to look for (args.length == 1) and avoid scanning the string? I haven't done any C programming for years now so my knowledge is rusty. I still remember (vaguely) that you can check for args or whatever the equivalent of it is in C. Is that not the case? So my fellow CPians, what is it about printf that makes it so hard?

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

                  It's not highly technical and is misleading. Any time you output a string, at some point you have to process it one character at a time if for no other reason that to stop processing it at the terminating zero. printf() does have specifiers that may require a second pass, but big deal; if you use printf(), it means you are formatting something. If that wasn't your intention, don't use it!

                  modified on Monday, April 4, 2011 1:47 PM

                  1 Reply Last reply
                  0
                  • M mrchief_2000

                    Thanks for shedding some light. But doesn't C have a way of supporting variable args in any other way (I'm thinking equivalent to C# 'params')?

                    P Offline
                    P Offline
                    peterchen
                    wrote on last edited by
                    #9

                    For a params[] - like construct to be useful, you would also need type information - somethng that simply doesn't exist in C.

                    FILETIME to time_t
                    | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      mrchief_2000 wrote:

                      what is it about printf that makes it so hard?

                      nothing really, printf is a powerful function which obviously reads and interprets its very first parameter. And the whole article is crap. :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #10

                      Luc Pattyn wrote:

                      And the whole article is crap.

                      That sounds harsh. While I agree the examples were somewhat random, the definitions of 'good code' subjective, the reasoning vague, the use of macros questionable, the ... oh, I see :)

                      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