Reputation: 69
I am trying to printf a string that shows a temperature table
printf("TABLE 24A (20\°C)");
The degree sign is a constant I have defined as 0xDF
so the the string looks like this: "TABLE 24A (20\xDF C)"
This works but looks incorrect because of the space between the \xDF
and the C
.
If I remove the space the compiler issues a warning hex escape sequence out of range.
If I modify the string to "TABLE 24A (20\xDF\C)"
I get the correct result but the compiler issues warning unknown escape sequence: '\C'
Is there a way to get rid of the warnings but lose the space between the two characters?
Upvotes: 3
Views: 2518
Reputation: 362127
\x
escape sequences consume as many adjacent hex digits as possible. The C
is being parsed as a hex digit.
With \x
, you could combine two adjacent string literals.
printf("**TABLE 24A (20\xDF""C)**");
Or use a \unnnn
Unicode escape, which is limited to four hex characters.
printf("**TABLE 24A (20\u00DFC)**");
Or octal \nnn
:
printf("**TABLE 24A (20\337C)**");
Upvotes: 8
Reputation: 225507
You can take advantage of the fact that consecutive string literals are automatically concatenated:
printf("**TABLE 24A (20\xDF" "C)**");
This prevents the parser from consuming more characters for the escape sequence than you want.
You could also pass in the character as a parameter and use the %c
format specifier to print it:
printf("**TABLE 24A (20%cC)**", '\xDF');
Upvotes: 9