top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between \r and \n in C/C++?

+3 votes
1,091 views
What is the difference between \r and \n in C/C++?
posted Oct 6, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

They're different characters. \r is carriage return, and \n is line feed.

On "old" printers, \r sent the print head back to the start of the line, and \n advanced the paper by one line. Both were therefore necessary to start printing on the next line.

Obviously that's somewhat irrelevant now, although depending on the console you may still be able to use \r to move to the start of the line and overwrite the existing text.

More importantly, Unix tends to use \n as a line separator; Windows tends to use \r\n as a line separator and Macs (up to OS 9) used to use \r as the line separator. (Mac OS X is Unix-y, so uses \n instead; there may be some compatibility situations where \r is used instead though.)
Eg:
printf("123\r");
printf("123\n");

O/P: 123

answer Oct 6, 2014 by Aarti Jain
...