How does "\ n", "\ t" add a new line and tab, respectively?

In programming languages, if I use "\ n", it adds a newline.

Can someone explain how "\ n" is translated to a new line and the same for "\ t"?

+5
source share
6 answers

When the compiler reads your program and sees backslash something , it knows that it is "pretending" that it saw something else. You can imagine that the compiler part works as follows:

 current_character = getNextCharacter(); if (current_character == BACKSLASH) { current_character = getNextCharacter(); if (current_character == 'n') { /*oh, they want a newline */ } else if (current_character == 't') { /* it a tab they want */ } else /* ... and so on and so forth */ } 
+5
source

Assuming we are talking about lines within the language, such as "Hello,\tWorld\n" , then it is the task of compilers to detect a backslash, and then insert the appropriate sequence of characters for the new line and tab on this computer into the line in the generated executable file .

The code for working with this in the compiler is quite complicated, because when parsing the code you need to consider a huge number of OTHER things, but if we simplify it a bit:

 if (inQuote) // So inside ' or " { if (currentChar == '\') { switch(nextChar) { case 'n': InsertNewLine(); break; case 't': InsertTab(); break; case 'b': InsertBackSpace(); break; ... several more of these ... case '\': // '\\' outputs one '\' InsertBackSlash(); break; default: Insert(nextChar); // Ignore any others. break; } } } 

I cheated and made functions for InsertXX , where in reality it is probably just *output++ = 10; or *output++ = 9; [or some specific constant that translates to the same].

+2
source

The \ character in many languages ​​is an "escape character". With this character, you can add special non-printable characters, such as a new line or tab, as well as quotation marks. If you need to use "to delimit a line, you can use" to print a quote label inside it.

for the Windows command line, ^ works similarly for printing special characters.

+1
source

The compiler knows that when he sees \ n or \ t in a line, he must replace it with something special: a new line or a tab character. It’s just the convention that a backslash followed by a special character (n, l, t, ...) means something special.

The reason for this convention is that in text editors, when writing your program, it is not possible to insert the true newlines and tabs in strings to help the programmer develop this convention.

0
source

A newline character is an "escape sequence", the so-called because the backslash character is considered an escape character - it causes the normal interpretation of the string to be exited, so that the next character is recognized as having special meaning, For example,

  • \ n introduces a new line.
  • \ t introduces a tab.
  • \\ inserts a backslash
  • \ "inserts a double quote.
0
source

The compiler stores strings ( "hello" ) as a set of characters ( 'h', 'e', 'l', 'l', 'o' ). And, in addition, it saves these characters as numbers, which are then represented as bits, etc.

\n represents one character, although I used two characters to write it. The transfer from a human-readable character to a stored number goes through some standardized system, such as ASCII or Unicode. In ASCII, a new line is represented by the decimal number 10 (see http://www.asciitable.com/index/asciifull.gif ). "A" is 65, "B" is 66, and so on. Each lowercase letter has its own number, different from uppercase, starting with 97.

A new line is converted only to a new new line when printing a character. Whenever a special character like \n appears in System.out.print() , the program asks the terminal to return the desired result, for example, skipping the next line.

The terminal delegates this further to the operating system and, finally, to the graphics card in order to actually make the pixels move. Each part has its own area of ​​responsibility, and each other part does not understand how things are going on inside the country. It’s just that they can interact with each other and do things.

Remember that most computer systems are united by a large group of people. Very few know every aspect of what goes on behind the scenes. And the beauty of it all is that they don’t need it!

0
source

Source: https://habr.com/ru/post/1242471/


All Articles