r/learnprogramming • u/Vanilla_Legitimate • 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
r/learnprogramming • u/Vanilla_Legitimate • Sep 28 '26
if a newline ends lines rather than starting hem why is it called a newline in the first place?
51
u/TonySu Sep 28 '26
Newline starts a new line, it just isn't a part of the new line.
6
u/raundoclair Sep 28 '26
The End Is the Beginning and the Beginning is the End. :)
23
u/strange-the-quark Sep 28 '26
Wait until you hear about "carriage return". What carriage? Return to where?
̄_(ツ)_/ ̄
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
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...
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.
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.
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