STRINGS: 

In „C‟ language the group of characters, digits, and symbols enclosed within double quotation ( " " ) marks are called as string otherwise a string is an array of characters and terminated by NULL character which is denoted by the escape sequence „\0‟. 

C Strings :

Declaration of String: C does not support string as a data type. However, it allows us to represent strings as character arrays. In C, a string variable is any valid C variable name and it is always declared as an array of characters. 

The general form of declaration of a string variable is : 

Syntax: char string_name[size]; 

The size determines the number of characters in the string name 

Different ways of initialization can be done in various ways :

 1 : Initializing locations character by character. 

2 : Partial array initialization. 

3 : Initialization without size. 

4 : Array initialization with a string . 


1 : Initializing locations character:

 by character Consider the following declaration with initialization, Char b[9]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟,‟T‟,‟E‟,‟R‟}; 

2 : Partial Array Initialization : 

If the characters to be initialized is less than the size of the array, then the characters are stored sequentially from left to right.The remaining locations will be initialized to NULL characters automatically .

3 : Initialization without size : 

consider the declaration along with the initialization char b[]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟,‟T‟,‟E‟,‟R‟}; 

In this declaration, The compiler will set the array size to the total number of initial values i.e 8. The character will be stored in these memory locations in the order specified. 

4) Array Initilization with a String : 

consider the declaration with string initialization. char b[ ] = “COMPUTER”;

 Here, the string length is 8 bytes. But , string size is 9 bytes. 

So the compiler reserves 8+1 memory locations and these locations are initialized with the characters in the order specified. The string is terminated by \0 by the compiler. 


Reading and Writing Strings : The „%s‟ control string can be used in scanf() statement to read a string from the terminal and the same may be used to write string to the terminal in printf() statement.

 Example : char name[10]; 

scanf(“%s”,name);

 printf(“%s”,name);