The preceding section described the handling of character variables.
Simply put, if you gather a lot of these character variables together, you should get a string.
"Don't you ever collect a bunch of variables of the same type...?"
That's right.Creating an array of character variables essentially creates a string.
In C, an array of characters can be used as a string variable.
However, a question arises here.
It's about how to store the number of characters in a string.
It's difficult to extract the string when you don't know the number of characters.
In C, the length of a string is determined by storing a special value at the end of the string.
Sometimes these characters are specifically called
EOS.
In C, '\0' is treated as the End Of String (EOS).'\0' represents the null character, which has a numerical value of 0.
【EOS】
文字列の終わりを表すsymbol。終端文字とも呼ばれる。
なお、End of String の頭文字をとっている。
If you can create a string, it's easy to display it.
The `printf` function can display strings using the `%s` specifier.
With this understanding, you can handle strings.
The following program is an example of storing and displaying a string in an array of character variables.
#include <stdio.h>
int main(void)
{
char str[6] = { 'M', 'A', 'R', 'I', 'O', '\0' };
printf("%s\n", str);
return 0;
}
The output of this program is as follows.
As you can see from this program, an EOS is placed at the end of each string.
The number of elements in the array needs to be one more than the number of characters you intend to store.
Of course, specifying a larger number of elements is fine.
If the string doesn't have an EOS, it will be treated as a string up to the end of the array.
Until an EOS (end-of-sequence) token is encountered somewhere.
I'm really frustrated with the string just endlessly displaying.
If elements are omitted during array initialization, the remaining positions will be filled with zeros.
char str[6] = { 'M', 'A', 'R', 'I', 'O' };
Even so, a zero will be inserted, and that will be the EOS, so it's fine.
However, I wrote it this time with the deliberate inclusion of an EOS at the end.
"Note that you can omit the number of elements when writing EOS."
In the previous section, we explained how to handle strings using a char array.
However, the method of assigning strings as done in the previous section is quite cumbersome.
I have to write it out character by character.
"In C, there's a more intuitive way to initialize strings."
"We've been enclosing strings in double quotes so far, but here's how to use them."
Sometimes, strings enclosed in "" are specifically referred to as
string literals.
String literals make it easy to initialize strings.
The following program is an example of rewriting the previous program as a string literal.
#include <stdio.h>
int main(void)
{
char str[] = "MARIO";
printf("%s\n", str);
return 0;
}
The output of this program is as follows.
With this method, you only need to write the string directly, and you can omit the element count.
In this case, the number of elements is allocated one more than the length of the string.
You can also specify it if you want to reserve extra space.
The problem with this approach is that it can only be used during initialization.
If you want to assign strings later, you have no choice but to assign them one by one.
The following program demonstrates an example of assigning values to elements one by one.
#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;
}
The result will be the same as before.