Wednesday, January 4, 2012

How do we concatenate (which means to connect) a C string literal?

Here, we illustrate three methods. In method 1, we use a backslash at the end of a line to indicate that a string literal has not finished and continues on the next line. Since the C compiler disregards all blank characters behind a statement, the connection to the next line will start right at the end of the preceding statement. If you want to include blank characters in a statement that occupies two lines, either place them before the backslash in the first lines or at the beginning of the second line. For example, the statement

printf("Welcome to New \
York!");

is equivalent to

printf("Welcome to New York!");

but not

printf("Welcome to New York!");


In method 2, we enclose each unfinished string literal in double quotes; for example, the statement

printf("From " "Russia "
"with" " love.\n");

is equivalent to the statement

printf ("From Russia with love\n");


Method 3 is a combination of methods 1 and 2. For example, the preceding statement is equivalent to

printf("From " "Russia \
with " "love.\n");

No comments:

Post a Comment