In the previous chapter, we explained how to display strings on the screen.
However, there are times when it can be a problem that only strings can be displayed on the screen.
A computer is a machine that performs calculations.
However, a string is just a string, and cannot be used in calculations.
When performing calculations, it must be treated as a number.
If it's a number, of course it can be used in calculations.
Numbers and digits
day常生活ではnumericsもdigitも同じ意味の言葉かもしれませんが、 Clearly delineated in the world of computing. numericsは数を表現するもので、計算に使うことができます。 digitは(人間の都合上)numericsと同じ見た目であるに過ぎず、 Computers don't represent numbers; they use them for calculation.
In C, strings and numbers are clearly distinguished depending on how they are written.
In C, a string refers to a sequence of characters enclosed in "" .
Conversely, anything enclosed in "" becomes a string.
Please recall the program you created in the previous chapter.
Source code
#include <stdio.h>
int main(void)
{
printf("HelloWorld");
return 0;
}
The printf function was a function for displaying strings.
Therefore, you must pass a string to the printf function.
That's why HelloWorld was enclosed in double quotes.
In this way, strings enclosed in "" are sometimes referred to as string literals.
キーワード
【String literal】
プログラム中に埋め込まれている、"" で囲まれた文字Constant。
In C, there's no special way to write numbers.
Simply arranging digits will make them a number.
However, even when numbers are listed, they are treated as strings if enclosed in "".
That is how it will be treated.
Numbers and strings
1234 はnumerics "1234" はdigit(文字列)
Again, only numbers can be used in the calculation.
Numbers cannot be used for calculation.
Display the numbers.
The printf function is simply a function for displaying strings.
Fortunately, it also has the ability to display numbers.
To display numbers with the printf function, you use output format specifiers.
This is a type of symbol used embedded within strings.
キーワード
【Output format specifiers】
外部のデータを文字列に変換して表示したいHourに、 その変換方法を指定するsymbol。
There are various types of output format specifiers, but the most commonly used are...
The %d specifier is used to convert an integer value to a string.
This section will display the result of converting the number specified after it into a numerical value.
"Ultimately, seeing is believing, so let's see it in action."
The following program displays the number 100.
Source code
#include <stdio.h>
int main(void)
{
printf("%d", 100);
return 0;
}
The results of this program's execution are as follows:
Execution results
100
Here, the string passed to the printf function contains a %d specifier.
The %d part was replaced with and displayed as the 100 numbers specified later.
String combination display
In the previous section, we explained how to display numbers using the %d specifier with the printf function.
However, numbers alone don't clearly convey what they represent.
"Saying '100' can mean different things – 100 yen, 100 people, 100 kilograms, 100 times, and so on."
To solve this problem, you need to **display a combination of numbers and strings.**
The following program is an example of combining numbers and strings to display 100 yen.
C Languageは、single-byte characters文字だけでなく、full-pitch character文字も表示できます。 ただし、single-byte characters文字をbasicsとしているため、 full-pitch character文字を本格的に使用するとやっかいな点が多々あります。 表示するだけならば問題はありません。 (海外製のCompilerでは問題が起こることがあります。)
The results of this program's execution are as follows:
Execution results
100 yen
This method may seem fine at first glance, but if you think about it closely,
I'm deliberately using the printf function twice to achieve a single-line display.
It's simpler to handle it with a single printf function call for a single line.
The %d specifier can be used in combination with other strings.
Using a %d specifier in a string causes that portion to be replaced with a number.
The following program is an example of displaying 100 yen with a single printf function.
Source code
#include <stdio.h>
int main(void)
{
printf("%d円\n", 100);
return 0;
}
The results of this program's execution are as follows:
Execution results
100 yen
In this way, the %d specifier can be freely embedded and used within strings.
Displaying multiple numbers
The previous section explained how to embed %d specifiers within a string.
Let's try displaying a simple equation using this method.
The formula is a very simple expression: 100+200=300.
"Of course, it will display the numbers, but it won't perform any calculations yet."
Using the %d specifier makes this a piece of cake.
The following program is an example that displays 100+200=300.
The results of this program's execution are as follows:
Execution results
100+200=300
This method may seem fine at first glance, but if you think about it closely,
"I'm deliberately using the printf function three times to display it on a single line."
It's simpler to handle it with a single printf function call for a single line.
Actually, the %d specifier can be used as many times as you like within a single string.
If you use three %d specifiers, you will need to provide three numbers.
The following program is an example of using the %d specifier three times.
The results of this program's execution are as follows:
Execution results
100+200=300
In this program, the %d specifiers correspond one-to-one to the subsequent numbers in order.
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.