※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Display of numerical value   

  1. ..clause 1..: Character string and numerical value
  2. ..clause 2..: Display the numerical value.
  3. ..clause 3..: Combination display with character string
  4. ..clause 4..: Display of two or more numerical values

[1] Character string and numerical value

In the preceding chapter, it explained the method of displaying the character string on the screen.
However, it is likely to embarrass it in being able to display only 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.


[ 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.
In C language, the character string and the numerical value are clearly distinguished depending on writing.
In C language, the character string means while it was enclosed with ""
Everything becomes a character string if enclosed with ""if it says oppositely.

Recall the program made in the preceding chapter.

 
#include <stdio.h> 

int main(void)  
{
	printf("Hello, world\n");  
	return 0;
}
The printf function was a function that displayed the character string.
Therefore, it is necessary to pass the character string to the printf function.
Hello and world\n were enclosed with ""

The character string enclosed with ""might be especially called a string literal in this manner.


[   Character row literal   ]
Hollerith constant enclosed with ""of burial while programming it.
In C language, special writing need not be used when you write the numerical value.
However, if the figure is arranged and written, it is treated as a numerical value.
However, it is treated as a character string when enclosing it with ""even when the figure is arranged.
In a word, it is treated like the following example.
1234 is a numerical value.
"1234" is a figure. (character string)
Being able to use to calculate is only a numerical value though repeated.
The figure cannot be used to calculate.

It returns to contents.


[2] Display the numerical value.

The printf function is a function that displays the character string to the last.
However, the function to display the numerical value thankfully is had.

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.


[   Output conversion finger fixed child   ]
When it wants to convert outside data into character string and display
Sign that specifies the conversion regime.
Being used best : though there are various kinds of output conversion finger fixed children.
It is %d finger fixed child who converts the integral value into the character string.
The result of converting the numerical value specified back into the figure is displayed in this finger fixed child part.

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.

 
#include <stdio.h> 

int main(void)  
{
	printf("%d",100);  
	return 0;
}
The execution result of this program becomes as follows.

100
Here, because %d is specified in the character string passed to the printf function
The part of %d would be specified later It was replaced by the numerical value of 100 and it was displayed.

It returns to contents.


[3] Combination display with character string

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; 
}

[ 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. )
The execution result of this program becomes as follows.

100 yen
When thinking well though it is this method and there seems seemingly to be no problem
The printf function is used ,divided into purposely two portions to display one line.
Finishing the printf function by one time to display one line becomes easy.

%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.

 
#include <stdio.h> 

int main(void) 
{
	Printf ("%d yen \n"  100);
	return 0; 
}
The execution result of this program becomes as follows.

100 yen
%d finger fixed child can use it by freely burying it in the character string in this manner.

It returns to contents.


[4] Display of two or more numerical values

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.

 
#include <stdio.h> 

int main(void)  
{
	printf("%d+",100);  
	printf("%d=",200);  
	printf("%d\n",300);  
	return 0;
}
The execution result of this program becomes as follows.

100+200=300
When thinking well though it is this method and there seems seemingly to be no problem
The printf function is used ,divided into purposely three portions to display one line.
Finishing the printf function by one time to display one line becomes easy.

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.

 
#include <stdio.h> 

int main(void)  
{
	printf("%d+%d=%d\n",100,200,300);  
	return 0;
}
The execution result of this program becomes as follows.

100+200=300
In this program, %d finger fixed child sequentially corresponds ahead to the following numerical value by couple 1.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.