11

By reading this code, I stumbled upon the following printf() statement:

// reset, hide cursor and clear screen printf("\e[0m\e[?25l\e[2J"); 

I must admit that I am not a fully qualified C hacker and do not fully understand this. I tweaked around, removing the arguments, and I understand what it does (well, the comment actually says it all), but I have no idea how it's done. Also, this is something kind of hard to google for.

How does this printf() call work?

6
  • 7
    man 5 termcap or man 5 terminfo Commented Nov 30, 2014 at 15:11
  • 2
    From printf()'s perspective it works like any other printf() statement: It prints a sequence of chars to the Standard Output. Try redirecting the program's output to a file and then inspect it's content. Commented Nov 30, 2014 at 15:23
  • possible duplicate of Reset screen point to the top of screen in Windows & Linux console Commented Nov 30, 2014 at 15:43
  • stackoverflow.com/a/425208/4082723 Commented Nov 30, 2014 at 15:43
  • 2
    What you are seeing is a CSI (Control Sequence Introducer), and Wikipedia has a nice list of them. You can get text color effects on the console, clear the screen, move the cursor and more. They generally start with \033[, for instance to print bold you'd use the SGR CSI (m) with parameter 1 by printing "\033[1m". The SGR parameters are here and are such fun. Wikipedia also provides examples at the bottom. Commented Nov 30, 2014 at 17:26

2 Answers 2

14

This doesn't really have anything to do with printf. The C11 standard lists escape sequences in §5.2.2, and the list consists of \a, \b, \f, \n, \r, \t and \v. As an extension, GCC considers \e to be an escape sequence which stands for the ASCII character Esc (\E may work as well, or your compiler may support neither of them. Consult the documentation for your compiler). What follows are non-portable control sequences. They are not guaranteed to work the same in all terminals, or even work at all. The best way to know is to consult the documentation for your system.

§6.4.4.4 also describes octal escape sequences. For example, \033, where 033 is 27 in decimal, and therefore the escape character in ASCII. Similarly, you can use \x1b, which is a hexadecimal escape sequence specifying the same character.

If we inspect the output of the program with od -c, it shows 033.

(✿´‿`) ~/test> ./a.out | od -c 0000000 033 [ 0 m 033 [ ? 2 5 l 033 [ 2 J 0000016 

The ANSI escape sequences are interpreted by terminal emulators. C will convert the octal/hexadecimal escape sequences to the ASCII Esc character. Your compiler, as an extension, might also convert \e or \E. As requested, a brief explanation of what the control sequences are doing:

  • [0m: resets all the SGR attributes
  • [?25l: hides the cursor
  • [2J: from Wikipedia:

    Clears part of the screen. If n is 0 (or missing), clear from cursor to end of screen. If n is 1, clear from cursor to beginning of the screen. If n is 2, clear entire screen ...

Sign up to request clarification or add additional context in comments.

1 Comment

Could you add a more detailed explanation of what exactly the printf command in the question does?
7

The printf() call is simply outputting a specific series of byte values. The "magic" is that those values are special in the terminal.

A special series of bytes starting with the ASCII "escape" character is called an "escape sequence". These were invented for serial data terminals, where the only means of communication with the terminal was by sending byte values through the serial connection. Ordinary characters are simply displayed on the terminal, but it was desirable to have a way to move the cursor, clear the screen, etc. and most terminals used escape sequences for this.

http://en.wikipedia.org/wiki/Escape_sequence

There was one particularly popular terminal called the "VT100", and most terminal emulators today operate using VT100 escape sequences.

Even today, escape sequences are useful. You can write a simple C program that will work on the terminal emulators in Linux, Mac, Windows, mobile devices, basically everywhere. When you need to do something simple like clear the screen, just outputting the proper escape sequence is the easiest way.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.