RAD Studio |  |

String Constants
See Also
Collapse All

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:

Copy Code

    "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:

Copy Code

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

prints like this:

Copy Code

    "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,

Copy Code

    #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

Copy Code

    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:

Copy Code

    puts("This is really \
    a one-line string");

See Also

Constants

Integer Constants

Integer Constant Without L Or U

Floating Point Constants

Character Constants

The Three Char Types

Escape Sequences

Wide-character And Multi-character Constants

Enumeration Constants

Constants And Internal Representation

Internal Representation Of Numerical Types

Constant Expressions

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!