Tell me where have crossed out the hex ends in the string

I want to print 10 Β° C with the function printf

I would normally do it like this:

printf("10\xF8Celsius");

      

where \ xF8 is the ANSI code for the exponent sign. The problem is that the compiler takes all the hex characters after the \ x and tries to convert to a character, basically it takes \F8Ce

and tries to convert it to a character.

I could write it like:

printf("10\xF8 Celsius"); //see additional space

      

but the question remains.

How do I tell the compiler where my hex code ends? Is it possible?

Note. I used Visual Studio 2015 PRE for Windows 8.1 to observe this issue (not to make the issue platform specific, but just to mention it)

+3


source to share


5 answers


From this link to the C ++ escape sequence :

Hexadecimal escapes have no length limit and end at the first character that is not a valid hexadecimal digit.



So, without any workarounds, there is simply no limit on the numbers that the compiler will read.

Note that the corresponding C reference says the same thing.

+2


source


The Hex escape sequence has no character limit, so - since the parser is greedy - it captures all the hexadecimal digits it can receive. C spec excerpt:

hexadecimal-escape-sequence:
    \x hexadecimal-digit
    hexadecimal-escape-sequence hexadecimal-digit

hexadecimal-digit: one of
    0 1 2 3 4 5 6 7 8 9
    a b c d e f
    A B C D E F

      

To fix this, split the string into 2 consecutive ones and they will be merged later (in translation phase # 6 ;-). So instead of:



printf("10\xF8Celsius");

      

using:

printf("10\xF8" "Celsius");

      

+3


source


The correct fix is ​​to use the printf function as intended. Have a look at the printf documentation.

printf("10%c Celsius", 0xF8)

      

+2


source


From the C11 specs 6.4.4.4 Character constants

hex-escape sequences:
         \ x hex-numeric
        hex-escape sequences hex-numeric

EXAMPLE 3 Even though eight bits are used for objects of type char, the '\ x123' construct specifies an integer character constant containing only one character, since the hexadecimal escape sequence is terminated only by a non-hexadecimal character. You can use '\ 0223' to specify an integer character constant containing two characters whose values ​​are '\ x12' and '3', since the octal escape sequence ends after three octal digits. (The value is this two-character integer character constant from the implementation.)

So, the parser will try to parse as many hex characters as possible.

One workaround is to use string concatenation:

printf("10\xF8" "Celsius");

      

Live example

or

printf("10%cCelsius", '\xF8');

      

Or, use the equivalent octal escape sequence, which must not exceed 3 characters.


NOTE. Usage '\xF8'

for printing is not portable and may not work on many platforms. You can use unicode string literal for portable code.

+1


source


You can use an octal escape sequence instead of a hexadecimal escape sequence. for example

printf("10\370Celsius");

      

From the C ++ standard (2.14.3 character literals)

4 escape \ ooo consists of a backslash followed by one, two or three octal digits, which are accepted to indicate the value of the desired character ....

+1


source







All Articles