Need help with \n and \r

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This sample code below is a small part of code that i found on the internet.

 

What is the meaning of the \n  and the  \r ...must the values be preloaded?

 

The complete code with librarys can be found in Google search for >> ''3A Variable Bench PSU With Digital Readout on Colour Display Using ATMEGA8 8 steps.''

 

* Instructable Project link. http://www.instructables.com/id/...

 

//====== small part of code ============//

void ili9341_write(uint8_t c)//write a character at setted coordinates after setting location and colour
{
if (c == '\n')
{
cursor_y += textsize*8;
cursor_x  = 0;
} else if (c == '\r')
{
 // skip em
}
else
{
ili9341_drawchar(cursor_x, cursor_y, c, textcolour, textbgcolour, textsize);
cursor_x += textsize*6;
}
}

Last Edited: Wed. Aug 16, 2017 - 12:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Get out your C manual. \n is "newline" or character 0x0A and \r is "return" or 0x0D

 

EDIT: if your C manual does not cover this(*) then Wikipedia does:

 

https://en.wikipedia.org/wiki/Es...

 

(*) and if it doesn't take it out to the back garden and burn it!

 

EDIT2: Oh and the C standard has this:

Last Edited: Wed. Aug 16, 2017 - 09:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
 if your C manual does not cover this, then ...   burn it!

Absolutely!!

 

Here are some 'C' learning & reference materials for you: http://blog.antronics.co.uk/2011... - including a free, online textbook - which lists the Escape Sequences here: http://publications.gbdirect.co....

 

 

 

Last Edited: Wed. Aug 16, 2017 - 09:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW it seems too easy to find an illegal PDF of the bible online but this is what it has to say on the subject....

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@mikedb

 

FYI: The author of that project  is Sharanyadas, who is a member here.

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:
 Sharanyadas, who is a member here.

https://community.atmel.com/users/sharanyadas

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wow. blast from the way past. this all goes back to the venerable ASR 33 teletype - the first ASCII terminal - which Kernighan and Ritchie took as their text model for Unix. In very early Unix days (Bell Labs V6), \n was interpreted as 2 characters - carriage return and line feed (\r and \l). On the ASR 33, line feed simply advanced the paper one line forward and carriage return brought the print head to column 1 (0?, I disremember).  So you needed both to get a "new line". Now it seems \n is called out as line feed (LF). This is the source of numerous problems today as some systems use a single character for end of line in text files and some use 2 characters (cr and lf). Some text programs understand this and some don't. Worst offender is windows notepad (which will probably never die the death it so rightfully deserves).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

philba wrote:
ASR 33 teletype - the (sic) first ASCII terminal

Wikipedia says one of the first: https://en.wikipedia.org/wiki/Te...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In C, the handling of line separation is one of the things that differentiates binary files from text files.

For a binary file, except for possible extra zeros at the end,

bytes are read and written as-are.

For a text file, the toolchain or the OS,

possibly a platform-specific library, knows what the line-separating sequence is.

A '\n' is written as that sequence.

That sequence is read as a '\n'.

IIRC a bare '\n' and a bare '\r' are read as themselves.
 

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

skeeve wrote:
In C, the handling of line separation is one of the things that differentiates binary files from text files.

Not just 'C' - that's an OS thing

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

5.2.2 above seems pretty clear:

 

 

That doesn't just say "move to next line" but "move to initial position on next line". The problem if there is a problem is that some systems simply treat 0x0A as "down one line" but if, for example the current output position is column 43 then it is column 43 on the next line it moves to. In such systems you need \r\n so that the \r gets you back to the initial column and the \n gets you down a line. But they seem to be acting in contravention of what 5.2.2 says.

 

Microsoft chose to treat \r\n from the Unix/Posix people so their line endings in text files tend to be 0x0D,0x0A rather than just 0x0A as found in Unix files.

 

Most terminals and editor programs that care about line endings can cope with both and will usually auto detect/adapt or give the user the option to control it.

 

In the original code it's now fairly obvious what's going on:

if (c == '\n')
{
    cursor_y += textsize*8;
    cursor_x  = 0;
} else if (c == '\r')
{
    // skip em
}
else
{
    ili9341_drawchar(cursor_x, cursor_y, c, textcolour, textbgcolour, textsize);
    cursor_x += textsize*6;
}

So if \n (move to start of new line) arrives he just does that - the y is moved down by the height of a text line and the x is set back to 0.

 

If \r arrives he chooses to do nothing (because if it was really "\r\n" then the \n will handle all that's required anyway). This is "Unix like".

 

If any other character arrives he prints it then moves X on by the width of a character.

Last Edited: Wed. Aug 16, 2017 - 02:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 '\r' and '\n' mean so much as "go to the next line" and "go to the beginning of the line.

So what to do if you want to go to the begining of the next line?

It's one of the oldest computer problems and it has / will never be solved properly I'm afraid.

 

Every combination of one or both those characters has been used over the years andit sticks it's head up whenever you want to interchange text between different (operating) systems.

When opening a file in C and reading it in "text" mode I believe some implicit translation is being done (but not if the file is opened in "binary" mode) and that translation changes depending on the OS the program is compiled on.

The result of the mess is that lots of software which handles text in some way tries it's best to "guess" what combination is being used.

Because there is no decent standard for this & too many legacy implementations you'll see that for example with almost any terminal emulator you have some options to adjust between different combinations of characters for "go to the begining of the next empty line"

Lot's of text editors have options to translate between "unix" and "windows" format.

Even git has built in options for changing these when commiting / cheking out files.

 

So this snippet of your code just ignores all '\r' characters found in the input. Simple, but often effective enough.

} else if (c == '\r')
{
 // skip em
}

 

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

Last Edited: Wed. Aug 16, 2017 - 02:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wow!!!

This is a big response to my small question...Thanks for all the info.

 

As i see this now that i know what \n and \r means for a PRINTER is as follow:

1.My Mega328p does not know what \n means.

2.\n is used as a special character placed in a string of other printable characters to change the current event.

3.My code must constantly test if the \n is present and then follow the next set op instructions,does not have to be NEW LINE.

4.\ is a character and n is the second character ,i dont know what the two dots before and after the \n means for the c compiler.

5.The \n could be replaced with any other character?
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mikedb wrote:
what \n and \r means for a PRINTER is as follow

\n and \r do no mean anything at all to a printer as such.

 

Printers (assuming they use ASCII) respond to the ASCII codes 0D for Carriage Return (CR) and 0A for Line Feed (LF)

 

\n and \r are simply forms of notation in the 'C' programming language used to represent these special character codes that cannot otherwise be typed into the source text.

 

1.My Mega328p does not know what \n means.

True.

2.\n is used as a special character placed in a string of other printable characters

True.

to change the current event.

I don't know what you mean by that.

 

3.My code must constantly test if the \n is present and then follow the next set op instructions,does not have to be NEW LINE.

That doesn't really make sense:

If your code is reading text, and \n has some specific meaning in that text, then your code must apply that meaning.

 

 

4.\ is a character and n is the second character

Yes. But when combined together as \n in 'C' source text, the combination represents a single character.

 

,i dont know what the two dots before and after the \n means for the c compiler.

"Dots" ??

Do you mean the single quotes?

Again, this is basic 'C' textbook stuff - see #4.

 

5.The \n could be replaced with any other character?

Not if \n is the character that you need!

 

 

Last Edited: Thu. Aug 17, 2017 - 06:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are 2 separate issues here:

 

  1. What \r and \n mean in 'C' source text;
     
  2. The effects & handling of the CR and LF ASCII control characters elsewhere in various systems.

 

As far as 'C' source text goes, \r represents CR and \n represents LF.

 

Elsewhere in various systems, there may be various conversions implied ...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
\n and \r do no mean anything at all to a printer as such.
+1

 

They are for any "serialized text stream". That can be a printer, an LCD, a UART terminal or any other "output device" where text appears and is displayed. A printer is just one of these things.

 

Your very own example in #1 is a graphic LCD for example.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
skeeve wrote:
In C, the handling of line separation is one of the things that differentiates binary files from text files.

Not just 'C' - that's an OS thing

The OS certainly set the convention.
As the application lacks the knowledge to do so,

at some point, the OS must take its idea of a next
line sequence and translate it into the next line
sequence for a printer, LCD or other text device.
The OS might or might not do other text-specific things.
If not, the application would need to translate newline
characters into the OS's idea of a next line sequence.

 

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?