Handling Characters
Variables for handling strings
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.
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 prepared using the following structures:
1. Fixed memory allocation (more wasteful memory consumption, but fast and easy to handle)
2. Variable memory allocation (reduces memory consumption, but slower)
Thus, both approaches have their pros and cons, each suited to different situations.
In C, programmers are free to represent strings using any method they choose.
1. Fixed memory allocation (more wasteful memory consumption, but fast and easy to handle)
2. Variable memory allocation (reduces memory consumption, but slower)
Thus, both approaches have their pros and cons, each suited to different situations.
In C, programmers are free to represent strings using any method they choose.
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.
The output of this program is as follows:
In this way, using a char type allows you to treat characters as you would 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 output of this program is as follows:
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. The char type can store a maximum of 255 different character types, and it cannot store the thousands of characters used in Japanese. The wchar_t type is provided as a solution to this problem, but since I want you to thoroughly understand the basic character variables first, we won't cover it here.
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.
A method of representing characters used on computers by assigning them unique numerical values. The ASCII standard assigns codes to half-width alphabetic characters and symbols. For handling Japanese, codes such as JIS, Shift JIS, and EUC are used, while Unicode is widely adopted for handling languages worldwide.
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.
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.
This utilizes the mechanism of character encoding used in computers.
Keyword
ćcharacter encodingć
A method of representing characters used on computers by assigning them unique numerical values. The ASCII standard assigns codes to half-width alphabetic characters and symbols. For handling Japanese, codes such as JIS, Shift JIS, and EUC are used, while Unicode is widely adopted for handling languages worldwide.
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.
| 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 |
Please don't try to memorize this table by rote.
When I present tables like this, some people try to memorize everything by rote, like cramming for exams. But there's absolutely no need to memorize anything. Of course, I haven't memorized it either. Though I do remember that A is around 65... What's important is the system where characters are assigned numbersānot the numbers themselves.
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.
The output of this program is as follows:
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.
The output of this program is as follows:
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.
The output of this program is as follows:
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.
The following program is an example of rewriting the previous program using the isdigit function.
The results will be exactly the same as before.
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; /* Since it starts at 0, add 9. */
printf("%c„n", c);
return 0;
}
The output of this program is as follows:
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'; /* Convert to numerical values */
printf("%d\n",suuti);
return 0;
}
The output of this program is as follows:
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' ) {
/* Judgment section */
suuti=c - '0' ; /* Convert to numerical values */
} else {
suuti=0;
}
printf("%d\n",suuti); return 0;
}
The output of this program is as follows:
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.
| name | Character type | Character List |
|---|---|---|
| isalnum | alphanumeric characters | Aļ½Z aļ½z 0ļ½9 |
| isdigit | decimal number | 0ļ½9 |
| isxdigit | hexadecimal | Aļ½F aļ½f 0ļ½9 |
| isalpha | Alphabet | Aļ½Z aļ½z |
| isupper | Capital letters | Aļ½Z |
| islower | English small text | aļ½z |
| ispunct | symbol | !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ |
| isspace | Space | 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)) {
/* Judgment section */
suuti = c - '0';
} else {
suuti = 0;
}
printf("%d\n",suuti);
return 0;
}
The results will be exactly the same as before.
Results
0
About This Site
Learning C language through suffering (Kushi C) isThis 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.




