Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a stray \ [backslash] have any meaning in C code? [duplicate]

Tags:

c

For the code:

#include <stdio.h>
int main(void) {
\
printf("Test");
}

compiling via gcc -Wall -Wextra -Wfatal-errors test.c gives no warnings.

However for other stray characters, compiling leads to the following error:

test.c:3:1: error: stray ‘`’ in program
 `
 ^
compilation terminated due to -Wfatal-errors.

I tried the same, with different stray characters, and each time I got an error.These errors ranged from expected primary-expression before the token to value computed is not used. I was expecting a similar warning/error for the backslash character as well.

So, does a stray backslash followed by no spaces have no meaning in C code?

I know for a fact that redundant semicolons (;) have no effect in C code, so are these somehow related?

like image 503
kalpaj agrawalla Avatar asked Aug 16 '19 14:08

kalpaj agrawalla


People also ask

What does a backslash mean 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 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 is stray C?

Go to the line which is reported and you see the straying special char. That happens if you had opend or edited you sourcecode with an editor that enters “non breakable spaces” instead of the common whitespace. That might happen if you push “shift-space” instead of only “space”, depending on the editor you use.

Why do we use slash in C++?

C++ comments come in two flavors: single-line comments and multiline comments. Single-line comments are accomplished using a double slash (//). The double slash tells the compiler to ignore everything that follows, until the end of the line.

How to solve stray error in C/C++?

error: stray '\' in program. If you are getting error messages like above in your C/C++ program, then don't worry. ;-) Perhaps you have copied the code from somewhere. If your code is small ,then the simplest solution is that you should start typing it from zero.

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 ( ).

Why is my C++ code not showing octal characters?

The reason for this is that your code might contain some hidden octal (not-readable) characters which the compiler cant understand and you can not see. They are present in your code because you have perhaps copied it from somewhere. So, simply you can retype the same code to solve the problem.

Is the stray ending of the game abrupt?

But, after you’ve played through the adventure, you might want the somewhat abrupt Stray ending explained. Stray presents many questions as you play through the game, but because you’re a cat and can’t talk in human (only meows), it’s hard for any exposition to be delivered, especially towards the end of the game.


Video Answer


2 Answers

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 \.

like image 136
dbush Avatar answered Oct 21 '22 23:10

dbush


It's the line continuation character. (You see it most often in macros.) The trigraph ??/ has the same effect.

If there was whitespace after it, then a compiler would issue a diagnostic.

like image 35
Bathsheba Avatar answered Oct 21 '22 23:10

Bathsheba