RAD Studio
ContentsIndex
PreviousUpNext
String Constants

String constants, also known as string literals, form a special category of constants used to handle fixed sequences of characters. A string literal is of data type array-of- const char and storage class static, written as a sequence of any number of characters surrounded by double quotes:

"This is literally a string!"

The null (empty) string is written "". 

The characters inside the double quotes can include escape sequences. This code, for example:

"\t\t\"Name\"\\\tAddress\n\n"

prints like this:

"Name"\       Address

"Name" is preceded by two tabs; Address is preceded by one tab. The line is followed by two new lines. The \" provides interior double quotes. 

If you compile with the -A option for ANSI compatibility, the escape character sequence "\\", is translated to "\" by the compiler. 

A literal string is stored internally as the given sequence of characters plus a final null character ('\0'). A null string is stored as a single '\0' character. 

Adjacent string literals separated only by whitespace are concatenated during the parsing phase. In the following example,

#include <stdio.h>
int main() {
   char    *p;
   p = "This is an example of how the compiler "   " will\nconcatenate very long strings for you"   " automatically, \nresulting in nicer" " looking programs.";
   printf(p);
   return(0);
}

The output of the program is

This is an example of how the compiler will
concatenate very long strings for you automatically,
resulting in nicer looking programs.

You can also use the backslash (\) as a continuation character to extend a string constant across line boundaries:

puts("This is really \
a one-line string");
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!