{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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

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

View Full Document Right 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");
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.
Image of page 3

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

View Full Document Right 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.
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.
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern