Home > Software engineering >  Why does C not recognize strings across multiple lines?
Why does C not recognize strings across multiple lines?

Time:04-12

(I am very new to C.)

Visual newlines seem to be unimportant in C. For instance:

int i; int j;

is same as

int i;
int j;

and

int k = 0 ;

is same as

int
k
=
0
;

so why is

"hello
hello"

not the same as

"hello hello"

CodePudding user response:

It is because a line that contains a starting quote character and not an ending quote character was more likely a typing mistake or other error than an attempt to write a string across multiple lines, so the decision was made that string literals would not span source lines, unless deliberately indicated with \ at the end of a line.

Further, when such an error occurs, the compile would be faced with reading possibly thousands of lines of code before determining there was no closing quote character (end of file was reached) or finding what was intended as an opening quote character for some other string literal and then attempting to parse the contents of that string literal as C code. In addition to burdening early compilers with limited compute resources, this could result in confusing error messages in a part of the source code far removed from the missing quote character.

This choice is effected in C 2018 6.4.5 1, which says that a string literal is " s-char-sequenceopt ", where s-char-sequence is any member of the character set except the quote character, a backslash, or a new-line character (and a string literal may also have an encoding prefix, a u8, u, U, or L before the first ").

CodePudding user response:

1- using double quotes for each string :

char *str = "hello "
 "hello" ;

** One problem with this approach is that we need to escape specially characters such as quotation mark " itself.

2- Using - \ :

 char *str = "hello \
       hello" ;

** This form is a lot easier to write, we don't need to write quotation marks for each line.

CodePudding user response:

Strings can be continued over newlines by putting a backslash immediately before the newline:

"hello \
hello"

Or (better), using string concatenation:

"hello "
"hello"

Note that the space has been carefully preserved so that these are equivalent to "hello hello" except for the line numbering in the file after the appearance.

The backslash-newline line elimination is done very early in the translation process — in phase 2 of the conceptual translation phases.

Note that there is no stripping leading blanks or anything. If you write:

printf("Some long string with maybe an integer %d in it\
        and some more data on the next line\n", i);

Then the string has a sequence of (at least) 8 blanks in it between in it and and some. The count of 8 assumes that the printf() statement is aligned in the left margin; if it is indented, you'd need to add the extra white space corresponding to the indentation.

  • Related