What this character sequence “\033[H\033[J” does in C? [duplicate]

天涯浪子 提交于 2021-01-27 11:32:29

问题


I have gone through the below strange sequence of characters in some random website. When compiled and executed, this Sequence cleared all previous content in terminal. Does it clear buffer in output stream or does it clear only tty buffers?.

int main()
{
   printf("\033[H\033[J");
   return 0;
}

回答1:


These are ANSI escape codes.

\033 stands for ESC (ANSI value 27).

ESC [ is a kind of escape sequence called Control Sequence Introducer (CSI).

CSI commands starts with ESC[ followed by zero or more parameters.

\033[H (ie, ESC[H) and \033[J are CSI codes.

\033[H moves the cursor to the top left corner of the screen (ie, the first column of the first row in the screen).

and

\033[J clears the part of the screen from the cursor to the end of the screen.

When used in combination, it results in the screen getting cleared with cursor positioned at the beginning of the screen.

This is the functionality that you get when using Ctrl+L or clear command on bash.

These CSI can have parameters as well. If none are provided, it will use the default values.




回答2:


If I am not mistaken, it makes use of ANSI/VT100 Terminal Control Escape Sequences.

\033 - ASCII escape character

[H - move the cursor to the home position

[J - erases the screen from the current line down to the bottom of the screen

However, this command may not be compatible in every terminal/console.



来源:https://stackoverflow.com/questions/55672661/what-this-character-sequence-033h-033j-does-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!