4

I've read that \x1b represents the escape character, but isn't '\' by itself the escape character? A tutorial wants me to write

write(STDOUT_FILENO, "\x1b[2J", 4);

to clear the screen. Why won't '\[2J' do the job?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Luke Kong
  • 71
  • 1
  • 7
  • Two different escape characters for two completely different things. – Shawn May 24 '19 at 01:01
  • can you be more specific? – Luke Kong May 24 '19 at 01:02
  • 1
    `\x1b` represents an ESC character - look at an ASCII chart. \ is a backslash, which the C compiler uses to escape characters. `ESC` is not the same as `escape character`. The line of code outputs the `ESC` character, followed by a sequence of three other characters (`[23`) for a total of 4 characters. – Ken White May 24 '19 at 01:06
  • what is the difference between an escape character an an ESC character? – Luke Kong May 24 '19 at 01:15
  • " escape character" is a generic concept. `ESC` is a specifier ASCII value: 27. – chux - Reinstate Monica May 24 '19 at 02:30
  • You could use `\033` to make it an octal representation of the ESC character. It is confusing, but there are two different meanings for escape here — and the difference has been outlined by Ken White. – Jonathan Leffler May 24 '19 at 03:47

2 Answers2

5

Although this question is a bit old, I'll give my two cents using the \n as an example.

Although the ASCII escape character is \x1B, and n is \x6E, it's not possible to insert a \n character into a string using \x1B\x6E, for two reasons:

  1. The ESC character in ASCII table, and the escape character \ in programming languages are two different things.

  2. Programming languages search for \n (as a single entity), in the source code (before we're in bytes level) to convert it into line-feed bytes, so \x1B\x6E will not get converted to anything. Thus, even if you use \x5C\x6E in the source code(an ASCII \ and an ASCII n), it won't get converted to a line-feed.

Example in Python:

>>> print("Hello\x5C\x6EWorld")
Hello\nWorld
# if one still insists on inserting a new line using an ascii line-feed (\x0A), they can do:
>>> print("Hello\x0AWorld")
Hello
World

HTH.

aderchox
  • 3,163
  • 2
  • 28
  • 37
1

To elaborate the comments of Ken and Jonathan a bit: Escaping here is a general concept. It basically implies the change of something's interpretation. The escape character in ASCII originates from teletype machines, where the appearance of ESC has changed the interpretation of the following character. It is used in the same way by many terminals. E.g., the appearance of ESC before the string [2J cause the the string (i.e. the characters [,2, and J) aren't displayed at the terminal, but interpreted as the command "clear entire screen". Thus, the escape character has changed the interpretation mode.

However, there is no straightforward way to enter the ASCII escape character (because pressing there ESC key at your keyboard usually changes a mode in your editor, terminal or whatever), and even if you could, the allowed character set of the language C doesn't include it. Now, you need a second level of escaping: C has its own character for escaping: \. It changes the meaning of the next character(s). E.g., a precedent backslash changes the meaning (interpretation) of the following character n into newline. In addition, the \x (or \0, respectively) let the C parser interpret the following two (three) characters as hexadecimal (octal) number - instead as the characters themselves - and insert the character that corresponds to that very code number. Since the ASCII code for escape has the number 27 (decimal), or 1b (hexadecimal), or 33 (octal), respectively, you generate the ASCII escape character in C by \x1b or \033.

With other words, you escape in C with \ to generate the ASCII escape character, that in turn forces an escaping at your terminal.

Matthias
  • 8,018
  • 2
  • 27
  • 53