- ..clause 1..: Character string and numerical value
- ..clause 2..: Display the numerical value.
- ..clause 3..: Combination display with character string
- ..clause 4..: Display of two or more numerical values
In the preceding chapter, it explained the method of displaying the character string on the screen.
The computer is a machine that calculates.
However, the character string cannot be used for the character string to calculate because it doesn't pass to the last.
It is necessary to treat as a numerical value when calculating.
Naturally, if it is a numerical value, it is possible to use it to calculate.
In C language, the character string and the numerical value are clearly distinguished depending on writing.
[ Or more value and figure ]
Though it might be a word of the meaning as which the numerical value and the figure are the same in daily life
It is distinguished clearly in the world of computer.
The numerical value can be used to calculate by the one to express the number.
The figure (For the convenience of man) doesn't pass ..the same externals as the numerical value it...
It is not possible to use it not the one it for the computer to express the number but to calculate.
Recall the program made in the preceding chapter.
The printf function was a function that displayed the character string.#include <stdio.h> int main(void) { printf("Hello, world\n"); return 0; }
The character string enclosed with ""might be especially called a string literal in this manner.
In C language, special writing need not be used when you write the numerical value.
[ Character row literal ]
Hollerith constant enclosed with ""of burial while programming it.
1234 is a numerical value.Being able to use to calculate is only a numerical value though repeated.
"1234" is a figure. (character string)
The printf function is a function that displays the character string to the last.
Use an output conversion finger fixed child to display the numerical value by the printf function.
This is a kind of the sign that buries in the character string and is used.
Being used best : though there are various kinds of output conversion finger fixed children.
[ Output conversion finger fixed child ]
When it wants to convert outside data into character string and display
Sign that specifies the conversion regime.
Anyway, [sukara] and the usage are seen in to see is to believe.
The following program is a numerical value It is program that displays 100.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { printf("%d",100); return 0; }
Here, because %d is specified in the character string passed to the printf function
100
It explained the method of displaying the numerical value by handling %d finger fixed child to the printf function in the preceding clause.
However, what on earth you mean becomes cramped only by the numerical value.
To the unit It is various 100 yen, 100 people, and 100Kg and 100 times, etc. even if it is said 100.
Solve it if the numerical value and the character string are combined and displayed to solve this problem.
The following program is an example of combining the numerical value and the character string and displaying 100 yen.
#include <stdio.h> int main(void) { printf("%d",100); Printf ("Yen \n"); return 0; }
The execution result of this program becomes as follows.
[ Treatment of this word during day ]
C language can display not only the one-byte character but also the em-size character.
However, to assume the one-byte character to be a basis
There is troublesome point a lot when the em-size character is used in full scale.
There is no problem only if it displays it.
(The problem might happen in the compiler made of foreign countries. )
When thinking well though it is this method and there seems seemingly to be no problem
100 yen
%d finger fixed child can use it by combining with other character strings.
When %d finger fixed child is handled to the character string, the part is replaced by the numerical value.
The following program is an example of displaying 100 yen by one time of the printf function.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { Printf ("%d yen \n" 100); return 0; }
%d finger fixed child can use it by freely burying it in the character string in this manner.
100 yen
In the preceding clause, it explained the method of burying %d finger fixed child in the character string.
Let's challenge the display of an easy expression by using this method.
The expression It is a very easy expression called 100+200=300.
Of course, do not calculate yet though it displays by using the numerical value.
If %d finger fixed child is handled, these kind of things are not the winds either.
The following program is an example of displaying 100+200=300.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { printf("%d+",100); printf("%d=",200); printf("%d\n",300); return 0; }
When thinking well though it is this method and there seems seemingly to be no problem
100+200=300
Actually, there is no problem no matter how %d finger fixed child uses it in one character string.
Specify three numerical values when you handle three %d finger fixed children.
The following program is an example of handling and displaying three %d finger fixed children.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { printf("%d+%d=%d\n",100,200,300); return 0; }
In this program, %d finger fixed child sequentially corresponds ahead to the following numerical value by couple 1.
100+200=300