String



A string in C is actually a character array. As an individual character variable can store only one character, we need an array of characters to store strings. Thus, in C string is stored in an array of characters. Each character in a string occupies one location in an array. The null character ‘\0’ is put after the last character. This is done so that program can tell when the end of the string has been reached.

Declaration of strings

char arr_name[size];

Initialization of string:

For example: char arr[20]={‘A’, ‘c’, ‘e’, ‘’, ‘P’, ‘u’, ‘b’, ‘l’, ‘i’, ‘c’, ‘a’, ‘t’, ‘i’, ‘o’, ‘n’, ‘\0’}

The string “Ace Publication” is stored as follows.

A c e P u b l i c a t i o n \0

Since the string has 15 characters (including space), it requires an array of at least, size 16 to store it. Thus, in C, a string is a one-dimensional array of characters terminated a null character. The terminating null character is important. In fact, a string not terminated by ‘\0’ is not really a string, but merely a collection of characters.