The printf()
function uses the format specifier %s
to print char *
.
The standard does not specify how char
is implemented as signed or unsigned.
So when char
is implemented as signed char
, and we use %s
to print unsigned char *
, is it safe to do this?
Which format specifier should we use in this case?
... when
char
is implemented insigned char
, and we use"%s"
to printunsigned char*
. Is it safe to do this?
Yes it is safe.
char *cp = ...;
signed char *scp = ...;
unsigned char *ucp = ...;
printf("%s", cp); // OK.
printf("%s", scp); // OK.
printf("%s", ucp); // OK.
(%s) ... the argument shall be a pointer to the initial element of an array of character type. ... C11dr §7.21.6.1 8
The three types
char
,signed char
, andunsigned char
are collectively called the character types. C11 §6.2.5 15
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