Beruflich Dokumente
Kultur Dokumente
Chapter-11
Strings
Introduction
In our previous chapter (arrays), we studied that array is a collection of similar elements.
These similar elements could be all ints, floats, doubles or any other data type. Array of
chars are often called strings.
We are going to study character array in a separate chapter because operations that can
performed on numbers, can not be applicable on characters. For example we can multiply
two numbers but there is no sense to multiply two characters or strings.
Example
main()
{
char text[6] = {H, e, l, l, o,\0 };
int i=0;
while(i<=5)
{
printf(%c,text[i]);
i++;
}
}
Explanation:
1) In this program we handle character array in the same way as we handled int or
float array. We have assigned Hello in a char array of size 6.
2) Notice subtle difference during initialization of array. At the end \0 is assigned.
The last element of character array should be nul character denoted by \0.
3) In C, Strings are always terminated by nul character. It is great importance during
handling of strings. It denotes the end mark of string.
4) First character H is stored in text[0], e is stored in text[1] and so on. The last
character \0 is stored in text[5].
5) Loop is used to repeat printf() statement. Printf() is printing one character at a
time.
6) Remember nul (\0) character is not a printable symbol.
7) ASCII value of nul character is 0.
Example
main()
{
char text[6] = {H, e, l, l, o,\0 };