CHARACTER STRINGS IN C!
A Character String is a sequence of characters that are stored in consecutive memory locations followed by a null character (\0). The null character has an ASCII code 0 and is called the end-of-string marker in C. For e.g., the string constant “CAT” is stored in the memory as follows:
Internally, each memory location of the above string holds an ASCII equivalent of the respective character. The null character (a byte with value zero) is placed at the end of the string.
String constants are always enclosed in double quotes as follows:
“Hello, World!”
String constants are useful while conveying some message to the user. For e.g., the statement
Printf(“Enter an ASCII code (0 – 127):”);
will display the string (Enter an ASCII code (0 – 127):) on the screen. Strings are also used for storing and manipulating text such as words, names and sentences.
Declaring String Variables
A string variable is declared as one dimensional array of characters as follows:
char array-name[size];
where, size is an integer that specifies the length of the string. The maximum length of the string is always one less than the size (size-1), because one storage location must be reserved for storing the end of the string character. For instance,
char title[50];
declares an array named title with 50 memory locations. But, the maximum number of characters that can be stored in this array is 49, because of the reason that the last character should be an end-of-string character.
Another way of declaring a string is using character pointer. A pointer can be considered as an alternative to an array, because the name of the array itself is a pointer to the first location of the array. The following example illustrates this:
char * title;
title = “C Programming”;
The above lines of code will declare a pointer variable of type char that points to the string “C Programming”, which is stored in memory using consecutive memory locations. The pointer title points at the first character of the string, and it reads up to the end of the string for retrieving the string.
The input and output operations with strings through pointers can be done as follows:
char * title;
scanf(“%s”, title);
printf(“%s”, title);
Initializing Strings
Strings can be initialized at the time of its declaration using the following syntax:
char array-name[size] = {list of values separated by comma};
For instance, the statement:
char month[] = {‘A’, ‘p’, ‘r’, ‘i’, ‘l’, 0};
defines the string variable month and assigns to it the string “April.”. The actual size of the string is 6. Because, the last character is 0 (zero), which indicates the end of the string and it occupies the last position of the character array. The end-of-string can also be marked with the character ‘\0’.
The end-of-string character must be given explicitly when the initialization is done character by character. But, for a string constant, it is assigned implicitly as in the following statement:
char month[] = “April”;
In this case, the compiler takes care of storing the ASCII codes of the characters of the string in memory, and it assigns the NULL character at the end.