is a carriage return character; it tells your terminal emulator to move the cursor at the start of the line.
The cursor is the position where the next characters will be rendered.
So, printing a
allows to override the current line of the terminal emulator.
Tom Zych figured why the output of your program is o world
while the
is at the end of the line and you don't print anything after that:
When your program exits, the shell prints the command prompt. The terminal renders it where you left the cursor. Your program leaves the cursor at the start of the line, so the command prompt partly overrides the line you printed. This explains why you seen your command prompt followed by o world
.
The online compiler you mention just prints the raw output to the browser. The browser ignores control characters, so the
has no effect.
See https://en.wikipedia.org/wiki/Carriage_return
Here is a usage example of
:
#include <stdio.h>
#include <unistd.h>
int main()
{
char chars[] = {'-', '\', '|', '/'};
unsigned int i;
for (i = 0; ; ++i) {
printf("%c
", chars[i % sizeof(chars)]);
fflush(stdout);
usleep(200000);
}
return 0;
}
It repeatedly prints the characters -
|
/
at the same position to give the illusion of a rotating |
in the terminal.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…