5

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, i.e., backslash + zero?

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

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
pepero
  • 7,095
  • 7
  • 41
  • 72

3 Answers3

8

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Another source for escapes: http://msdn.microsoft.com/en-us/library/h21280bw(VS.80).aspx It is interesting to note that it doesn't explicitly include \0 – BCS Nov 24 '10 at 01:32
  • @BCS: I'm sure that's just another not-so-interesting anomaly in the MSDN documentation. It is explicitly mentioned [here](http://msdn.microsoft.com/en-us/library/6aw8xdf2.aspx). – Cody Gray - on strike Nov 24 '10 at 01:57
  • It's interesting because it's *impliedly* mentioned as part of octal literals. – BCS Nov 24 '10 at 14:59
8

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, e.g., 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.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • hi, Marcelo, thank you for your detailed answer. Just one thing, when we make a c program like string processing, we, as human beings, use "\0" in the program to express our operation with the end of the string. then, what the compiler does? Is "\" stored or the following character "0" (or "a" in your example) stored? then, when the program executes, how does the code responds or proceeds with this stored value? – pepero Nov 23 '10 at 10:57
  • @pepero: For the case of ending a string with \0, the value that is stored is a byte with the value of zero (no bits set). This happens to be yet another C conversion used to allow finding the end of strings. – BCS Nov 24 '10 at 01:27
  • @pepero: I'm guessing that you made your comment before I saved my edited answer, which covers this point explicitly in the second paragraph. I've just tweaked it slightly to eliminate any possible ambiguity. – Marcelo Cantos Nov 24 '10 at 09:59
0

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.

Lee Netherton
  • 21,347
  • 12
  • 68
  • 102