简体   繁体   English

printf(“ \\ 033c”)是什么意思?

[英]What does printf(“\033c” ) mean?

I was looking for a way to "reset" my Unix terminal window after closing my program, and stumbled upon printf("\\033c" ); 关闭程序后,我一直在寻找一种“重置” Unix终端窗口的方法,偶然发现了printf("\\033c" ); which works perfectly, but I just can't understand it. 效果很好,但我听不懂。 I went to man console_codes and since I'm somewhat inexperienced with Unix c programming, it wasn't very helpful. 我去了man console_codes ,由于我对Unix c编程不了解,所以它不是很有帮助。

Could someone explain printf("\\033c" ); 有人可以解释一下printf("\\033c" ); ?

In C numbers starting with a leading zero are octal numbers. 在C中, 以前导零开头的数字是八进制数字。 Numbers in base 8. 以8为底​​的数字。

What it does is print the character represented by octal number 33 followed by a 'c' . 它所做的是打印由八进制数字33表示的字符,后跟一个'c'

In ASCII encoding the octal number 33 is the ESC (escape) character, which is a common prefix for terminal control sequences. ASCII编码中 ,八进制数字33ESC (转义)字符,它是终端控制序列的通用前缀。

With that knowledge searching for terminal control sequences we can find eg this VT100 control sequence reference ( VT100 was an old "dumb" terminal, and is emulated by most modern terminal programs). 借助搜索终端控制序列的知识,我们可以找到例如VT100控制序列参考VT100是一个古老的“哑”终端,并且被大多数现代终端程序所模拟)。 Using the VT100 reference we find <ESC>c in the terminal setup section, where it's documented as 使用VT100参考,我们在终端设置部分找到<ESC>c ,该文件记录为

Reset Device <ESC>c 重置设备<ESC>c

Reset all terminal settings to default. 将所有终端设置重置为默认值。


The ESC character could also be printed using "\\x1b" (still assuming ASCII encoding). ESC字符也可以使用"\\x1b" (仍然假定为ASCII编码)进行打印。 There is no way to use decimal numbers in constant string literals, only octal and hexadecimal. 无法在常量字符串文字中使用十进制数字,只能使用八进制和十六进制。

However (as noted by the commend by chux ) the sequence "\\x1bc" will not do the same as "\\033c" . 但是 (如所指出的chux的表彰序列) "\\x1bc" 不会做一样的"\\033c" That's because 0x1bc is a valid hexadecimal number, and the compiler is greedy when it parses such sequences. 这是因为0x1bc是有效的十六进制数,并且编译器在解析此类序列时会贪婪 It will print the character represented by the value 0x1bc instead, and I have no idea what it might be (depends on locale and terminal settings I suppose, might be prited as a Unicode character). 它会改为打印由值0x1bc表示的字符,我不知道它可能是什么(取决于我认为的语言环境和终端设置,可能应将其当作Unicode字符使用)。

That's an escape sequence used to reset a DEC VT100 (or compatible) terminal. 这是用于重置 DEC VT100(或兼容)端子的转义序列。 Some terminals (such as Linux console) accept VT100-style escape sequences, even when they are not actually VT100s. 某些终端(例如Linux控制台)接受VT100样式的转义序列,即使它们实际上不是VT100。

The \\033 is the ASCII escape character, which begins these sequences. \\033是ASCII转义字符,以这些序列开头。 Most are followed by another special character (this is a rare exception). 多数字符后跟另一个特殊字符(这是罕见的例外)。 XTerm Control Sequences lists that, along with others that are not followed by a special character. XTerm Control Sequences列出了该列表以及未带有特殊字符的其他字符。

In ECMA-48 it is possible to use a different character for the usual case, eg, [ for the *control sequence initiator. 在ECMA-48中,通常情况下可以使用其他字符,例如[ *控制序列启动器。

Resetting a real VT100 (in contrast to a terminal emulator) does more than clear the screen, as noted in Debian Bug report logs - #60377 "reset" broken for dumb terminals , but users of terminal emulators tend to assume it is a short way to clear the screen. 重置真正的VT100(与终端仿真器相比)并不能清除屏幕,正如Debian Bug报告日志中指出的那样-笨拙的终端#60377“重置”已损坏 ,但是终端仿真器的用户倾向于认为这是一种简短的方法清除屏幕。 The standard way would be something like this: 标准方式如下:

printf("\033[H\033[J");

The ncurses FAQ Why does reset log me out? ncurses常见问题解答为什么重置会使我注销? addresses that issue. 解决了这个问题。

Incidentally, users of terminal emulators also get other issues with the terminal confused. 顺便说一下,终端仿真器的用户也会因终端混乱而遇到其他问题。 The ncurses FAQ How do I get color with VT100? ncurses常见问题解答如何使用VT100着色? addresses one of those. 解决其中之一。

It clears the screen in Linux type operating systems (ubuntu, fedora etc...). 它会在Linux类型的操作系统(ubuntu,fedora等)中清除屏幕。 You can check the ASCII table here. 您可以在此处检查ASCII表。 Under octa 33 (decimal 27) you have ESC character. 在八进制33(十进制27)下,您具有ESC字符。 http://www.asciitable.com/index/asciifull.gif http://www.asciitable.com/index/asciifull.gif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM