Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

evaluate expression with backslash in middle of statement

Tags:

c

Suppose in C, I have the following code:

i=5 \    
+6;  

If I print i, it gives me 11.

I do not understand how the above code executes correctly. At first glance, I guessed it to be compiler error because of unrecognized token \. Can somebody explain the logic? Is it related to maximal munch logic?

like image 992
Rakesh_Kumar Avatar asked Dec 15 '22 05:12

Rakesh_Kumar


2 Answers

A backslash at the end of a line tells the compiler to ignore the new-line character.

It is a way of formatting lines to be readable for humans without interrupting the source text. E.g., if you have a long string enclosed in quotation marks, you can use a backslash to continue the string on a new line without inserting a new-line character in the string.

(This was more useful before the C standard added the property that adjacent strings, such as "abc" "def", are concatenated. Now you can put strings on consecutive lines, and they will be concatenated. Prior to that, you had to use the backslash to do it.)

Nowadays the most common use of the backslash is, as heretolearn points out, to continue preprocessor macro definitions. Unlike regular C statements, preprocessor statements must be on a single line. However, some preprocessor macro definitions are quite long. To format them (somewhat) nicely, a definition is spread over multiple physical lines, but the backslash makes them into one line for the compiler (including the preprocessor).

A backslash followed by a new-line character are completely removed from the source text by the compiler, unlike a new-line character by itself. So the source text:

abc\
def

is equivalent to the single identifier abcdef, not abc def. You can use it in the middle of any operator or other language construction except trigraph sequences (trigraph sequences, such as ??=, are converted to replacement characters, such as #, before the backslash-new-line processing):

MyStructureVariable-\
>MemberName

IncrementMe+\
+

However, do not do that. Use it reasonably.

like image 118
Eric Postpischil Avatar answered Mar 22 '23 23:03

Eric Postpischil


The practice of escaping the newlines at the end of a line is indeed to mark the continuation of the statement onto the next line. Apparently that was needed in the old C compilers There is only one place that I'm sure it is still needed and that is in macro definitions of functions, something that is generally frowned upon in C++.

A continued line is a line which ends with a backslash, . The backslash is removed and the following line is joined with the current one. No space is inserted, so you may split a line anywhere, even in the middle of a word. (It is generally more readable to split lines only at white space.)

The trailing backslash on a continued line is commonly referred to as a backslash-newline.

If there is white space between a backslash and the end of a line, that is still a continued line. However, as this is usually the result of an editing mistake, and many compilers will not accept it as a continued line, GCC will warn you about it.

Reference

like image 45
heretolearn Avatar answered Mar 22 '23 23:03

heretolearn