Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a backslash in C++ mean?

Tags:

c++

syntax

What does this code: (especially, what does a backslash '\' ? )

s23_foo += \ 
s8_foo * s16_bar;

I added the datatypes, because they might be relevant. Thanks for your help.

like image 753
Christian Avatar asked Oct 16 '13 13:10

Christian


People also ask

What is backslash used for in C?

In Windows systems, for example, the backslash is used to separate elements of a file path, for example: C:\Documents\User\File. In C, Perl and Unix scripting, the backslash indicates that the following character must be treated in some special way. Within the TeX typesetting markup system, the backslash starts tags.

What is '\ A in C?

It is an ascii character and can be used anywhere. \a Is used to display a sound.

What is the \0 character in C?

\0 is zero character. In C it is mostly used to indicate the termination of a character string. Of course it is a regular character and may be used as such but this is rarely the case. The simpler versions of the built-in string manipulation functions in C require that your string is null-terminated(or ends with \0 ).

What does a backslash do?

It is a “backwards slash” that is the reverse of a forward slash ( / ). The backslash has no usage in formal or informal writing. It is not considered to be a punctuation mark and shouldn't be confused with the forward slash (often just called a slash), which is used as a punctuation mark in writing.

What are backslash character constants in C language?

What are Backslash character constants in C language? A backslash ( \ ) that allows a visual representation of some nongraphic characters introduces an escape. One of the common escape constants is the newline character ( ).

What is a backslash ( \ ) in JavaScript?

A backslash ( \ ) that allows a visual representation of some nongraphic characters introduces an escape. One of the common escape constants is the newline character ( ). The backslash characters are as follows −

How are backslash characters spliced into logical source lines?

Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice.

What is the meaning of the slash character in C?

In C ,Backslash (\) is a escape sequence character followed by predefined alphabets ( E.g - \t, \b, \r, \n…etc.). It indicate that the character to their right should be interpreted differently. Its a non-printable characters, if you want to print (\) , you’ve to use (\\) . And Slash (/) is a normal character.


2 Answers

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.

like image 173
John Dibling Avatar answered Oct 19 '22 14:10

John Dibling


It lets you continue a statement onto the next line - typically you only need it inside a #define macro block

like image 26
benjymous Avatar answered Oct 19 '22 14:10

benjymous