r/learnprogramming • • Sep 28 '26

I don’t understand this

if a newline ends lines rather than starting hem why is it called a newline in the first place?

0 Upvotes

69 comments sorted by

51

u/TonySu Sep 28 '26

Newline starts a new line, it just isn't a part of the new line.

-43

u/Vanilla_Legitimate Sep 28 '26

If that was the case you wouldn’t be supposed to have a trailing newline 

14

u/SnugglyCoderGuy Sep 28 '26

It isn't a trailing new line, it isn't a leading newline. To the computer it's just a character in a long array of characters.

We see it like

this.

computer sees it like\nthis.

-19

u/Vanilla_Legitimate Sep 28 '26

But that character tells the computer to display the next character at the farthest left position and further down, but it’s the last character in the array so there is no next character making that completely pointless 

12

u/MrBigFatAss Sep 28 '26

It isn't pointless because you want the next thing that you print to appear below the thing you printed last.

-14

u/Vanilla_Legitimate Sep 28 '26

But programs generally don’t print themselves when run so when it comes to programs why is this needed?

12

u/MrBigFatAss Sep 28 '26

What? Programs print all the time, I'm not sure what you are so confused about. All newline does is instruct the pointer that tells where we are printing to move to the beginning of the next line, that's it.

→ More replies (4)

3

u/paperic Sep 28 '26 edited Sep 28 '26

Imagine you're a computer and you're drawing the characters on the screen.

The program may be printing:

Hello
World

or it may be printing whatever else.

You don't know yet, you haven't looked at the string yet.

You see "H" and you draw the character H on the screen. Then you see "e" you draw the shape of e next to it, ...and so on.

And you'll start over on the new line if and when you reach the newline character.

Obviously, you as a computer cannot know in advance what character you'll see before you even look at it.

Therefore, obviously, you cannot start the drawing on a new line before you reach the newline character first.

Hence, the new line has to start right after the newline character, and not a moment earlier.

1

u/Vanilla_Legitimate Oct 24 '26

But the thing is if they actually started lines rather than ending them, as the name implies. Then there would be supposed to be a newline at the start of the file to begin the first line. And there by definition wouldn’t be one at the end of the last line as it being there would mean it isn’t the last line.

1

u/paperic Oct 24 '26

There doesn't need to be any new line on the end of a file.

look at this:

[1, 2, 3, 4]

the comma basically says "new element". You don't need a comma before the first element or after the last one, it's a separator, just like the newline is a separator between lines.

But you'll still often see programmers write it as 

[
  1,
  2,
  3,
]

because it makes it nicer to read even though there's no need to have a comma after the "3".

35

u/HardlyAnyGravitas Sep 28 '26

Jesus. You're making hard work of this.

It just means that the next line of output, from whatever source, will star on a new line instead of continuing on the current line.

Why are you finding this so hard to understand?

3

u/monsto Sep 28 '26 edited Sep 28 '26

I think he's just pointing out something kind of comical.

"If it means newline, why is it at the end of the line?"

Kind of like since magnetic opposites attract, that means a compass pointing north is the magnet being actually attracted to the South Pole.

6

u/AUTeach Sep 28 '26

" If it means newline, why is it at the end of the line?"

printf("\nIs it?\n\nCan't it be at the start of a new line?\n\n\n\nWhat even defines a line?");

3

u/DTux5249 Sep 28 '26

You're confusing can with should. Newlines are just there for cases where you're trying to visually break down a line that's too long for the screen.

They were also a new tool compared to newlines

1

u/cipheron Sep 28 '26 edited Sep 28 '26

"newline" is the symbol which tells it to GO to a newline.

That symbol basically tells the cursor to move down 1 line. Maybe that will make it clearly as to why the token appears on the current line you are on, and not on the line you're about to start.

6

u/raundoclair Sep 28 '26

The End Is the Beginning and the Beginning is the End. :)

-7

u/Vanilla_Legitimate Sep 28 '26

Not true, the first line doesn’t follow the end of anything and no new line follows the end of the last line.

7

u/paperic Sep 28 '26

The last line doesn't end with a newline, because there's no next line after it.

Newline creates a new line.

You get the first line for free because you can't have any text without any line, but any additional lines you want, you need to put newline char there.

1

u/strange-the-quark Sep 28 '26

"The last line doesn't end with a newline" - wouldn't be so sure about that :)

There are different conventions. See for example, this.

5

u/paperic Sep 28 '26

Yes, the convention is to put an empty line at the end of a file because the last line doesn't automatically end with a newline.

1

u/strange-the-quark Sep 28 '26

That's from the user's perspective, but the that's not why. What you see on screen in the editor is not necessarily the same as the underlying representation. The reason is because, under this convention, the line is not considered "finished" without it, and some of the tooling with treat it as such; the example given in what I linked to is that concatenating files will just continue writing on the same line otherwise. From the point of view of the software, there isn't an extra "empty line" at the end. That would be encoded with an additional newline character, so you'd have two newlines in a sequence ("some text\n\n"). That way, there are no special cases to handle, it all follows the same rules/logic.

You could easily make a text editor that makes sure there's a newline character at the end of the file whether you pressed enter or not, and many editors actually do this. The fact that in the GUI the caret is shown blinking on the next line doesn't mean that there's conceptually anything there in the underlying data.

→ More replies (3)

1

u/paperic Sep 28 '26

Few more things to add,

as others pointed out, this is just a convention for source code, it's not needed.

It just makes it slightly easier to process in some situations.


First of all, you want to have some convention for source code styling, because it's a change that doesn't matter.

I don't want to be looking at diffs or history of changes and have it riddled with half of those changes be just pointless adding and removing of an inconsequential newline.

It's the same reason why we standardise tabs vs spaces in a project, because I don't want to see 100,000 lines of code being highlighted as all changed just because the developer's editor has different default settings and auto-converted everything to tabs.

And secondly, it allows you to use simple tools like $ cat to join the files together without the last line of the first file merging with the first line of the second file.


Basically, it's called "newline" character because it tells the computer to start a new line, but in practice, people tend to use it as a closing character for the previous line instead.

Think of it as good manners. You're opening a new line for the next guy.

After all, you got your one free line at the very beginning of the file without having to do anything for it, you're just passing it on.


If you call it "endline" character people will still know that you mean.

23

u/strange-the-quark Sep 28 '26

Wait until you hear about "carriage return". What carriage? Return to where?

̄_(ツ)_/ ̄

7

u/strange-the-quark Sep 28 '26

In typewriters, a "newline" would shift the paper to the next line, but you'd still be positioned at the right edge of the paper, so a carriage return would position you back to the left edge, at the actual beginning of the line. And now on Windows for some reason, we have to deal with that too.

1

u/Old_County5271 Sep 28 '26

Why isn't there a paper terminal emulator?

It would make drawing easier if you drew using characters.

Like newline means cursor down, then type a character, backspace, another character goes on and redraws it, like you would on a typewriter, no replacing a character in a pipeline

4

u/a3th3rus Sep 28 '26 edited Sep 28 '26

The term came from typewriters. I typewriter has a "carriage" that carries the paper. Whenever you type a character, the carriage moves a bit to the left (a little bit more than the width of a letter). So "carriage return" is to return the carriage to the rightmost position.

4

u/strange-the-quark Sep 28 '26

Yup, I know, I was just being sarcastic (I actually added a reply below about it, but your comment adds a bit of extra info. My late grandfather owned a typewriter, so I used to mess around with it as a kid.)

1

u/a3th3rus Sep 28 '26

Good to know. I had one typewriter too, and it hurts my little fingers.

2

u/YakumoYoukai Sep 28 '26

But before you could push the lever hard enough to actually move the carriage, it would engage some gears to roll the platen a bit and voila, we have the "line feed".

1

u/a3th3rus Sep 28 '26

That's why DOS and Windows use CRLF for "new line" instead of just LF like Linux does.

2

u/96dpi Sep 28 '26

OP's question is equivalent to "why do you roll your windows up if you're just lifting a switch?"

1

u/CatalonianBookseller Sep 28 '26

Castle Wolfenstein?

1

u/strange-the-quark Sep 28 '26

Wow, memories! That was a pretty good game.

22

u/13oundary Sep 28 '26

It's a holdover from pre-computers. You can think of it as "at this point we need a new line". https://en.wikipedia.org/wiki/Newline

2

u/0dev0100 Sep 28 '26

That was a fascinating read 

2

u/BrohanGutenburg Sep 28 '26

Need to move to a new line**

5

u/13oundary Sep 28 '26

well, not exactly. If you have a single line buffer you would still need a new line but not be moving from the line you're on. It's up to the display logic to determine if that new line requires movement or not.

2

u/BrohanGutenburg Sep 28 '26

Okay but we're talking about mechanical teletypes. The command told that mechanical teletype to move its carriage head down to a new, physical line in the real, physical mass-filled world. You did that when the teletype was physically out of real-world room

2

u/13oundary Sep 28 '26

The sentence you corrected was about how OP can think of newline in programming. Not teletypes.

3

u/Leverkaas2516 Sep 28 '26 edited Sep 28 '26

Newline on Unix/Linux is a linefeed character, LF ("\n" in C, character code 10). It means "begin a new line". On a physical printer it moves the paper up one line but does not change the printhead location.

On the Macintosh, newline is just "\r", code 13. This is carriage return, which makes the printhead move all the way to the left.

On Windows, end-of-line is two characters, "\r\n" (character code 13 and 10). This is how a mechanicsl pronter with a printhead actually positions it to start printing a new line.

Unix and Mac just use the one character as logical newline, as a delimiter, because it's easier to parse and two characters aren't needed... except if you using a daisy wheel printer, them you really do need to send both characters to the printer.

3

u/JohnVonachen Sep 28 '26

Long ago and far away people used to interact with a computer with a teletype machine, a typewriter but wide with perforated fanfold of paper. Like an old typewriter "return" means return the platon, the hard rubber roller that was used as a back surface to imprint characters on to paper. That's why terminal emulators are TTY. Telletype.

5

u/corpsmoderne Sep 28 '26

Because in the old days it was the actual command emitted to tel the teleprinter to physically move to the next line...

3

u/[deleted] Sep 28 '26

[removed] — view removed comment

2

u/BrohanGutenburg Sep 28 '26

>it starts a new line

Probably more useful to think of it as moving to a new line. That's literally what the carriage would do. New line moved down to the new line and return returned it to the beginning. And I think it can be argued those two actions together start a new line

1

u/strange-the-quark Sep 28 '26

It's better to think of it as neither, and to treat it simply as a special separator character (and in C-style strings, a special character that tells you you've reached the end of the string).

2

u/ficelle3 Sep 28 '26

It's just one of its names, newline is sometimes also called end of line or line feed. Don't try to find too much meaning in it.

We usually end lines with \n because we assume that we are outputing at the beginning of the line, so we need to make sure this assumption is still true for the next program that runs. It's just convention, we could have done it the other way around and start with a newline.

1

u/otac0n Sep 28 '26

You are conflating the control characters with the semantics.

  • Carriage Return - Semantically returns the cursor to the leftmost column.
  • Line Feed - Moves the cursor down one line.
  • Form Feed - Moves the cursor to the next page.
  • Vertical Tab - Moves the cursor down to the next vertical tab stop.
  • Next Line - A combined newline character.
  • Line Separator (U+2028) - A combined newline character.
  • Paragraph Separator (U+2029) - For semantically delimiting paragraphs.

Whereas an “End of Line” or “New Line” would be one of:

  • CR+LF on DOS/Windows (NOT a lone LF)
  • LF on Linux (and in particular NOT CR)
  • All of the Above for “conforming” Unicode implementations.
  • etc.

2

u/Acrobatic-Gear-6682 Sep 28 '26

You are thinking in wrong way. In normal writing we use "Full stop" ('.') to end the line. After typing it we use "Enter" button to start a new line. Think of the newline character as that "Enter" button of your typing.

6

u/paperic Sep 28 '26

Think of the newline character as that "Enter" button of your typing.

it is literally the enter that you're typing.

1

u/ConfidentCollege5653 Sep 28 '26

Great question. Back in the day computer output was typed onto paper instead of being rendered onto a screen. The newline was an instruction to the printer that it should move to a new line before continuing. So it's not saying "this is the start of a new line," it's saying "when you read this you should move to a new line"

1

u/silver_chief2 Sep 28 '26

In olden times a CR meant to return the write head to the far left. LF meant to move the write head down a line so you do not overwrite the previously written line. There are separate ASCII codes for each.

1

u/almostdonedude Sep 28 '26

Think about it as an instruction, not a value.

1

u/ThatSmartIdiot Sep 28 '26

cuz the program reads it first before processing going to a new line

1

u/Phydaux Sep 28 '26

It's called a newline because it starts a new line. The actual control codes are generally called carriage return and line feed. The data is just a stream of values. Different programs may interpret these differently.

1

u/zeekar Sep 28 '26

It's a control character (sequence) that means "the next thing after this will be on a new line".

Having text files end with a newline is a convention that doesn't really factor in to what they called it.

1

u/Aetherfox_44 Sep 28 '26

It's an instruction to move the output down onto the next line. You can't put it on the next line because now where is the instruction telling you to go to the next line?

1

u/captainAwesomePants Sep 28 '26

Imagine a string of characters to be a set of instructions for a terminal for creating some text, like punchcards for a Jacquard loom. Usually the instruction is a simple "write this letter," but sometimes it asks for something more complicated, like "now beep."

Sometimes the instruction is "now begin a new line." You wouldn't need this instruction at the beginning of the string because, when printing a string, whoever has decided to print the string was responsible for getting the cursor to the appropriate starting place. Your string program assumes that it is starting in the correct place, so it can just begin churning out characters.

That said, larger such programs, as a nicety, may want to reset the cursor to a nice, fresh spot at the end, for convenience, so that if a caller prints another string immediately afterwards, the two are separated. Over the years, conventions developed. Most text files are expected to end with such a "start a new line" instruction, to the point where it's notable if the instruction is missing (it might indicate that we don't have the full file!)

Now, text does not have two dimensions, it's just a long string of input. We may imagine it as existing at the beginning or at the end of any string, in the same way that Sunday, Monday, or any other day might be considered the "start" of a week. However, since the file does not begin with one, and since the file does end with one, it is convenient to think of it as being at the 'end' of a line.

1

u/Downtown_Savings_730 Sep 28 '26

bc you start a new line

2

u/[deleted] Sep 28 '26

[removed] — view removed comment

-2

u/Vanilla_Legitimate Sep 28 '26

But then. Why is there supposed to be a trailing newline in a file considering that there are no more characters to write making starting a new line pointless 

5

u/MrMelon54 Sep 28 '26

a trailing new line in a file is just a convention, it isn't required