3 ms·
And the first argument to printf (the format), what kind of string is that? And what kind of string does sprintf() produce?
by OskarS 5y ago
And the first argument to printf (the format), what kind of string is that? And what kind of string does sprintf() produce?
- TazeTSchnitzel 5y agosprintf tells you how many characters it has written, so there's no reason you can't use it for non-null-terminated strings.
- comfydragon 5y agowilliamvds's point is that the first argument to printf is still itself a null-terminated string, so it's basically turtles all the way down if you're using the C standard library.
- TazeTSchnitzel 5y agoTheir comment talked about two things, and the sibling comment addressed the other one.
- 10000truths 5y agoIn practice, it is almost always a compile-time-known string. gcc will warn you if it isn't, especially since allowing the use of untrusted input for the format can lead to vulnerabilities: https://en.wikipedia.org/wiki/Uncontrolled_format_string https://en.wikipedia.org/wiki/Uncontrolled_format_string
- tialaramex 5y agoAnywhere that this format is a variable, you probably already screwed up. C allows that, but if I see it that's getting flagged in my review. So long as the format string is a literal you needn't care how it works. Now, one of the places where C makes this nastier than it needed to be is that C built-in types are silly, and so any non-trivial program is using better fundamental types like uint32_t (or the more succinct u32), for which the built-in formatter offers no syntax. So you end up writing format strings like "There are "PRIu32" dogs\n" using macros to bring in the appropriate specifier for your literal. Blergh.