- ..clause 1..: Necessity of memory
- ..clause 2..: Memory of variable
- ..clause 3..: Declaration of variable
- ..clause 4..: Substitution of value for variable
- ..clause 5..: Use the variable instead of the numerical value.
- ..clause 6..: Substitution and the operation at the same time
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).
Though the numerical value was written every time directly as a program in the method to the preceding chapter
At that time, everything was written foolish and honestly even though compared and was suitable of the same numerical value.
Made ahead, and in the program that calculates 10+3, 10-3, 10×3, and 10÷3
The numerical value called 10 and 3 was written many times though was the same.
I want to take the place of In a word, 10 and 3 in this method 20 and 7.
It is necessary to rewrite all numerical values purposely.
Frankly speaking, this is troublesome.
The means to memorize the numerical value is necessary to lose this trouble.
Memorize one frequency value, and if there is a method of taking out the numerical value and use
All numerical values can be changed only by substituting the numerical value to memorize.
The function to achieve the idea in the preceding clause exists in C language. It is a variable.
Though there might be a lot of people who recall mathematics and become unpleasant when hearing the variable, too
The variable of the programming and the variable of mathematics are complete different things.
The variable is to give the name to the memory area to preserve the numerical value.
Though it explains the box where the numerical value is put in this and most introductions
It might be a how to explain in the age when such [noha] and every computer had not been felt.
For the person who is using the computer as such, saying as the memory is refreshing.
The memory of the computer is a structure like the locker hugely displayed in a row in side.
[ Variable ]
Method of management giving name to memory that memorizes numerical value
Put and put out the numerical value based on the number of the locker if usual.
It is not about troublesome [nakotoko] on when a prosy number is used every time every time.
You do not think that it will want to distinguish the locker by the employer identification number for seven digits either.
Then, the name will be given to each locker.
Whether for what it is locker the name is seen if it does so is understood at once.
Moreover, because of saying that it will become very easy to treat
It explained management giving the memory the name in the preceding clause.
In C language, it is called as the declaration of the variable to give the variable the name.
Use the following writing to declare a variable.
Type name variable identifier;The type name is a name that shows the kind of the numerical value to be memorized.
The variable identifier is a name given to the variable as its name suggests.
In how to give this name, there is regulations like the under.
1, normal-width alphabet, normal-width figure, and normal-width _ can be used.Do not think seeing that, this, and somewhere?
The first two characters, the figure cannot be used.
3 and the reserved word decided beforehand cannot be used.
If only this is understood, it is possible to declare a variable.
The following program, variable value of the type named int (integral value) is declared.
The variable can be basically declared only at the head of the function.#include <stdio.h> int main(void) { int value; /* part */ of variable declaration Return 0; }
It makes an error of this program when it is operated.#include <stdio.h> int main(void) { printf("Hello\n"); int value; /* part */ of variable declaration Return 0; }
[ Function of compiler ]
Actually, this program moves in a lot of compilers.
It is because of being able to use it in C++ ([shi-purapura]) of the extended version of C language it.
Moreover, it is possible to use it even with C99 that is a new case of C language decided in recent years.
However, remember if it is not possible to use it by original C language.
The variable declared once can be used freely within the range.
It is possible to use it freely in the main function because it declares in the main function this time.
Because only the main function is used, it will not relate so much for the present.
There are two kinds of usages of the variable. One in that is substitution.
Substitution means the numerical value is memorized in the variable.
Use the following writing to substitute the numerical value for the variable.
[ Generation insertion ]
Memorize the numerical value in the variable.
Variable identifier = numerical value;Never misunderstand it. In this =, the equal sign of mathematics is completely a sign of another meaning.
If only this is understood, the numerical value can be substituted for the variable (Memorize it).
The following program is a variable of int (integral value) type To value Substitute ten.
#include <stdio.h> int main(void) { int value; /* part */ of variable declaration value = 10; /* part */ of substitution Return 0; }
The usage of another ..weirdy.. ..several.. is to use it as taking the place of the numerical value.
It is not because there is especially such writing for this.
It replaces the numerical value that the variable memorizes to write the variable identifier in the expression.
It is possible to apply it by all scenes of the display and the calculation, etc. of the numerical value that have used the expression up to now.
The following program is an example of displaying the value memorized in the variable.
The execution result of this program is as follows.#include <stdio.h> int main(void) { int value; /* part */ of variable declaration value = 10; /* part */ of substitution Printf("%d\n",value); /* part */ of display Return 0; }
If this variable is used, the problem of taking it up at the head of this chapter is solved.
10
The execution result of this program is as follows.#include <stdio.h> int main(void) { int left; int right; left = 10; right = 3; printf("%d\n",left + right); printf("%d\n",left - right); printf("%d\n",left * right); printf("%d\n",left / right); printf("%d\n",left % right); return 0; }
The place where this program is good only rewrites the numerical value substituted for the variable.
13
7
30
3
1
The numerical result of the expression can be substituted directly for the variable.
In the following program, the result of 10+30 is substituted for value.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { int value; value = 10 + 30; printf("%d\n",value); return 0; }
In addition, it is also already possible in the value that the variable remembers to calculate directly.
40
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { int value; value = 10; value += 30; printf("%d\n",value); return 0; }
The point of this program is in the += operator.
40
It is also possible to rewrite the part of this operator as follows.
This writing is considerably a strange expression for the person who is not accustomed to the program.value = value + 30;
As the += operator etc. are unnecessary if you can achieve an increase in the value of the variable by this writing though it is possible to think
+= The description is and there is an advantage of easiness because it is good to write the variable if the operator is used only by one.
The operator with a similar function is prepared in other calculations.
| Operator | Function | |
| += | Substitute addition with the value of the variable for the variable. | |
| -= | Substitute the subtraction with the value of the variable for the variable. | |
| *= | Substitute multiplication with the value of the variable for the variable. | |
| /= | Substitute division with the value of the variable for the variable. | |
| %= | Substitute [yozan] with the value of the variable for the variable. |
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { int value; value = 10; printf("%d\n",value); value++; printf("%d\n",value); value--; printf("%d\n",value); return 0; }
In the programming, as for one increasing the value of the variable, an awful lot and this operator are convenient.
10
11
10