What does back slash \ really mean?
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.
What does a backslash in C++ mean?
Backslashes denote two different things in C++, depending on the context.
As A Line Continuation
Outside of a quotes string (see below), a \
is used as a line continuation character. The newline that follows at the end of the line (not visible) is effectively ignored by the preprocessor and the following line is appended to the current line.
So:
s23_foo += \
s8_foo * s16_bar;
Is parsed as:
s23_foo += s8_foo * s16_bar;
Line continuations can be strung together. This:
s23_foo += \
s8_foo * \
s16_bar;
Becomes this:
s23_foo += s8_foo * s16_bar;
In C++ whitespace is irrelevant in most contexts, so in this particular example the line continuation is not needed. This should compile just fine:
s23_foo +=
s8_foo * s16_bar;
And in fact can be useful to help paginate the code when you have a long sequence of terms.
Since the preprocessor processed a #define
until a newline is reached, line continuations are most useful in macro definitions. For example:
#define FOO() \
s23_foo += \
s8_foo * s16_bar;
Without the line continuation character, FOO
would be empty here.
As An Escape Sequence
Within a quotes string, a backslash is used as a delimiter to begin a 2-character escape sequence. For example:
"hello\n"
In this string literal, the \
begins an escape sequence, with the escape code being n
. \n
results in a newline character being embedded in the string. This of course means if you want a string to include the \
character, you have to escape that as well:
"hello\\there"
results in the string as viewed on the screen:
hello\there
The various escape sequences are documented here.
Does a stray \ [backslash] have any meaning in C code?
The backslash is escaping the newline that follows it. It is used to logically combine two or more lines in cases where whitespace is relevant, such as in the middle of a string constant or as part of a macro definition so that it may span multiple lines.
In this particular case the whitespace is not significant so there is no effect on the code.
If you were to add spaces after the \
you would get a warning, and if you put a ;
after those spaces you would get an error regarding a stray \
.
backslash usage in C language
A backslash can be used to escape a newline in source code so that two lines are treated as a single line.
In your particular case it has no effect on the code, however it's more commonly used in macros which end at the time a newline is reached, i.e.:
#define MY_MACRO(x) \
do {\
int y;\
y = foo();\
x = y;\
} while (0)
Here the backslashes are required, otherwise only the first line would be part of the macro.
What does \a mean in C?
The '\a'
in an escaped representation of the BEL
charcater which has ascii code 7.
The \
is used to "escape" a character that otherwise has no representation and cannot be written in a string by other means. Another examples are the newline '\n'
and carriage return '\r'
.
Explain Backslash in C
This '\010'
is a octal escape sequence 10
in octal is 8
in decimal and it will be promoted to an int
when calling printf
so that explains that value.
This '010'
is a multi-character constant and it's value is implementation defined, if we look at the C99 draft standard section 6.4.4.4
Character constants paragraph 10 says(emphasis mine):
[...]The value of an integer character constant containing more than one character (e.g.,
'ab'), or containing a character or escape sequence that does not map to a single-byte
execution character, is implementation-defined.[...]
and if you were using gcc
you would have seen at least this warning:
warning: multi-character character constant [-Wmultichar]
and probably this warning as well on overflow:
warning: overflow in implicit constant conversion [-Woverflow]
the value that y
obtains is a little more interesting since character constant has an integer value it can not just be taking the first character, the multi-character constant has to take an integer value and then be converted to char. clang
helpfully provides a more detailed warning:
warning: implicit conversion from 'int' to 'char' changes value from 3158320 to 48 [-Wconstant-conversion]
and current versions of gcc
produces the same value, as we can see from this simple piece of code:
printf("%d\n",'010');
so where does 3158320
comes from? For gcc
at least, if we look at the documentation for Implementation-defined behavior it says:
The compiler evaluates a multi-character character constant a character at a time, shifting the previous value left by the number of bits per target character, and then or-ing in the bit-pattern of the new character truncated to the width of a target character. The final bit-pattern is given type int, and is therefore signed, regardless of whether single characters are signed or not (a slight change from versions 3.1 and earlier of GCC). If there are more characters in the constant than would fit in the target int the compiler issues a warning, and the excess leading characters are ignored.
if we perform the operation(assuming 8-bit char) document above we see:
48*2^16 + 49*2^8 + 48 = 3158320
^ ^
| decimal value of ASCII '1'
decimal value of ASCII '0'
gcc
will convert the int
to char
using modulus 2^8
regardless of whether char
is signed or unsigned which effectively leaves us with the last 8
bits or 48
.
Related Topics
C++ Reading the Data Part of a Wav File
Is #Pragma Once Part of the C++11 Standard
Insert VS Emplace VS Operator[] in C++ Map
Difference Between Rdtscp, Rdtsc:Memory and Cpuid/Rdtsc
Can the Use of C++11's 'Auto' Improve Performance
C++ Singleton VS. Global Static Object
What Would Be C++ Limitations Compared C Language
How to Create a Function Dynamically, During Runtime in C++
Pimpl Idiom VS Pure Virtual Class Interface
How to Get the Gl Library/Headers
In C++, Is It Still Bad Practice to Return a Vector from a Function
Fast Fixed Point Pow, Log, Exp and Sqrt
Why Do We Actually Need Private or Protected Inheritance in C++
How to Erase Elements from Stl Containers
C/C++ Function Definitions Without Assembly
How to Use the Non-Default Constructor for a Member
Is a Union Member's Destructor Called
Efficient Bitwise Operations for Counting Bits or Find the Right|Left Most Ones