String in C Program Language - What is String constant with examples | Coding classpoint

String



Array of character is called a string. It is always terminated by the NULL character. String is a one dimensional array of character. 

We can initialize the string as 
                                      
                            char name[]={‘j’,’o’,’h’,’n’,’\o’};

Here each character occupies 1 byte of memory and last character is always NULL character. Where ’\o’ and 0 (zero) are not same, where ASCII value of ‘\o’ is 0 and ASCII value of 0 is 48. Array elements of character array are also stored in contiguous memory allocation.

From the above we can represent as;


The terminating NULL is important because it is only the way that the function that work with string can know, where string end.

String can also be initialized as; 

char name[]=”John”; 

Here the NULL character is not necessary and the compiler will assume it automatically.  

String constant (string literal)

A string constant is a set of character that enclosed within the double quotes and is also called a literal. Whenever a string constant is written anywhere in a program it is stored somewhere in a memory as an array of characters terminated by a NULL character (‘\o’).

Example – “m”

                   “Tajmahal” 
                   “My age is %d and height is %f\n”

The string constant itself becomes a pointer to the first character in array. 

Example-char crr[20]=”Taj mahal”;



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.