What does CR, carriage return means?

What does CR, carriage return means?
0

#1

I’ve looked it up on google and every time I see this:

‘carriage return means moving the cursor to the beginning of the line.’

That doesn’t explain anything to me. And when they introduce the concept of typewriter and computer, it makes it even more confusing because i’ve never typed on a typewriter before so i cant picture it in my head. but obviously i use a computer all the time so i think \r is like hitting the ‘enter’ keyboard button, and the blinking ‘l’ going to a new line.

Is that what it means?


#2

It’s \n that works like pressing Enter . I am not really an expert in these things, but the first and second answers here, explain it pretty well.


#3

i created an image to illustrate my qns and the black rec bars are the blinking cursors.


#4

the difference between newline “\n” ascii 0x0a and carriage return “\r” ascii 0x0d is difficult to see in a web page

if you have a console terminal e.g. windows powershell or linux bash you can try the following oneliners - use ctrl-c to kill either command

# windows powershell command
# newline is `n - carriage return is `r
for($i=0;;++$i) { $line=if($i%2) {"`rcr $i"} else {"`nnl $i"} write-host -nonewline "$line"; sleep 5 }
# linux bash command
# newline is \n - carriage return is \r
for((i=0;;++i)); do ((i%2)) && line="\rcr $i" || line="\nnl $i"; echo -en "$line"; sleep 5; done

The output of either command is like this - every even numbered line starts with a newline 0x0a - every odd numbered line starts with a carriage return 0x0d

the first line appears

nl 0

then 5 seconds later - the line is overwritten because the carriage return begins new output at the beginning of the current line

cr 1

then 5 seconds later - a new second line appears

cr 1
nl 2

then 5 seconds later - the second line is overwritten due to the carriage return

cr 1
cr 3

so every “nl” line shows up for 5 seconds before being replaced with a “cr” line


#5

hmmm okayyy. but i use mac

thanks for ur effort and reply though. appreciate it!


#6

the bash command should work on mac too