char string[20] = { ‘w’ , ’e’ , ‘l’ , ‘c’ , ‘o’ , ‘m’ , ‘e’ ,’-’ , ‘t’ , ‘o’ ,’-’, ’f’ , ‘t’ , ‘l’ ,’ ’, ‘\0’}; (or)
char string[20] = “welcome-to-ftl”; (or)
char string [] = “welcome-to-ftl”;
Difference between above declarations are, when we declare char as “string[20]“, 20 bytes of memory space is allocated for holding the string value.
When we declare char as “string[]”, memory space will be allocated as per the requirement during execution of the program.
char a[5]=”ftl”; //compiler raises an error because the size of array is not enough for string
char a[20]=”hello”; //remaining 14 locations automatically filled with null characters
Accessing individual character in a string is just as accessing elements in the array. The index of the character should be specified as array subscript.
char c[]=”ftl”;
The expression ‘c[0]’ accesses the first character ‘f’
‘c[1]’ accesses the third character ‘t’,……….
Similarly remaining elements are accessed.