※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Method of treating character string   

  1. ..clause 1..: Let's make it to the array.
  2. ..clause 2..: Initialization of character string

[1] Let's make it to the array.

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.


[  EOS  ]
Sign that shows end of character string. It is called the terminal character.
The initial of End of String is taken.
It is easy to make them display it if the character string can be made.
In the printf function, the character string can be displayed by handling %s finger fixed child.

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.

 
#include <stdio.h> 

int main(void)  
{
	char str[6] = {'M','A','R','I','O','\0'};  
	printf("%s\n",str);  
	return 0;
}
The execution result of this program is as follows.

MARIO
Though it understands when this program is seen so that EOS may enter for the end of the character string
The number of array elements is needed from the number of a lot of one of characters actually stored.
Of course, there is no problem even if a lot of numbers of elements are specified.

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

 
char str[6] = {'M','A','R','I','O'};
There is no problem because 0 enters to remain even if it makes it, and it becomes EOS.
However, it wrote to dare to show that there was EOS at the end this time.
When EOS is written, the number of elements can be omitted.

It returns to contents.


[2] Initialization of character string

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.

 
#include <stdio.h> 

int main(void)  
{
	char str[] = "MARIO";  
	printf("%s\n",str);  
	return 0;
}
The execution result of this program is as follows.

MARIO
In this method, it is good only to write the character string usually, and can omit the number of elements.
In this case, though the number of elements is secured from the length of a lot of one of the character string
It is possible to specify it to secure the area a lot.

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.

 
#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 becomes it as well as ahead.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.