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


  How to treat character   

  1. ..clause 1..: Variable that treats character string
  2. ..clause 2..: To treat the character
  3. ..clause 3..: Character-code
  4. ..clause 4..: Calculation of character

[1] Variable that treats character string

Up to now, while programming it though the character string has been treated many times
It had not explained the variable that memorized the character string even once why.
The reason is easy. It is because there is no variable that memorizes the character string in C language.

The purpose of the variable's for the character string there is with the character with a special character string.
It cannot forecast how many characters to become to the character string beforehand.
It is likely to become the number of characters of thousands of characters if ending by about five characters.
The size of the memory needed according to circumstances is different in this manner.

There is no variable for the character string in C language because there are such circumstances.

[   Other languages   ]
A lot of languages except C language prepare the character string variable by the following method.
Secure 1 and the memory fixing. (The limitation of the number of characters, and the memory become useless. )
Secure 2 and the memory in changeability. (The speed slows. )
(In one, old BASIC, PASCAL, and 2 are Java etc.The function that C++ looks alike to two, too is provided. )
Because the programmer can choose a free method in C language, it is flexible.

It returns to contents.


[2] To treat the character

Though the character string variable was written it not is in C language in the preceding clause
The character string variable is prepared in case of the character variable even in case of not being.
Because the character is one character without fail unlike the character string, it is possible to treat by the variable.

In C language, char (character) type is prepared as a character variable.
One character can be memorized in the variable of the char type.
The character is expressed by enclosing it with ''

Moreover, if %c finger fixed child is handled, it is possible to display it by the printf function.
The following program is an example of memorizing and displaying the character in the variable of the char type.

 
#include <stdio.h> 

int main(void)  
{
	char c = 'A';  
	printf("%c\n",c);  
	return 0;
}
The execution result of this program becomes as follows.

A
If the char type is used, the character can be handled as well as the variable in this manner.

[ The   em-size character is useless   ].
Actually, the em-size character cannot be treated by this method.
255 kinds : the kind of the character that can be memorized by the char type in maximums in the limit.
Thousands of character Japanese character cannot be memorized.

Though the wchar_t type is prepared as a means to solve this problem
It seems to be able hardly to touch by the introduction.
Will treat the wchar_t type in volume 2 because it becomes confusing.

It returns to contents.


[3] Character-code

Though it explained one character can be handled by the char type in the preceding clause
This uses the mechanism of the character-code used with the computer.


[   Character code   ]
Method of expression applying number that corresponds to character used with computer by couple 1.
The alphabet and the sign of normal-width are allocated in the standard of ASCII code.
Another for whom JIS, shift JIS, and EUC are used as code in which Japanese can be treated
As a code in which a language all over the world can be treated Unicode is widespread.
The character-code is a method of the expression applying the number that corresponds to the character by couple 1.
For instance, normal-width The 65th is allocated in A.
Substituting the character for the char type variable only substitutes this number.
In short, The char type is quite the same as a usual integer type.

For instance, though 'A' was substituted for char type variable c in the program of the preceding clause
'A' ..: of the compiler because of the allocation of the 65th A to this.. It is interpreted as 65.
It doesn't pass ..only the substitution of the 65 for c...

Moreover, in the printf function Because contents of the variable were 65 A was displayed
In the printf function It only processed it to display as A.

In a word, all characters are shown by the number on the computer.
The char type is only to memorize the number.

It returns to contents.


[4] Calculation of character

Though the character memorized by the char type explained it is actually a mere number in the preceding clause
If this is used, it is possible to calculate for the character.

For instance, the number of the character is basically defined in the character-code in order.
A is decided, and in the normal-width alphabet, 65 and B are decided and 66 and C have been decided as 67.
In a word, the character of the alphabet how many can be taken out when adding up to A.
The following program is an example of taking out the tenth alphabet.

 
#include <stdio.h> 

int main(void)  
{
	char c = 'A' + 9; /* the first */ that adds nine because it is 0 Printf("%c\n",c);  
	return 0;
}
The execution result of this program becomes as follows.

J
When the figure is used, it can know an original numerical value because of the subtraction.
The number of the character is allocated in the figure and ,for instance, '0' is allocated. to the 48th
If the number of '0' is subtracted from the figure, it is converted into the numerical value and it is possible to use it to calculate.
The following program is an example of converting the figure into the numerical value.
 
#include <stdio.h> 

int main(void)  
{
	char c = '8'; /* figure */  int suuti =  c- '0'; /* Convert it into the numerical value */ Printf("%d\n",suuti);  
	return 0;
}
The execution result of this program becomes as follows.

8
In this program, %d finger fixed child is made to display the result of conversion into the numerical value.

However, the problem of the program ahead is to be converted characters other than the figure.
For instance, when 'A' is converted into the numerical value because 'A' is the 65th, it becomes 17.
It is necessary to judge this problem and to judge whether the converted character is a figure to solve it.

This is comparatively easy. It is only examined whether the number of the character is between '9'0' -'.
It is an example of the conversion of the following program of the figure into the numerical value, and conversion into all 0 excluding the figure.

 
#include <stdio.h> 

int main(void)  
{
	char c = 'A'; /* figure */  Int suuti;    /* judgment part */ suuti =   Convert into c-'0' ;/* numerical value */. 
	} else { suuti = 0; 
	}
	printf("%d\n",suuti);  
	return 0;
}
The execution result of this program becomes as follows.

0
Of course, when the figure is specified, it is neatly converted into the numerical value.

The method of examining whether a certain character is a numerical value can be applied also to the alphabet.
However, because it is not consecutive from 'Z' of the capital letter in 'a' of the small letter
'A' It is necessary to examine 'Z -' and 'z 'a' -' respectively.

Moreover, it is made to the function, and the following function can be used for these functions.
# include should do ctype.h to the use of these functions.
Name Character kind Character list
isalnum Alphanumeric character (A〜Z,a〜z,0〜9)
isdigit Decimal number (0〜9)
isxdigit Hexadecimal number (A〜F,a〜f,0〜9)
isalpha Alphabet (A〜Z,a〜z)
isupper English capital letter (A〜Z)
islower English small letter (a〜z)
ispunct Sign (!"#$%&'()*+,- /:;<=>?@^_`{|}~)
isspace Space (0x09〜0x0D,0x20)
The following program is an example of rewriting the program ahead by using the isdigit function.

 
#include <stdio.h> #include <ctype.h> 

int main(void)  
{
	char c = 'A';  
	int suuti;  
	  /* judgment part */ suuti =  '0 c -';
	} else { suuti = 0; 
	}
	printf("%d\n",suuti);  
	return 0;
}
The execution result becomes quite the same ahead.

It returns to contents.


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