L13 - 13. Strings String Literals String literals are...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
13. Strings
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
String Literals • String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ • A string literal may be extended over more than one line by writing \ immediately followed by the end of the line: printf("Put a disk in drive A, then \ press any key to continue\n"); • A string literal may be divided into two or more shorter strings; the compiler will join these together into one string: printf("Put a disk in drive A, then " "press any key to continue\n");
Background image of page 2
How String Literals Are Stored • The string literal "abc" is represented by the three characters a, b, and c, followed by a null character (\0): • Like any array, a string literal is represented by a pointer to the first character in the string. • A string literal of length 1 is different from a character constant. A string literal of length 1 ("a", for example) is represented by a pointer. A character constant ('a', for example) is represented by an integer value.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How String Literals Are Stored • Warning: Don’t ever use a character constant when a string literal is required (or vice-versa). The call printf("\n"); is legal, because printf expects a string as its first parameter, but printf('\n'); is not.
Background image of page 4
String Variables • A string variable is just a one-dimensional array of characters: #define STR_LEN 80 char str[STR_LEN+1]; The array should be one character longer than the string it will hold, to leave space for the null character. Warning : Failure to leave room for the null character may cause unpredictable results when using string-handling functions in the C library.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
String Variables • A string variable can be initialized:
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2012 for the course CGS 3460 taught by Professor Staff during the Summer '08 term at University of Florida.

Page1 / 23

L13 - 13. Strings String Literals String literals are...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online