5.3.7.3 Character And String Constants

Character constants are enclosed by single quote characters, ', for example 'a'. A character constant has int type, although this can be later optimized to a char type by the compiler.

To comply with the C standard, the compiler does not support the extended character set in characters or character arrays. Instead, they need to be escaped using the backslash character, as in the following example.

const char name[] = "Bj\370rk";
printf("%s's Resum\351", name);     \\ prints "Bjørk's Resumé"

Multi-byte character constants are not supported by this implementation.

String constants, or string literals, are enclosed by double quote characters ", for example "hello world". The type of string constants is const char * and the characters that make up the string are stored in program memory, as are all objects qualified const.

A common warning relates to assigning a string literal, which cannot be modified, to a pointer that does not specify a const target, for example:

char * cp = "hello world\n";

See 5.3.6.1 Combining Type Qualifiers And Pointers and qualify the pointer as follows.

const char * cp = "hello world\n";

Defining and initializing an array (i.e., not a pointer) with a string is an exception. For example:

char ca[]= "hello world\n";

will actually copy the string characters into the RAM array, rather than assign the address of the characters to a pointer, as in the previous examples. The string literal remains read-only, but the array is both readable and writable.

The MPLAB XC8 compiler will use the same storage location and label for strings that have identical character sequences, except where the strings are used to initialize an array residing in the data space. For example, in the code snippet

if(strncmp(scp, "hello", 6) == 0)
    fred = 0;
if(strcmp(scp, "world") == 0)
    fred--;
if(strcmp(scp, "hello world") == 0)
    fred++;

the characters in the string "world" and the last 6 characters of the string "hello world" (the last character is the null terminator character) would be represented by the same characters in memory. The string "hello" would not overlap with the same characters in the string "hello world" as they differ in terms of the placement of the null character.