views:

46

answers:

3

In visual basic I have the following 2 strings:

"\\.\" & "\"

How do I represent them in C?
Also, & in VB is the concatenation operator?

+4  A: 

Like this:

"\\\\.\\"
"\\"
pcent
+3  A: 

The \ is an escape character so if you want to print \ you need to put two of them: \\

To concatenate two string you can use strcat(string1, string 2) which is shown here.

Kyra
-1: C has no concatenation operator. The C++ string class overrides operator+ to provide similar functionality, but by no means is that a core language operator even in C++.
Billy ONeal
oops. Just changed it. thanks
Kyra
+1 for the edited answer. @Billy ONeal, you might consider removing your downvote after the correction.
Fred Larson
The second part of the question is about VB...
pcent
I got the second part from a C tutorial. From http://www.java2s.com/Code/C/String/Demonstrateshowtoputstringstogetherusingstrcat.htm, http://www.roseindia.net/c-tutorials/c-str-con.shtml and http://stackoverflow.com/questions/308695/c-string-concatenation
Kyra
@Fred Larson: Downvote removed.
Billy ONeal
@Kyra: Yes, but I'd avoid `strcat` and the other `<string.h>` facilities if at all possible (though no downvote for that :) ).
Billy ONeal
Two string literals, as in this question, can actually be concatenated simply by placing them next to each other.
caf
+1  A: 

As others have said, the backslash character () in C is an escape character. Look at http://msdn.microsoft.com/en-us/library/h21280bw%28VS.80%29.aspx to find more about it.

So your strings come out as follows:

"\\.\" is "\\\\.\\"
"\" is "\\"

There are many ways to concatenate strings.

puts("Hello" " " "World");

will print "Hello World".

A common way is to use strcat().

char szBuff[60];                  /* szBuff is an array of size 60 */
strcpy(szBuff, "Hello");          /* szBuff contains "Hello" */
strcat(szBuff, " World");         /* szBuff contains "Hello World" */
strcat(szBuff, " from Michael");  /* now contains  the whole sentence */
strcpy(szBuff, "New message");    /* strcpy overwrites the old contents */
Michael J
Ok, but one should avoid the facilities in `<string.h>` unless you have no other choice.
Billy ONeal
@Billy: Why? In C++ there are better ways, but in C <string.h> is generally the preferred way to manipulate strings. How would you recommend doing it?
Michael J
@Michael J: If available I'd recommend writing them yourself using ways that accept a buffer length input, or using something like the Safe C String library M$ uses (strcpy_s, strcat_s, and friends). Why the C standard uses a method that allows such easy buffer overflows when length-prefixed strings were available does not make sense to me.
Billy ONeal