Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if character equals \ in C++

Tags:

c++

I'm trying to see if a character c equals \

if (c == '\')
 //do something

I don't know exactly how this is called but everything after \ turns in a character string.

like image 422
Shury Avatar asked May 19 '16 07:05

Shury


People also ask

How do you check if a character is the same in C?

Use strcmp() to compare strings. The return value is 0 if strings are the same. The return value from strcmp() also indicates order: the value is < 0 or > 0 to indicate which string is lesser or greater.

Can you use == for char?

Yes, char is just like any other primitive type, you can just compare them by == .


2 Answers

Backslash is used as the escape character in C++, as it is in many other languages. If you want a literal backslash, you need to use \\:

if (c == '\\') {

}
like image 176
TartanLlama Avatar answered Sep 21 '22 20:09

TartanLlama


\ backslash is an escape character.

Escape sequences are used to represent certain special characters within string literals and character literals. Read here

So you should do:

if (c == '\\'){
}
like image 34
granmirupa Avatar answered Sep 21 '22 20:09

granmirupa