String Literals

A string literal is a sequence of zero or more characters, including escape sequences (See: Constants), between a pair of quote marks. This will create an array of char somewhere in memory, initialized with this sequence of characters plus a terminating NUL (all bits 0) character. A string literal is thus of type pointer to char.

It is not allowed to explicitly put a newline character within a string literal. This would cause the string to continue on to the next line. All compilers will flag this as an error. If you explicitly want a newline within the quote marks of the string, use the escape sequence: \n. For example:

	"I am a string literal!\nWhich prints on two lines"

Please note that the NUL character has all bits zero and a value of zero and is different from '0' which, in ASCII has a value of 48.

If a very long string literal is wanted, and yet it would be nice if all the characters would be seen on a screen or printed page, then a long string literal can be formed by writing the string literal in pieces, with each piece in quote marks, and each such string literal separated from the other by at most white space (or a comment). Here is an example of a single string literal on three lines (with no newline character in it):

	"I am the first part of a long string literal,"
	"I am the second part of this string literal,"
	"and that makes me the last part of this string literal"

When the constituting parts are concatenated into one, all but the last NUL character are discarded. Because the concatenated parts may start in any column, they may be arranged to reflect program structure, this method of having long string literals is preferable to the use of the continuation operator: backslash followed by a newline - where the continued string must continue in the first column of the next line.


© 1991-2008 Prem Sobel. All Rights Reserved.