In the preceding clause, it explained the handling of the character variable.
If a lot of this character variables are collected if it thinks simply, it is sure to become a character string.
Collect a lot of variables of the same type ・・・ Will you have heard it somewhere?
It is so. It becomes making the array of the character variable, that is, character string.
The array of the character variable can be used as a character string variable in C language.
However, one doubt is caused here.
It is how to memorize the number of characters of character strings.
When the character string is taken out, it embarrasses it when the number of characters is not understood.
In C language, judge the number of characters by memorizing a special value at the end of the character string.
Such a character might be especially called EOS.
'\0' is treated for C language as EOS. '\0' is equivalent to 0 as the numerical value.
It is easy to make them display it if the character string can be made.
[ EOS ]
Sign that shows end of character string. It is called the terminal character.
The initial of End of String is taken.
The character string can be treated if it understands here.
The following program is an example of memorizing and displaying the character string in the array of the character variable.
The execution result of this program is as follows.#include <stdio.h> int main(void) { char str[6] = {'M','A','R','I','O','\0'}; printf("%s\n",str); return 0; }
Though it understands when this program is seen so that EOS may enter for the end of the character string
MARIO
Because it is treated as a character string until the array destination when there is no EOS in the character string
By chance until 0(EOS) is found somewhere
The length and the character string will keep being displayed, and it embarrasses it very much.
Because 0 enters for the remainder when the element is omitted at the initialization of array
There is no problem because 0 enters to remain even if it makes it, and it becomes EOS.char str[6] = {'M','A','R','I','O'};
In the preceding clause, it explained the character string was able to be handled by arranging the char type.
However, the method of substituting the character string done in the preceding clause is very troublesome.
It is necessary to delimit separately one character and to write.
In C language, there is a method by which the character string can be initialized more intuitively.
It is a method of using this though had enclosed with ""up to now when the character string was shown.
The character string that encloses it with ""might be especially called a string literal.
The character string can be easily initialized by using the string literal.
The following program is an example of rewriting the program of the preceding clause by the string literal.
The execution result of this program is as follows.#include <stdio.h> int main(void) { char str[] = "MARIO"; printf("%s\n",str); return 0; }
In this method, it is good only to write the character string usually, and can omit the number of elements.
MARIO
The problem of this method is to be able to use it only when declaring.
There is only substituting it for the element to substitute the character string later one by one.
The following program is an example of the individual substitution for the element.
The execution result becomes it as well as ahead.#include <stdio.h> int main(void) { char str[6]; str[0] = 'M'; str[1] = 'A'; str[2] = 'R'; str[3] = 'I'; str[4] = 'O'; str[5] = '\0'; printf("%s\n",str); return 0; }