1

When trying to use '\x05' in a string, the compiled C code seems to behave oddly when handling the value.

For instance when using the following string:

char *weird = "\x04\x68\x65\x79\x79\x03\x6e\x65\x74\x05\x6c\x6f\x63\x61\x6c\x00";

When printing each character of the string, the '\x05' is not corresponding to the integer 5, whereas the '\x04' and '\x03' are corresponding to the integers 4 and 3 respectively.

The following for loop demonstrates the problem:

for(int i = 0; i < strlen(question); i++){
    printf("question[%d] = %c\n", i, question[i]);
}

Output:

0x05

Any ideas why this specific case of '\x05' is so troublesome?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
reyyez
  • 365
  • 2
  • 3
  • 16
  • It behaves just fine. 3 and 4 don’t correspond to any integers. Your font just shows them that way. – Sami Kuhmonen Jan 31 '18 at 21:19
  • Are you trying to print the character '5'? If so, '\x05' is not what you're looking for. The ASCII code for '5' is '\x35'. – frslm Jan 31 '18 at 21:20
  • 1
    `0x03` is `ETX` (end of text), `0x04` is `EOT` (end of transmission) and `0x05` is `ENQ` (enquiry). These are not printable characters, but [control characters](https://en.wikipedia.org/wiki/Control_character). – Pablo Jan 31 '18 at 21:26
  • "Are" and "are not" printable depends on what the OP's terminal is designed to handle. Obviously, on OP's characters they *are* – whether they *should* is something else. I assume it's for convenience that something is shown, rather than "nothing" – or a total system crash. – Jongware Jan 31 '18 at 21:41
  • Assuming the screenshot is exactly what that code produces, it's more weird that the text changed. Try explaining *that*. – Jongware Jan 31 '18 at 21:51
  • 1
    Tip: To add clarity to what is printed, surround by sentinel characters --> `printf("question[%d] = [%c]\n", i, question[i]);` Add `[]`. Perhaps `'\0x05'` is printing a space? Better yet, try `for(int i = 0; i <= 48; i++) { printf("%2d = [%c]\n", i, i); }` – chux - Reinstate Monica Jan 31 '18 at 22:41

3 Answers3

4

%c format specifier is for the printable character representation for the corresponding integer value supplied as argument. As per the ASCII encoding, neither 0x03, 0x04 or 0x05 is a printable value!!

For sake of confirmation, you can pass the same value to isprint() before attempting to print that. In case you chose to print it anyway, the "display" finally depends on how your terminal chooses to interpret the display.

Here's some references (1, 2) which lists printable and non-printable ASCII values

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

Your assumption is wrong. ASCII codes 3, 4, and 5 do not correspond to (visible) integers 3, 4, and 5.

What you are seeing is your local terminal's translation of what it thinks you mean to print:

  1. It does not try to interpret the codes 3 and 4. These correspond to ASCII control characters End of Text and End of Transmission, but your terminal chooses to not handle them. Therefore, it writes these two codes literally into the output stream, which looks up a suitable font to display them. In modern (Unicode) fonts, there are no characters for these characters because they are not supposed to be "printed". However, some terminals are aware of that and provide a fall-back so you can at least see something.

  2. The code 5, on the other hand, is interpreted by your terminal: "Signal intended to trigger a response at the receiving end, to see if it is still present." The terminal sends that signal, receives (most likely) a "yes" answer – yes, the output stream is active – and continues outputting the rest of the text. As it is a signal and not a character, nothing is displayed.

This behavior is not unique for the 0x05 code. You don't mention it in your question, but you seem not suprised to get a new line when you "print" \n' even though it sends a code 0x0A to the output stream. And you do not get a 10 on screen but the cursor moves down a line. Again, this is because that code gets interpreted by the terminal to take some specific action.

Jongware
  • 22,200
  • 8
  • 54
  • 100
0

It is completelly ok. \x05 is not printable character in ASCII table that's why you see strange character.

%c expects printable character and your is not.

List of printable ASCII characters

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40