5

Is the following well defined?

const char not_a_c_string[] = { 'h', 'e', 'l', 'l', 'o' };
printf( "%.5s", (const char*) not_a_c_string );

This is a question about the specific form "%.5s", and not an how to print a possibly not NUL-terminated string? as this question has already been answered here where the "%.*s" construct is suggested.

Community
  • 1
  • 1
YSC
  • 38,212
  • 9
  • 96
  • 149
  • 3
    http://stackoverflow.com/questions/3767284/using-printf-with-a-non-null-terminated-string – P.P Jan 12 '16 at 16:50
  • 2
    See the accepted answer of the dup. It does not matter when the length is determined. Just use a fixed size. I'd recommend to avoid magic numbers, though, either by using a bit macro-magic with a `WIDTH` macro or still passing the length explicitly. – too honest for this site Jan 12 '16 at 16:55
  • YSC, I have modified my answer based on the comment from Mr. @rici. If you really wanted to know about the _precision_, please [edit] the question and add the clarification to make it complete. Thanks. – Sourav Ghosh Jan 12 '16 at 17:21
  • 1
    I eill not retract my vote, because it **is** a dup. How about RTFM? `printf` is well-defined. Note that you specifiy the field-width. The proceision is another field. – too honest for this site Jan 12 '16 at 17:41
  • Curious, why the `(const char*)`? – chux - Reinstate Monica Jan 12 '16 at 18:00

1 Answers1

12

First of all, I believe, you meant to ask about the precision, not the field width. So, your example is to look like

 printf( "%.5s", (const char*) not_a_c_string );  //precision

instead of

 printf( "%5s", (const char*) not_a_c_string );   //field width.

Considering the above approach, no, it will not be UB in your example.

To quote the C11 standard, chapter §7.21.6.1, The fprintf function, paragraph 8, (emphasis mine)

s               If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type.(280) Characters from the array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.

So, you need to have a null delimited array (string) only if you're either

  • missing the precision
  • supplied precision is > the size of the supplied char array.

In your case, the mentioned precision (5) is not greater that the size of the array (also 5). So, It's fine.


FWIW, if the example remains

 printf( "%5s", (const char*) not_a_c_string );

then it will be UB, as you'll be missing precision there.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    @PaulR: I think you missed the first sentence: "No, it will **not** be UB in your example." (emphasis mine) He's agreeing with you – R_Kapp Jan 12 '16 at 16:55
  • Aha - my bad - apologies all round - down-vote and comments removed. – Paul R Jan 12 '16 at 16:55
  • 1
    @PaulR Thanks for revisiting. I've updated the answer to lose any possible confusion. :) – Sourav Ghosh Jan 12 '16 at 17:02
  • The supplied format (`"%5s"`) *is* missing the precision, so it is definitely UB. Had it been `"%.5s"`, your answer would apply. In the first, the `5` is the "field width". – rici Jan 12 '16 at 17:10
  • @rici Right, I missed that completely. Apologies. Let me update my answer. – Sourav Ghosh Jan 12 '16 at 17:15
  • I think it will never be UB, also with the precision not supplied. `fprintf` just prints to the file, and prints and prints and.... The only limitation is available storage for the file printed to. Garbage will be printed, but there is no UB of the `printf` function. – Paul Ogilvie Jan 12 '16 at 17:18
  • @PaulOgilvie how???as soon as you go past the allocated memory in the supplied argument, you'll invoke UB. – Sourav Ghosh Jan 12 '16 at 17:20
  • @SouravGhosh, you mean memory access violation? Yes that could happen. – Paul Ogilvie Jan 12 '16 at 17:27
  • @PaulOgilvie yes, right. Memory access violation also leads to UB, correct? – Sourav Ghosh Jan 12 '16 at 17:30
  • 1
    @PaulOgilvie: Sourav quoted from the standard's definition of `fprintf`, which clearly says that in this case the array must contain a NUL. Since the constraint is not satisfied, the behaviour of `fprintf` is undefined (by definition). – rici Jan 12 '16 at 17:45