not possible for printf to avoid scanning the format string - why?
-
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? -
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?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 -
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?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.
-
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.
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 -
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.
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
-
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
Glad that I'm not the only one thinks the article was random or sub-par. Sanity restored!
-
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 hierarchyThanks 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')?
-
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?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
-
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')?
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 -
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.
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 :)