Next: Variable Attributes, Previous: Character Escapes, Up: C Extensions
Specifying the -fpascal-strings option will cause the compiler to recognize and construct Pascal-style string literals. This functionality is disabled by default; furthermore, its use in new code is discouraged.
Pascal string literals take the form `"\pstring"'. The special escape sequence `\p' denotes the Pascal length byte for the string, and will be replaced at compile time with the number of characters that follow. The `\p' may only appear at the beginning of a string literal, and may not appear in wide string literals or as an integral constant.
As is the case with C string literals, Pascal string literals are terminated with a NUL character; this character is not counted when computing the value of the length byte. The maximum `unsigned char' value that can be stored in the length byte is also the maximum permissible length for the Pascal literal itself. On most target platforms, this value is 255 (excluding both the length byte and the terminating NUL).
Pascal-style literals are treated by the compiler as being of type `const unsigned char []' in C++ and `unsigned char []' (or `const unsigned char []', if the -Wwrite-strings option is given) in C. Pascal string literals may be used as static initializers for `char' arrays (whose elements need not be `unsigned' or `const'). They may also be converted to `const unsigned char *' and, in the C language to `const char *' of any signedness (In C, if the -Wwrite-strings is not given, then `const' may be omitted as well). For example:
const unsigned char a[] = "\pHello"; char b[] = "\pGoodbye"; const unsigned char *c = "\pHello"; const signed char *d = "\pHello"; /* error in C++ */ char *e = "\pHi"; /* error in C++; warning in C with -Wwrite-strings */ unsigned char *f = "\pHello"; /* error in C++ */
In all other respects, Pascal-style string literals behave the same as ordinary string literals. For example, if a program attempts to modify the conents of a Pascal-style string literal at run-time, the behaviour is undefined, unless the -fwritable-strings option is used.
Pascal-style literals are useful for calling external routines that expect Pascal strings as arguments, as is true with some Apple MacOS Toolbox calls.