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

Article translations Article translations
Article ID: 40160 - View products that this article applies to.
This article was previously published under Q40160
Expand all | Collapse all

On This Page

SYMPTOMS

If program contains a string literal that is incorrectly split over multiple lines the following error will occur:
error C 2001: newline in constant
Several other errors may be caused by this also. This sample will also give errors C2065, C2121, C2143, and C2198 as a result of the newline errors.

CAUSE

This is a common programming mistake. Special considerations must be taken to split a constant 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 as specified by the ANSI standard):
   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.

MORE INFORMATION

Sample Code

#include <stdio.h>
void main()
{
   printf("\n %s"
          " %s"
          " %s",
          "this", "is", "it");
}
				

Properties

Article ID: 40160 - Last Review: July 5, 2005 - Revision: 1.1
APPLIES TO
  • Microsoft C Professional Development System 6.0
  • Microsoft C Professional Development System 6.0a
  • Microsoft C Professional Development System 6.0a
  • Microsoft C Professional Development System 6.0
  • Microsoft C Professional Development System 6.0a
  • Microsoft C/C++ Professional Development System 7.0
  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 1.51
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
Keywords: 
kbcompiler kbprb KB40160
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com