Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i was trying to print the \ character in c++, but the compiler thinks its a escape character and doesnt print it. Pls help

What I have tried:

i have tried searching it up but i cant find a answer regarding my question.
Posted
Updated 8-Aug-21 20:20pm

Quote:
i was trying to print the \ character in c++, but the compiler thinks its a escape character and doesnt print it.

Because '\' is an escape character, and the escape sequence to print '\' is '\\'.
C++
printf("\\"); // print your \

Escape sequences - cppreference.com[^]
 
Share this answer
 
v2
In C++ normal sting literals need Escape characters - cppreference.com[^] in order to insert some special characters into the string.

You can disable them if your compiler supports it using Raw Strings:
C++
cout << R"(Hello "Mike" - C:\myfolder\)";
Will print as
Hello "Mike" - C:\myfolder\
But be aware that this disables all special characters, so you can no longer enter newlines or tabs eitehr.
 
Share this answer
 
An additional way is to use escape sequences for printing the backslash.
C++
print("my backslash: %c", 0x5c);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900