What's with "\" ???

Started by
16 comments, last by Gaiiden 23 years, 5 months ago
Why does C++ not like it when I do this: LPSTR path = "C:\"; ??? I get errors like "newline in constant" and "missing ;" on the next line down. When I take out the "\" in the string, I get no errors! Why is this? I've never had this problem before. I also noticed when putting it inot a define like this: #define PATH "C:\PFiles\C++\" // these comments aren't green!! the comments afterwards aren't comments! anyone know what's going on here? ============================== "Need more eeenput..." - #5, "Short Circuit" ============================== Edited by - Gaiiden on 10/29/00 2:34:30 PM

Drew Sikora
Executive Producer
GameDev.net

Advertisement
In C/C++, using a \ in a string indicates an escape sequence. For example:

\n - newline
\t - tab
\" - double-quotes

To get a backslash character, just use it twice in a row, like this: \\. So a valid filename would look like this:

#define PATH "c:\\pfiles\\C++\\"

-Ironblayde
 Aeon Software

The following sentence is true.
The preceding sentence is false.

Edited by - Ironblayde on October 29, 2000 3:42:29 PM
"Your superior intellect is no match for our puny weapons!"
\ starts a sequence ( in c/c++ ) ( like \n \r \t \" ).

If you want to use only a \ you have to write \\ ( so it looks like this

LPSTR path = "C:\\";

)

That''s all.
C was designed on a unix system, so you can use / instead of \\. \\ will not work if you are on a unix system only on windows. new compilers should be clever enough to make the switch, be I would not bet on it.

char* pcString[Size] = "c:/directory/file";
quote:
C was designed on a unix system, so you can use / instead of \\.


That has nothing to do with C, it has to do with the operating system.
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
even on windows, the forward slash "/" is the directory separator. the backslash is basically only used in the shell.
quote:
the backslash is basically only used in the shell.


It's used in many more places than that, most Windows programs use \ (Explorer for example).

Edited by - Muzzafarath on October 30, 2000 8:14:44 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Backslash is used under DOS, in case you have not been using computers for long enough to have used it, and the forward slash is not a valid delineation character for DOS. And the / being used in C is to do with the language. If you try using that under DOS it will _NOT_ work. The reason the www uses / as its delineation character is because it is based on UNIX, which as mentioned before used / as its delineation character.


Please state the nature of the debugging emergency.


sharewaregames.20m.com

Guys, I''m no unix expert, but how often do you see the string "c:\" in unix? =)
So to sum up, in C/C++ if you want a forward slash in a string, use /.
If you want a backslash in a string, use \\.




This topic is closed to new replies.

Advertisement