C language learned by suffering
C language learned by suffering
How to handle strings
Let's make it an array.
In the previous section, the handling of character variables was explained.
Simply put, if you collect many of these character variables, you should end up with a string.
Collecting many variables of the same type...where have you heard that before?
That's right. If you create an array of character variables, that is, a string.
Arrays of character variables can be used as string variables in C.
However, one question arises here.
That is, how to store the number of characters in a string.
Without knowing the number of characters, it is difficult to retrieve the string.
In C, the number of characters is determined by storing a special value at the end of the string.
Such a character is sometimes specifically referred to as EOS.
In C, '\0' is treated as EOS.' \ɑ0' is equivalent to 0 as a number.
Once a string can be created, it is easy to display it.
The printf function can be used to display strings using the %s specifier.
Once you know this much, you can work with strings.
The following program is an example of storing and displaying a string in an array of character variables.
The result of executing this program is as follows
As you can see in this program, the string ends with EOS, so
The number of elements in the array must be one more than the actual number of characters to be stored.
Of course, specifying a larger number of elements is not a problem.
If there is no EOS in the string, it will be treated as a string to the end of the array.
Until by chance we find 0 (EOS) somewhere.
The string will continue to be displayed endlessly, which is very annoying.
Note that if you omit an element when initializing the array, the remainder will be filled with zeros.
If the rest is 0, then there is no problem, because it is EOS.
However, in this case, I dared to write it to show that there is an EOS at the end.
Note that if EOS is written, the number of elements can be omitted.
Simply put, if you collect many of these character variables, you should end up with a string.
Collecting many variables of the same type...where have you heard that before?
That's right. If you create an array of character variables, that is, a string.
Arrays of character variables can be used as string variables in C.
However, one question arises here.
That is, how to store the number of characters in a string.
Without knowing the number of characters, it is difficult to retrieve the string.
In C, the number of characters is determined by storing a special value at the end of the string.
Such a character is sometimes specifically referred to as EOS.
In C, '\0' is treated as EOS.' \ɑ0' is equivalent to 0 as a number.
Keywords.
EOS
A symbol indicating the end of a string. Also called the terminating character.
The first letter of End of String.
Once a string can be created, it is easy to display it.
The printf function can be used to display strings using the %s specifier.
Once you know this much, you can work with strings.
The following program is an example of storing and displaying a string in an array of character variables.
source code
#include <stdio.h>
int main(void)
{
char str[6] = { 'M', 'A', 'R', 'I', 'O', '\0'};
printf("%s\n", str);
return 0;
}
The result of executing this program is as follows
Execution Result
MARIO.
As you can see in this program, the string ends with EOS, so
The number of elements in the array must be one more than the actual number of characters to be stored.
Of course, specifying a larger number of elements is not a problem.
If there is no EOS in the string, it will be treated as a string to the end of the array.
Until by chance we find 0 (EOS) somewhere.
The string will continue to be displayed endlessly, which is very annoying.
Note that if you omit an element when initializing the array, the remainder will be filled with zeros.
source code
char str[6] = { 'M', 'A', 'R', 'I', 'O' }
If the rest is 0, then there is no problem, because it is EOS.
However, in this case, I dared to write it to show that there is an EOS at the end.
Note that if EOS is written, the number of elements can be omitted.
String initialization
In the previous section, we explained that arrays of type char can handle strings.
However, the method of assigning strings performed in the previous section is very cumbersome.
Each character must be separated and written one by one.
In C, there is a more intuitive way to initialize strings.
Until now, strings have been enclosed in "" characters, but this method can be used.
Strings enclosed in "" are sometimes referred to as string literals.
The following program is an example of rewriting the program in the previous section using string literals.
The result of executing this program is as follows
In this method, you can simply write the string normally and omit the number of elements.
In this case, the number of elements is reserved one more than the length of the string, but
If you want to reserve more space, you can specify it.
The problem with this method is that it can only be used during initialization.
If you want to assign strings later, you have to assign them to the elements one by one.
The following program is an example of assigning one element at a time.
The execution result will be the same as before.
However, the method of assigning strings performed in the previous section is very cumbersome.
Each character must be separated and written one by one.
In C, there is a more intuitive way to initialize strings.
Until now, strings have been enclosed in "" characters, but this method can be used.
Strings enclosed in "" are sometimes referred to as string literals.
String literals can be used to easily initialize strings.
The following program is an example of rewriting the program in the previous section using string literals.
Source Code
#include <stdio.h>
int main(void)
{
char str[] = "MARIO";
printf("%s\n", str);
return 0;
}
The result of executing this program is as follows
Execution Result
MARIO.
In this method, you can simply write the string normally and omit the number of elements.
In this case, the number of elements is reserved one more than the length of the string, but
If you want to reserve more space, you can specify it.
The problem with this method is that it can only be used during initialization.
If you want to assign strings later, you have to assign them to the elements one by one.
The following program is an example of assigning one element at a time.
source code
#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 execution result will be the same as before.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.