Jump to content

Difference between \r and \n?

duckwithanokhat

In C code I know that \n makes a new line, but what does \r do? Pls explain in easy to understand english, wikipedia cannot do that for me.

Link to comment
Share on other sites

Link to post
Share on other sites

Look up carriage returns, they kind of do the same thing, but it also depends on the environment.

 

\r\n is a new line in windows for example. 

\n is a new line in unix

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, djdwosk97 said:

Look up carriage returns, they kind of do the same thing. 

So from what I looked up do carriage returns move your cursor to the beginning of the line you put the code in?

Link to comment
Share on other sites

Link to post
Share on other sites

In C (and not only C):

\n - new line

\r - carriage return

In Linux you only need \n, while in window you need \n\r to perform the same task: a new line with the cursor on the first column.

That is how the different operating systems were coded.

 

In C you would usually use these when you want to make a new line in a printed text (either in an output console or on some display box).

Gravitation cannot be held responsible for people falling in love. How on earth can you explain in terms of chemistry and physics so important a biological phenomenon as first love? Put your hand on a stove for a minute and it seems like an hour. Sit with that special girl for an hour and it seems like a minute. That's relativity.

Albert Einstein

Link to comment
Share on other sites

Link to post
Share on other sites

It all comes from typewriters that had two separate functions, one for returning the head to the left of the paper and one for advancing the paper a line. UNIX decided to do away with compatibility, dos and then windows didn't. Now it's just one of those annoying backward compatiblity things you have to deal with but both platforms mostly consume the other and ignore the differences except occasionally.

Link to comment
Share on other sites

Link to post
Share on other sites

/n simply starts a new line on a output./r can be used in a system.in in Java so when you click the console it automatically takes you to the next line to enter each value , for example.

public static int[] getIntegers(int number) {
    System.out.println("enter " + number + " numbers \r");
    int [] values = new int [number];

    for (int i=0; i<values.length; i++){
        values [i]= scanner.nextInt();
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×