2

Carriage return \r is used to mark cursor to the start line.But when below code is compiled in turbo c it gives different output than from an online compiler !

#include<stdio.h>
main()
{
printf("abcd\r123");
getch();
}

Output:

123d

Code used in online compiler :

#include <stdio.h>

int main(void) {

     printf("abcd\r123");

    return 0;
}

Output :

enter image description here

What is the correct output ? Im confused also whats the reason for that behavior? [In both cases printf produces same result !]

Some references that could not help me understand:

What are carriage return, linefeed, and form feed?

What is the difference between a "line feed" and a "carriage return"?

Community
  • 1
  • 1
Tharif
  • 13,794
  • 9
  • 55
  • 77
  • how come that become a duplicate ? – Tharif Oct 27 '15 at 04:59
  • Can you tell about the OS/platform you are running locally as well as online (if you know). The most likely explanation for what you see is that `\r` is being intepreted as newline in one place but not the other. – Tim Biegeleisen Oct 27 '15 at 04:59
  • sure @TimBiegeleisen OS : Windows 7 (locally)..online compiler link : https://ideone.com/fAMDBC – Tharif Oct 27 '15 at 05:00
  • thank you @TimBiegeleisen would you please tell me whats the correct output ? shall i go with the first one ? – Tharif Oct 27 '15 at 05:05
  • It's hard to say what is "correct" here. Cats like eating fish, but fish like eating grass. Which one is right? It depends on what you want to really see (i.e. a newline or something else). – Tim Biegeleisen Oct 27 '15 at 05:06
  • So there is no standard implementation for that.it depends on platform or software ..thank you tim...but still i believe "cats like eating firsh" is correct rather "fish like eating grass" :) – Tharif Oct 27 '15 at 05:08
  • @TimBiegeleisen Isn't newline in Windows `\r\n`? Sorry if I'm wrong, it's been a while since I've coded for Windows. – Levi Oct 27 '15 at 05:09
  • exactly when we use \r\n ..but not when \r alone – Tharif Oct 27 '15 at 05:11
  • 2
    `\n = CR (Carriage Return)` :- new line character in Unix, `\r = LF (Line Feed)` :- new line character in Mac OS, `\n\r = CR + LF` :- new line character in Windows, `(char)13 = \n = CR` :- Same as \n – SKD Oct 27 '15 at 05:18

0 Answers0