简体   繁体   中英

What does back slash “\” really mean?

I'm wondering about Java's backslash. How does the computer or the compiler see this backslash and how is it stored in computer?

I read that backslash removes the special meaning of the following character. But how does a computer treat this one and in what conditions treat it in some other ways?

For example the null character \\0 in C programming, is the end of the string, but is it a single character or two characters, ie, backslash + zero?

The objective of back slash is to indicate for humans or to indicate for 0-1 computer?

The backslash \\ is a character, just like the letter A , the comma , , and the number 4 . In some programming languages, notably C and its descendants (and maybe ancestors), it is used inside a string or character literal to escape other characters. For instance, '\\a' represents the bell character, and will produce a beep from the computer if you print it ( printf("%c", '\\a') ).

As a C-language escape character, it is largely a human construct allowed by the compiler so humans can express, eg, the bell character. The compiled code simply stores the character — a byte with the value 7. Just to be absolutely clear, it does not store a \\ followed by an a .

Under other contexts, the backslash means something to the program at runtime. The most well-known instance of this is regular expression syntax, in which a backslash escape other characters in order to either give them special meaning or take away a special meaning they might have. For example, grep '\\<foo\\>' file.txt will locate lines with the word foo in file.txt. In this case the backslashes really are there at runtime, and are interpreted by the program as escapes for the < and > that follow them. In this case, \\< and \\> don't represent characters at all; they denote a zero-width match against the beginning and end of a word, respectively.

It really depends entirely on the context. Backslashes can mean many different things, depending on where you see them used. For example, in Windows, backslashes are commonly found as path separators.

In C-based programming languages (as well as other scripting languages), they are escape characters . They indicate that the character to their right should be interpreted differently from the way it normally would. In your example, \\0 is a null-terminator, and the backslash indicates that the 0 should be interpreted by the compiler (and the human!) as the null character instead of as the number zero. It would be seen as just one character because the backslash is dropped off once its function is served—it has no meaning in that sequence beyond its use as an escape character.

A backslash only means something special if the situation it is used in deems it to be. For example, many programming languages use them as escape characters in strings. This usually changes the meaning of the next character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM