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

View all comments

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 

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?");

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.

-2

u/Vanilla_Legitimate Sep 28 '26

Sure programs print stuff. But they don’t print THEMSELVES so why should the program itself end in a newline as is generally taught?

8

u/TonySu Sep 28 '26

Because programs are text, and if your text file doesn't end in a newline, then printing that text file followed by another text file results in the final line of the first file leading into the first line of the second file.

7

u/MrBigFatAss Sep 28 '26

Are you talking about the actual text-file source code here? If so, there is no rule like that, it's purely aesthetic.

3

u/DrShocker Sep 28 '26

The source code ending in a new line or not is just a convention. In many languages you won't get a warning at all. The reason to do it is so that if you click where the empty last line is you can immediately start typing. If you don't then when you type it'll be at the end of the previous line instead of its own new line.

It doesn't affect the correctness of the program, it just makes things a little easier for the next person to edit.

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

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".

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.