So far, I've dealt with strings numerous times within programs.
For some reason, the variable used to store strings was never explained.
The reason is simple."This is because C doesn't have variables to store strings."
The reason there are no variables for strings is because strings have special properties.
The length of the string cannot be predicted in advance.
Sometimes it can be as short as five characters, other times it can be thousands.
"That's why the required memory size can vary depending on the circumstances."
"Due to these circumstances, C does not have variables specifically for strings."
In other languages
In programming languages other than C, string variables are typically declared with the following structure.
1、メモリを固定的に確保(メモリ消費量のムダが多くなるが、高速で扱いやすい) 2. Dynamically allocate memory (reduces memory consumption but slows down performance).)
Both have their strengths and weaknesses, and are suitable for different situations. In C, programmers have the freedom to represent strings in various ways.
To handle characters,
"As mentioned in the previous section, C doesn't have string variables,"
String variables may not be available, but character variables are provided.
Unlike strings, characters are always a single unit, which allows them to be handled by variables.
In C, the `char` type is provided as a character variable.
A `char` variable can store a single character.
Characters are represented by enclosing them in single quotes.
"Also, you can display it using the %c specifier in the printf function."
The following program demonstrates storing and displaying a character in a char variable.
Source code
#include <stdio.h>
int main(void)
{
char c = 'A';
printf("%c\n", c);
return 0;
}
The results of this program's execution are as follows:
Execution results
A
In this way, using a char type allows you to treat characters as you would variables.
No full-width characters allowed.
Actually, this method cannot handle full-width characters. char型で記憶できる文字の種類は最大で255種類が限界で、 It's simply impossible to memorize thousands of Japanese characters.
この問題を解決する手段として、wchar_t型が用意されているのですが、 For now, we won't cover it because I want you to first get a good grasp of the basic character variables.
Character encoding
"In the previous section, we explained that characters can be handled as a single character using the char type,"
This utilizes the mechanism of character encoding used in computers.
キーワード
【Character encoding】
"A method of representing characters used in computers by assigning them a unique number for a one-to-one correspondence." The ASCII standard assigns half-width alphabets and symbols. day本語を扱えるCodeとして、JIS、シフトJIS、EUCが使われている他、 Unicode is becoming the dominant code for handling languages around the world.
A character encoding is a method of representing characters by assigning each one a unique number.
The following table provides examples of the ASCII code, the most commonly used standard for a numbering system using single-byte characters.
番号
番号(hexadecimal)
文字
62
0x3e
63
0x3f
?
64
0x40
@
65
0x41
A
66
0x42
B
67
0x43
C
68
0x44
D
97
0x61
a
98
0x62
b
Please don't try to memorize this table by rote.
こういった表を出すと、受験勉強のように丸暗記しようとする人が出てくるのですが、 There's absolutely no need to memorize it.Of course, I didn't memorize it either. 実はAが65くらいは覚えていたりしますが・・・ 重要なことは、文字には番号がついているという仕組みあり、番号そのものではありません
Assigning a character to a char variable is simply assigning a number.
In essence, the char type is exactly the same as a regular integer type.
For example, in the program in the previous section, we assigned 'A' to the char variable c.
This is because 'A' is assigned to position 65, so the compiler interprets 'A' as 65.
It was simply a matter of assigning 65 to c.
Also, the 'A' displayed by the printf function was due to the variable's value being 65.
I simply processed it to display 'A' using the printf function.
In essence, in computers, all characters are represented by numbers.
It essentially just stores a number.
Computation on characters
As explained in the previous section, characters stored as `char` types are, in fact, simply numbers.
This can be leveraged to perform calculations on characters.
For example, in character encodings, character numbers are essentially defined in sequential order.
In half-width alphabets, A is assigned the value 65, B is 66, C is 67, and so on.
"In other words, adding to A allows you to extract a letter corresponding to its position in the alphabet."
The following program is an example of extracting the tenth letter.
Source code
#include <stdio.h>
int main(void)
{
char c = 'A' + 9; /* 最初は0なので9を足す */
printf("%c¥n", c);
return 0;
}
The results of this program's execution are as follows:
Execution results
J
When using numbers, you can also determine the original value through subtraction.
Numbers also have character codes assigned to them, for example, '0' is assigned code 48.
Subtracting the number '0' from a digit converts it to a numerical value that can be used for calculation.
The following program is an example of converting numbers to numerals.
Source code
#include <stdio.h>
int main(void)
{
char c = '8'; /* digit */
int suuti = c - '0'; /* numericsに変換 */
printf("%d\n",suuti);
return 0;
}
The results of this program's execution are as follows:
Execution results
8
This program displays the result of the numeric conversion using the %d specifier.
However, a problem with the previous program is that it also converts characters other than numbers.
"For example, 'A' is the 65th character, so converting 'A' to a number results in 17."
To solve this problem, you need to determine if the character to be converted is a digit.
This is relatively easy.It simply checks if the character code falls between '0' and '9'.
The following program is an example of converting digits to numbers and converting anything that isn't a digit to zero.
Source code
#include <stdio.h>
int main(void)
{
char c = 'A'; /* digit */
int suuti;
if (c >= '0' && c <= '9' ) {
/* 判定部minute */
suuti=c - '0' ; /* numericsに変換 */
} else {
suuti=0;
}
printf("%d\n",suuti); return 0;
}
The results of this program's execution are as follows:
Execution results
0
Of course, if you specify numbers, they will be properly converted to numeric values.
The method for checking if a character is a number can also be applied to alphabets.
However, it's not continuous from uppercase 'Z' to lowercase 'a'.
I need to check 'A' through 'Z' and 'a' through 'z' respectively.
Furthermore, these features are functional, allowing for the use of the following function.
In addition, using these functions requires including ctype.h.
名前
文字種類
文字一覧
isalnum
英digit
A~Z a~z 0~9
isdigit
decimal number
0~9
isxdigit
hexadecimal
A~F a~f 0~9
isalpha
英字
A~Z a~z
isupper
英大文字
A~Z
islower
英小文字
a~z
ispunct
symbol
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
isspace
スペース
0x09~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)) {
/* 判定部minute */
suuti = c - '0';
} else {
suuti = 0;
}
printf("%d\n",suuti);
return 0;
}
The results will be exactly the same as before.
Execution results
0
About This Site
Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.