-1

I always wondered, why the new-line-character is just a single character, even it is made of two: The escape character '\' and the character 'n'.

Notepad also counts it as 2 characters:

enter image description here

So why can \n be used like a char, while it's definitely bigger than just one character?

DudeWhoWantsToLearn
  • 751
  • 2
  • 10
  • 29
  • 4
    _even it is made of two_ No it isn't. It is one. Your notepad example counts \r\n, that is return + newline. These are two. – TaW Apr 28 '20 at 12:52
  • "[An escape sequence is regarded as a single character and is therefore valid as a character constant](https://learn.microsoft.com/en-us/cpp/c-language/escape-sequences?view=vs-2019)" – Broots Waymb Apr 28 '20 at 12:58
  • 1
    When you open an empty file in Notepad++, then use the numeric keypad to enter just a newline by holding down the ALT key and typing 010 (which is ASCII for linefeed), you'll see 'length: 1 lines: 2' in your info bar, with two lines being shown. The same occurs if you enter 013 (carriage return) instead. – Michael Tracy Apr 28 '20 at 12:59
  • If you save that notepad++ file and open it in a hex editor (or use a hex editor extension, if there is one), you'll see both \r and \n, not just \n. – Broots Waymb Apr 28 '20 at 12:59
  • 1
    \n is just a visible representation used to indicate the single ascii code 13 In Unix systems this is enough to start a newline, while Windows uses the combination \r\n (two characters). If you need to handle these differences then use Environment.NewLine (and this is a string) – Steve Apr 28 '20 at 13:01

2 Answers2

1

I think the reason for it is ASCII table should have a newline code, so '\n' - is a litteral for it. You should check this one and this one. For notepad - it is just a symbols.

Nicefsf
  • 140
  • 1
  • 1
  • 9
0

The backslash escapes the 'n' signifying to the interpreter it’s a linefeed and not just a regular n. As TaW has said you have a return and a carriage return in the example.

This post should be of help

atoms
  • 2,993
  • 2
  • 22
  • 43