Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 ユーザーズマニュアル
製品コード
SW006021-1
MPLAB
®
XC8 C Compiler User’s Guide
DS52053B-page 160
2012 Microchip Technology Inc.
5.4.6.3
CHARACTER AND STRING CONSTANTS
Character constants are enclosed by single quote characters, ’, for example ’a’. A
character constant has int type, although this may be later optimized to a char type
by the compiler.
character constant has int type, although this may be later optimized to a char type
by the compiler.
To comply with the ANSI C standard, the compiler does not support the extended char-
acter set in characters or character arrays. Instead, they need to be escaped using the
backslash character, as in the following example:
acter 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 exam-
ple “hello world”. The type of string constants is const char * and the character
that make up the string are stored in the program memory, as are all objects qualified
const
ple “hello world”. The type of string constants is const char * and the character
that make up the string are stored in the program memory, as are all objects qualified
const
.
A common warning relates to assigning a string literal to a pointer that does not specify
a const target, for example:
a const target, for example:
char * cp = "hello world\n";
The string characters cannot be modified, but this type of pointer allows writes to take
place, hence the warning. To prevent yourself from trying to overwrite the string,
qualifier the pointer target as follows. See also Section 5.4.5.1 “Combining Type
Qualifiers and Pointers”.
place, hence the warning. To prevent yourself from trying to overwrite the string,
qualifier the pointer target as follows. See also Section 5.4.5.1 “Combining Type
Qualifiers and Pointers”.
const char * cp = "hello world\n";
Defining and initializing an array (i.e., not a pointer) with a string is an exception. For
example:
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.
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
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”
world”
(the last character is the nul terminator character) would be represented by the
same characters in memory. The string “hello” would not overlap with the same char-
acters in the string “hello world” as they differ in terms of the placement of the nul
character.
acters in the string “hello world” as they differ in terms of the placement of the nul
character.
Two adjacent string constants (i.e., two strings separated only by white space) are
concatenated by the compiler. Thus:
concatenated by the compiler. Thus:
const char * cp = "hello" "world";
will assign the pointer with the address of the string “hello world “.