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.
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 nol
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
>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With