0

I am trying to understand carriage return(\r). So I have written the code below. If i run the below code, I am getting the output is "go".

Method1:

#include <stdio.h>
int main()
{

        printf("chi\rca\rgo");
        return 0;
}

But if i have tried with "\n", then i am getting the output "goi".

Method 2:

#include <stdio.h>
int main()
{
        printf("chi\rca\rgo\n");
        return 0;
}

What is the error in this?
Can anyone help me ?

bolov
  • 72,283
  • 15
  • 145
  • 224
Akaash
  • 61
  • 1
  • 8
  • 2
    [tag:linux] *Use this tag only if your question relates to programming using Linux APIs or Linux-specific behavior, not just because you happen to run your code on Linux.* – eyllanesc Sep 03 '18 at 10:34
  • 7
    My *guess* is that the prompt overwrites the `i` from the first output. – Some programmer dude Sep 03 '18 at 10:37
  • There's probably some other differences between outputs of the two versions, look carefully. – n. m. could be an AI Sep 03 '18 at 10:38
  • @Someprogrammerdude, I am also guessing that too. But how can we prove it that the prompt will overwrite the character? – Akaash Sep 03 '18 at 10:47
  • 4
    Flush the output (to make sure it's printed) and add a small delay before returning from the `main` function? If the output you see before the program exit is `goi` and after you only see `go` followed by the prompt, then you know. – Some programmer dude Sep 03 '18 at 10:51
  • Possible duplicate of [String is not printing without new line character in C++](https://stackoverflow.com/questions/4198675/string-is-not-printing-without-new-line-character-in-c) – phuclv Sep 03 '18 at 11:15
  • 3
    the behavior of `CR` is OS and app dependent. Because that makes a "Linux-specific behavior" the [tag:linux] tag is appropiate – bolov Sep 03 '18 at 11:15
  • same reason: [c stdout print without new line?](https://stackoverflow.com/q/7431425/995714), [printf anomaly after "fork()"](https://stackoverflow.com/q/2530663/995714), [printf without \n does not display text when placed before while(1)](https://stackoverflow.com/q/7384110/995714) – phuclv Sep 03 '18 at 11:16

1 Answers1

1

You should get "goi" in both cases. You can test it here.

Carriage return is literally just that - it returns the carriage, i.e. it goes back to the beginning of the line.

So when you print "chi\rca\rgo", this will happen to the output:

==============================
| 1. | print "chi"     | chi |
==============================
| 2. | carriage return | chi |
==============================
| 3. | print "ca"      | cai |
==============================
| 4. | carriage return | cai |
==============================
| 5. | print "go"      | goi |
==============================

Adding \n at the end of your output doesn't change that behavior. However terminals are often line buffered, meaning that a newline character behaves like a flush. It could be that you only see "go" in the first scenario, because without the newline character the terminal just hasn't printed all characters for that line yet.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • yeah... well what should be and what is are unfortunately different things. End of line is a complete fiasco across OSs. On old Mac `CR` aka `\r` was used as line break. Notepad++ interprets it as new line, Windows Notepad ignores it. WordPad as new line. – bolov Sep 03 '18 at 11:01
  • Well, OP tagged their question with *linux*, giving context as to the system and how `\r` will be interpreted. Unfortunately the tag got removed, even though it's relevant for the reasons you just gave. – Max Vollmer Sep 03 '18 at 11:05