String.pdf - Strings By Ekta Rani UID 24982 Lovely Professional University What are strings \u2022 The way group of elements are stored in integer array in

String.pdf - Strings By Ekta Rani UID 24982 Lovely...

This preview shows page 1 - 18 out of 61 pages.

Strings By Ekta Rani UID 24982 Lovely Professional University
Image of page 1
What are strings The way group of elements are stored in integer array, in same way group of characters can be stored in a character array. Character array can be called as Strings. A string is one dimensional array of characters terminated by a null (‘ \ 0’).
Image of page 2
Declaration and initialization Data_type name_of_array []={‘A’,’M’,’A’,’N’,’ \ 0’}; Char name[]={‘A’,’M’,’A’,’N’,’ \ 0’}; Last character is always NULL character. It looks like two \and 0 but it is actually one character. String can be initialized as (shortcut for initializing strings) Char name[]=“AMAN”;
Image of page 3
Null Character There is difference between ‘ \ 0’ and ‘0’ ASCII value of ‘ \ 0’ is 0 and ASCII value of ‘0’ is 48 Terminating character is important it’s the only way we can know that where character array ends. It is not necessary to put ‘ \ 0’ in end. C insert the null character automatically.
Image of page 4
Character array stored in memory A M A N \0 6518 6519 6520 6521 6522 Array name Memory Address 0 1 2 3 4
Image of page 5
Program to print a string #include<stdio.h> int main() { char name[]="aman"; int i=0; while(i<=4){ printf("%c",name[i]); i++; } return 0; }
Image of page 6