简体   繁体   English

Printf带回车

[英]Printf with carriage return

printf("Hello%cWorld\n", '\r');

Outputs: 输出:

World

Because '\\r' moves the cursor to the beginning of the line 因为'\\r'将光标移动到行的开头

Can I trust that all terminals have this behavior? 我可以相信所有终端都有这种行为吗?

lib curses会告诉你终端的功能。

.0x0d in ASCII encoding is '\\r', in this case 在这种情况下,ASCII编码中的.0x0d是'\\ r'

printf("Hello%cWorld\n", 0x0d);

is equal to 等于

printf("Hello\rWorld\n");

A common C programming error is to assume a particular encoding is in use when, in fact, another one holds. 常见的C编程错误是假设在实际上另一个编码保持时使用特定编码。

However, it works on most computers as @Joachim Pileborg said. 但是,它适用于大多数计算机,如@Joachim Pileborg所说。

BUT I suggest to use '\\r' instead of 0x0d, for portability and the latter looks like a magic number, which makes others who read your code even yourself feel confused someday. 但我建议使用'\\ r'而不是0x0d,以便于移植,而后者看起来像一个神奇的数字,这使得读你的代码的其他人甚至有一天会感到困惑。

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

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