Small Doubt about \r escape sequence

Hi guys!
I read on a forum on this website that it is OK to put doubts on the C language here. I hope you don't mind.

The \r escape sequence returns the cursor to the start of the line. So when I learned that, I just put some random text in a printf() function and in the middle of it I put \r.
The \r sequence wrote the rest of the string. But I noticed that it wrote the first part of the string before the \r sequence only to the number of characters as in the part "This is a sample code".

question is why does \r do that? Shouldn't it skip the part before it and continue printing from what's after it?
And if it does so, why not the whole string? why only a fixed amount of characters?

I'm using Microsoft Visual Studio 2015.


1
2
3
4
5
6
7
8
  #include <stdio.h>
int main(void)
{
	printf("Hello,This is a very very very long piece of text which is as you see a very long text and its just going on and on and on... \r This is a sample code \n");

	
	return 0;
}
The console acts as a metaphor for a typewriter or a dot matrix printer. '\r' literally just means "return the writing head to the start of the line". If you write more text at this point, only the text that there previously will be overwritten. Any text that was at a character position higher will remain where it was.
For example,
printf("AAAAA\rBB");

(_ is the writing head position)
_ (initial state)
A_
AA_
AAA_
AAAA_
AAAAA_
AAAAA
BAAAA
BBAAA

printf("hello")

BBhAA
BBheA
BBhel_
BBhell_
BBhello_
Last edited on
Topic archived. No new replies allowed.