cp_1_lect_38 - Character Arrays and Strings A string is a...

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

View Full Document Right Arrow Icon
Character Arrays and Strings A string is a series of characters treated as a unit. No standard adopted for implementation (Pascal strings differ from C strings). A string in C is a variable length array of characters that is delimited by the null character. Examples: Hello “Well done !” H e l l o \0 ‘h’ : h (a char) “h” : h\0 (a string) “ ” : \0 (empty string) ‘\0’ and ‘0’ are they equal? Ascii values are different (‘\0’: 0 and ‘0’: 48)
Background image of page 1

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

View Full DocumentRight Arrow Icon
5/10/10 char name[8] = “John”; int i = 2; printf (“Character at index %d is %c.\n”, i, name[i]); A Char in a String output: Character at index 2 is h. index 0 index 4 J o h n \0 0x3000 0x3007 name   is starting at address location :   0x3000
Background image of page 2
5/10/10 A Char in a String continued… index 2 J o h n \0 0x3000 0x3007 name is 0x3000 char name[8] = “John”; name[2] = ‘P’; printf (“Name: %s\n”, name); P output: Name: JoPn
Background image of page 3

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

View Full DocumentRight Arrow Icon
Declaring and Initializing Strings char city [7]; char city [7] = “PILANI”; char name [12] = “BITS PILANI”; char name [12] = { ‘B’,‘I’,‘T’,‘S’, ,‘P’,‘I’,‘L’,‘A’,‘N’,‘I’,‘\0’}; char name [ ] = “BITS PILANI”; char name [15] = “BITS PILANI”; must B I T S P I L A N I \0 \0 \0 \0 Can store at most 6 letters, because of `\0’
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/07/2010 for the course CS&IS TA C162 taught by Professor Kavitha during the Spring '09 term at Birla Institute of Technology & Science.

Page1 / 25

cp_1_lect_38 - Character Arrays and Strings A string is a...

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

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