PRB: C2001: Newline in Constant for a String on Multiple Lines

ID Number: Q40160

5.x 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

SYMPTOMS

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++

version 7.0, if the program below, which contains a string literal

that is incorrectly split over multiple lines, is compiled, the

following error will occur:

error C 2001: newline in constant

Sample Code

-----------

#include <stdio.h>

main() {

printf("\n %s

%s

%s",

"this", "is", "it");

}

CAUSE

This is a common programming mistake. Special considerations must

be taken to split a formatted string over several lines.

RESOLUTION

The best method is to change the format string, as in the following

example (this works because strings separated only by spaces, tabs,

and/or newlines are concatenated):

#include <stdio.h>

main() {

printf("\n %s"

"%s"

"%s",

"this", "is", "it");

}

The older and less-preferred method is to use continuation lines by

typing a backslash followed by a carriage return at the end of a

line, as in the following example:

printf("\n %s\

%s\

%s",

"this", "is", "it");

This is not as good as the previous example because the spaces at

the beginning of the continuation line become part of the string,

unlike the first example.

Additional reference words: 5.00 6.00 6.00a 6.00ax 7.00