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


  Function for input   

  1. ..clause 1..: Necessity of input
  2. ..clause 2..: Scanf function
  3. ..clause 3..: Input of numerical value
  4. ..clause 4..: Two or more input
  5. ..clause 5..: Simple sigma program

[1] Necessity of input

If it is a calculation by the content to the preceding chapter that uses the arithmetic operation, any complex expression is computable.
However, satisfying it with this is too low the will (aim).

In the method to the preceding chapter, the numerical value was written directly in the program.
In a word, when the program is executed, the numerical value will already have been decided.
In a word, only every time the same calculation can be done in the program.

With this, it is necessary to program when it wants to calculate by another numerical value and it becomes it.
In these kind of things, it changes almost with the use of a high-level calculator and it doesn't exist.
It is not an exaggeration to say that there is no meaning to which the programming is done at all.

The method of breaking down this problem is to input the numerical value from the keyboard.
If the numerical value is input whenever the program is executed, it becomes more convenient.

It returns to contents.


[2] Scanf function

As you used the printf function when it displays it on the screen
Use the function for that when you input it from the keyboard.
Scanf ([sukyan;efu]) function is prepared in C language for that.
Use the scanf function as follows.


scanf("入力変換指定子",&変数名);
An input conversion finger fixed child is a character that shows into what numerical value to convert the input figure.
It is possible to use it almost similarly to an output conversion finger fixed child who used it by the printf function.

Specify the variable identifier to which the input data is memorized for the variable identifier.
When the scanf function is used, it is necessary to apply the first [nino] character of the variable identifier.
It explains [kononi] in detail in Chapter 15 though there is a meaning that informs them of the place of the variable.

If this function is executed, the program becomes an input waiting state.
The program when the user inputs data and return (Enter) key is pushed
It is substituted for the variable that the data specified.

# include < stdio.h > is necessary to use the scanf function.
It is not necessary to add it again because it is the same as time that uses the printf function.

It returns to contents.


[3] Input of numerical value

If only this is understood, data can be input from the keyboard.
The following program is an example of displaying the numerical value input by using the scanf function as it is.

 
#include <stdio.h> 

int main(void)  
{
	int data;  
	scanf("%d",&data); /* input part */ 
	Printf("%d\n",data);  
	return 0;
}
The example of the execution result of this program becomes as follows.
The character string weakly displayed is not actually displayed by the sentence added for the explanation.

100   Input data
100   Displayed data
If this program is executed, it becomes an input waiting state.
Then, the numerical value is displayed as it is when the numerical value is input, and it presses return.
100 naturally Do not care about the numerical value other than either.

In the scanf function, not only the integer but also the input of the real number is possible.
However, it is necessary to use %lf finger fixed child for the real number.
Warn %f finger fixed child in the printf function it.
The following program is an example of displaying the real number input by using the scanf function as it is.

 
#include <stdio.h> 

int main(void)  
{
	double data;  
	scanf("%lf",&data); /* input part */ 
	Printf("%f\n",data);  
	return 0;
}
The example of the execution result of this program becomes as follows.
The character string weakly displayed is not actually displayed by the sentence added for the explanation.

175.128   Input data
175.128000   Displayed data

[4] Two or more input

In the scanf function, it is not because only one data a degree can be input.
Two or more input can be done to one degree by employing two or more finger fixed children.
Input two numerical values by using the scanf function and display the following program.

 
#include <stdio.h> 

int main(void)  
{
	int data1,data2;  
	scanf("%d%d",&data1,&data2); /* input part */ 
	Printf("%d , %d\n",data1,data2);  
	return 0;
}
The example of the execution result of this program becomes as follows.

100 200   Input data
200   displayed data)
Here, two numerical values a degree are input specifying two %d finger fixed children.
Delimit two numerical values by space, the tab, and changing line when inputting it and input it.

By the way, int data1 and data2 in this program;、Making
When two or more variables are declared in the same type, it is convenient writing.
int data1; int data2;、..declare solving ,divided into two portions..
Having brought it together in the line becomes easy.

It is also possible to make them input two numerical values by a separate type.
For instance, when you give a finger fixed child who says, "%d%lf"
Eyes can input the integer and the 2nd by one by the real number value.

Moreover, it is also possible to make them input it specifying the delimited sign.
In this case, specify the delimited sign between two finger fixed children.
Delimit by the following program and input two numerical values.

 
#include <stdio.h> 

int main(void)  
{
	int data1,data2;  
	scanf("%d,%d",&data1,&data2); /* input part */ 
	Printf("%d , %d\n",data1,data2);  
	return 0;
}
The example of the execution result of this program becomes as follows.

100,200   Input data
200   Displayed data
This time, delimit when inputting it and input it.
It is necessary to note it because it becomes impossible to delimit in space etc.

It returns to contents.


[5] Simple sigma program

Here, I want to make not the explanation of a new matter but practicing program.
I will assume the title to be a simple sigma calculation.

It is not basically difficult so much to say the sigma though learns by mathematics of the high school.
Calculating is a sigma as for 1+2+3+4+5+・・・・100 etc.
Will request the total value of the integer between the maximum values from minimum value here to keep simple.

It explains official before it programs.
The total value is requested by (min+max) × (max-min+1) ÷2.

First of all, it is understood that the input of two integral values is necessary in this program.
And, it is necessary to calculate it, and to display the result.
Naturally, these are all feasible only in the knowledge that has been studied up to now.

Though the scanf function is used naturally for the input of two numerical values
Because the explanation is not displayed in the scanf function at all before it inputs it
Will display the explanation beforehand by using the printf function.

Only strike the program now if you think here.
The following program displays the total value of the integer between the maximum values from minimum value requesting it.

 
#include <stdio.h> 

int main(void)  
{
	int min,max,sum;  
	
	/* input part */ Printf ("
	Delimit minimum value and the maximum value and input it");  scanf("%
	d , %d",&min,&max);  
	
	/* calculation part */ 
	sum =  (min + max) *  (max - min + 1 )/ 2;  
	
	/* display part */ Printf 
	("The total of %d-%d is %d.  \n" min, max, and sum);  return 0;
}
The example of the execution result of this program becomes as follows.

Delimit minimum value and the maximum value and input it. :100,200
100-200, Total.
Because a lot of hints of a good program are included here, it explains in order.

First of all, pay attention to the execution result.
Another who is displaying it. clearly , ..taking.. sign ..district.. switched off delimit it
It does plainly by displaying the value within the range that was able to be input by even displaying the result.

In the program Three variables of min, max, and sum are declared.
It is an English word with the meaning of the minimum, the maximum, and the total respectively.
It is comprehensible ..use.. ..variable [nanoga].. in what.

I think that about 3-8 characters are suitable in a comprehensible name of the variable identifier.
Because it need not stick to the meaning of English If it is an example of sum
It is another total Total.
It becomes a meaning of answering answer solve Solution etc. are thought.
In the Roman alphabet I think that putting away as goukei kotae is a good idea.
Avoid not significant variable identifier of what and the variable identifier etc. of one character.

Pay attention to applying the comment by appropriately dividing another one and the program.
This program is divided into three stages (the input, the calculation, and the display), and the comment has adhered.
Though it is also possible to put away the calculation and the display in a lot among these
Here, because dividing dares to become comprehensible, it divides.


[ Evil of   comment   ]
Because it is written that the comment is put in a lot of introductions
The comment might be extremely put according to the person.
However, the comment is unnecessary in an understanding easy part if it sees.
Moreover, in case of as it is the comment when you correct the program
Because the comment is not corresponding to the program, it becomes the origin of the misunderstanding.
The author is putting the comment on the delimitation like this example.
Making plainly is important also in the person who makes the program and the user in this manner.

It returns to contents.


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