The printf / fprintf / sprintf family supports a width field in its format specifier. I doubt for the case of the (not wide) argument of char arrays:
Does the width field mean bytes or characters?
What is the (de facto correct) behavior if the char array matches (say) the original UTF-8 string? (I know that usually I should use some kind of wide char type this is not the point)
For example, in
char s[] = "ni\xc3\xb1o";
fprintf(f,"%5s",s);
This function should try to output only 5 bytes (simple C characters) (and you take the responsibility of misalignment or other problems if two bytes lead to text characters)?
Or should he try to calculate the length of the "text characters" from the array? (decoding ... according to the current language?) (in the example, this will mean that the string has 4 unicode characters, so it will add a space to fill in).
The UPDATE . I agree with the answers, it is logical that the printf family does not work to distinguish simple C characters from bytes. The problem is that my glibc doest does not seem to fully respect this concept if the locale was installed earlier, and if one has (most commonly used today) LANG / LC_CTYPE = en_US.UTF-8
Example:
#include<stdio.h>
#include<locale.h>
main () {
char * locale = setlocale(LC_ALL, "");
char s[] = {'n','i', 0xc3,0xb1,'o',0};
printf("|%*s|\n",6,s);
printf("|%.*s|\n",4,s);
char s3[] = {'A',0xb1,'B',0};
printf("|%s|\n",s3);
printf("|%.*s|\n",15,s3);
}
, -POSIX-C, printf, , : (c ), Unicode. . char, , ( - "|" - )... . , utf-8, /. glibc?
glibc 2.11.1 (Fedora 12) ( glibc 2.3.6)
: - od: $ ./a.out | od -t cx1 :
0000000 | n i 303 261 o | \n | n i 303 261 | \n
7c 20 6e 69 c3 b1 6f 7c 0a 7c 6e 69 c3 b1 7c 0a
0000020 | A 261 B | \n |
7c 41 b1 42 7c 0a 7c
2 ( 2015 .). glibc ( 2.17, ). glibc-2.17-21.fc19 .