C language learned by suffering
C language learned by suffering
How to Handle Characters
Variables that handle strings
We've dealt with strings many times in our programs.
For some reason, we have never explained the variables that store strings.
The reason for this is simple: the C language does not have a variable to store strings.
The reason there are no variables for strings is that strings have special properties.
The number of characters in a string cannot be predicted in advance.
It can be as few as five characters or as many as thousands of characters.
Thus, the size of memory required varies from case to case.
Because of this, the C language does not have variables for strings.
For some reason, we have never explained the variables that store strings.
The reason for this is simple: the C language does not have a variable to store strings.
The reason there are no variables for strings is that strings have special properties.
The number of characters in a string cannot be predicted in advance.
It can be as few as five characters or as many as thousands of characters.
Thus, the size of memory required varies from case to case.
Because of this, the C language does not have variables for strings.
In other languages
Programming languages other than C provide string variables with the following structure.
1. Fixed memory allocation (more wasteful memory consumption, but fast and easy to handle)
2, Variable memory allocation (reduces memory consumption, but is slower.)
Thus, both have their advantages and disadvantages, and are not suitable for everyone.
The C language allows programmers to represent strings in any way they wish.
1. Fixed memory allocation (more wasteful memory consumption, but fast and easy to handle)
2, Variable memory allocation (reduces memory consumption, but is slower.)
Thus, both have their advantages and disadvantages, and are not suitable for everyone.
The C language allows programmers to represent strings in any way they wish.
To work with characters
In the previous section, I mentioned that there are no string variables in the C language.
Even if there are no string variables, character variables are available.
Unlike strings, characters can be handled by variables because they are always a single character.
The C language provides the char (character) type as a character variable.
A variable of type char can store a single character.
Characters are represented by enclosing them with ''.
The following program is an example of storing and displaying characters in a variable of type char.
The result of executing this program will be as follows
Thus, the char type can be used to treat characters as if they were variables.
Even if there are no string variables, character variables are available.
Unlike strings, characters can be handled by variables because they are always a single character.
The C language provides the char (character) type as a character variable.
A variable of type char can store a single character.
Characters are represented by enclosing them with ''.
It can also be printed with the printf function using the %c specifier.
The following program is an example of storing and displaying characters in a variable of type char.
Source Code
#include <stdio.h>
int main(void)
{
char c = 'A';
printf("%c¥n", c);
return 0;
}
The result of executing this program will be as follows
Execution Result
A
Thus, the char type can be used to treat characters as if they were variables.
No double-byte characters
Actually, this method cannot handle double-byte characters.
The maximum number of character types that can be stored in the char type is 255.
It is not possible to store thousands of Japanese characters.
The wchar_t type is provided as a solution to this problem.
We do not deal with this here because we want you to learn the basic character variables well first.
The maximum number of character types that can be stored in the char type is 255.
It is not possible to store thousands of Japanese characters.
The wchar_t type is provided as a solution to this problem.
We do not deal with this here because we want you to learn the basic character variables well first.
character code
In the previous section, we explained that the char type can handle a single character.
This is based on the character code system used in computers.
A character code is a way of representing a character by assigning a number to it that corresponds one-to-one.
The following table shows an example of ASCII code, the most widely used standard for numbering tables for one-byte characters.
Assigning a character to a variable of type char is simply assigning this number.
So, as a matter of fact, the char type is exactly the same as an ordinary integer type.
For example, in the program in the previous section, 'A' is assigned to the char variable c, but
This is because 'A' is assigned the number 65, so the compiler interprets 'A' as 65, and the
The 65 is simply assigned to c.
Also, the printf function displayed A because the variable's contents were 65.
The printf function simply processed the data so that it would be displayed as "A".
In other words, on a computer, all letters are represented by numbers, and
The char type is merely to store that number.
This is based on the character code system used in computers.
Keywords.
[Character code
A method of representing characters used in computers by numbering them with a one-to-one correspondence.
A standard called ASCII code assigns one-byte alphabets and symbols.
JIS, Shift JIS, and EUC are used as codes that can handle the Japanese language.
Unicode is widely used as a code that can handle the world's languages.
A character code is a way of representing a character by assigning a number to it that corresponds one-to-one.
The following table shows an example of ASCII code, the most widely used standard for numbering tables for one-byte characters.
Number | Number (hexadecimal) | Character |
---|---|---|
62 | 0x3e | |
63 | 0x3f | ? |
64 | 0x40 | @@ |
65 | 0x41 | A |
66 | 0x42 | B |
67 | 0x43 | C |
68 | 0x44 | D |
97 | 0x61 | a |
98 | 0x62 | b |
Do not try to memorize this table!
When I give out a table like this, some people try to memorize it as if they were studying for an entrance exam.
There is absolutely no need to memorize it. Of course, the author has not memorized it either.
In fact, I remember about 65 A's...
The important thing is that the letters are numbered, not the numbers themselves!
There is absolutely no need to memorize it. Of course, the author has not memorized it either.
In fact, I remember about 65 A's...
The important thing is that the letters are numbered, not the numbers themselves!
Assigning a character to a variable of type char is simply assigning this number.
So, as a matter of fact, the char type is exactly the same as an ordinary integer type.
For example, in the program in the previous section, 'A' is assigned to the char variable c, but
This is because 'A' is assigned the number 65, so the compiler interprets 'A' as 65, and the
The 65 is simply assigned to c.
Also, the printf function displayed A because the variable's contents were 65.
The printf function simply processed the data so that it would be displayed as "A".
In other words, on a computer, all letters are represented by numbers, and
The char type is merely to store that number.
Calculation for characters
As explained in the previous section, the characters stored in the char type are actually just numbers.
This can also be used to perform calculations on characters.
For example, in a character code, character numbers are basically defined in order.
In the one-byte alphabet, A is 65, B is 66, C is 67, and so on.
In other words, adding to A will retrieve the number of letters of the alphabet.
The following program is an example of retrieving the tenth letter of the alphabet.
The result of executing this program will be as follows
When using numbers, you can also subtract to get the original number.
Numbers are also assigned letter numbers, for example '0' is assigned to number 48.
Subtracting the number '0' from a number converts the number into a numerical value that can be used in calculations.
The following program is an example of converting a number to a numerical value.
The result of executing this program will be as follows
The program displays the result of the conversion to numeric values with the %d specifier.
The problem with the previous program, however, is that it also converts characters other than numbers.
For example, 'A' is the number 65, so if 'A' is converted to a number, it becomes 17.
To solve this problem, it is necessary to determine if the character to be converted is a number.
This is relatively simple. Simply check if the letter number is between '0' and '9'.
The following program is an example of converting a number to a numerical value and all non-numerical values to 0.
The result of executing this program will be as follows
Of course, if you specify a number, it will be converted to a numerical value.
The method of checking whether a character is a number can also be applied to the alphabet.
However, since there is no continuity from the uppercase 'Z' to the lowercase 'a', the
'A' through 'Z' and 'a' through 'z' must be examined, respectively.
In addition, these functions are functionalized and the following functions can be used.
Note that you must #include ctype.h to use these functions.
The following program is an example of rewriting the previous program using the isdigit function.
The result is exactly the same as before.
This can also be used to perform calculations on characters.
For example, in a character code, character numbers are basically defined in order.
In the one-byte alphabet, A is 65, B is 66, C is 67, and so on.
In other words, adding to A will retrieve the number of letters of the alphabet.
The following program is an example of retrieving the tenth letter of the alphabet.
source code
#include <stdio.h>
int main(void)
{
char c = 'A' + 9; /* add 9 since it starts out as 0 */
printf("%c¥n", c);
return 0;
}
The result of executing this program will be as follows
Execution Result
J
When using numbers, you can also subtract to get the original number.
Numbers are also assigned letter numbers, for example '0' is assigned to number 48.
Subtracting the number '0' from a number converts the number into a numerical value that can be used in calculations.
The following program is an example of converting a number to a numerical value.
source code
#include <stdio.h>
int main(void)
{
char c = '8'; /* number */
int suuti = c - '0'; /* convert to number */
printf("%d\n",suuti);
return 0;
}
The result of executing this program will be as follows
Execution Result
8
The program displays the result of the conversion to numeric values with the %d specifier.
The problem with the previous program, however, is that it also converts characters other than numbers.
For example, 'A' is the number 65, so if 'A' is converted to a number, it becomes 17.
To solve this problem, it is necessary to determine if the character to be converted is a number.
This is relatively simple. Simply check if the letter number is between '0' and '9'.
The following program is an example of converting a number to a numerical value and all non-numerical values to 0.
source code
#include <stdio.h>
int main(void)
{
char c = 'A'; /* number */
int suuti;
if (c >= '0' && c <= '9') {
/* Judgment part */
suuti=c - '0' ; /* convert to number */
} else {
suuti=0;
}
printf("%d\n",suuti); return 0;
}
The result of executing this program will be as follows
Execution Result
0
Of course, if you specify a number, it will be converted to a numerical value.
The method of checking whether a character is a number can also be applied to the alphabet.
However, since there is no continuity from the uppercase 'Z' to the lowercase 'a', the
'A' through 'Z' and 'a' through 'z' must be examined, respectively.
In addition, these functions are functionalized and the following functions can be used.
Note that you must #include ctype.h to use these functions.
Name | Character Type | Character list |
---|---|---|
isalnum | Alphanumeric | A to Z a to z 0 to 9 |
isdigit | Decimal number | 0-9 |
isxdigit | Hexadecimal | A to F a to f 0 to 9 |
isalpha | alphabetic character | A to Z a to z |
isupper | English capital letter | A to Z |
islower | English lowercase | a~z |
ispunct | symbol | !" #$%&'()*+,-. /:; <=>? @[\]^_`{|}~ |
isspace | Space | 0x09 to 0x0D 0x20 |
The following program is an example of rewriting the previous program using the isdigit function.
Source code
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char c = 'A';
int suuti;
if (isdigit(c)) {
/* Judgment part */
suuti = c - '0';
} else {
suuti = 0;
}
printf("%d\n",suuti);
return 0;
}
The result is exactly the same as before.
Execution Result
0
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.