Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
802 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...