Is printf()'s string width safe with unterminated strings?

谁说我不能喝 提交于 2019-12-30 22:59:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/34749278/is-printfs-string-width-safe-with-unterminated-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!