Memorizing numbers.
The Necessity of Memory
With the content covered in the previous chapters, if it involves calculations using the four basic arithmetic operations, you can calculate even the most complex expressions.
But being satisfied with this shows a lack of ambition.
In the previous chapters, numbers were directly written into the program each time.
At that time, even if there were the same numbers, I wrote them all down, completely and honestly.
In the program I just created to calculate 10+3, 10-3, 10×3, and 10÷3,
I was writing the number 10.3 over and over again, even though it's the same number.
Essentially, if you wanted to replace 10 and 3 with 20 and 7,
I have to write out all the numbers.
To be honest, this is a real pain.
To eliminate this hassle, we need a way to memorize the numbers.
If there were a way to memorize a value and then retrieve and use it,
Simply substitute that memorable number and you can change all the numbers.
But being satisfied with this shows a lack of ambition.
In the previous chapters, numbers were directly written into the program each time.
At that time, even if there were the same numbers, I wrote them all down, completely and honestly.
In the program I just created to calculate 10+3, 10-3, 10×3, and 10÷3,
I was writing the number 10.3 over and over again, even though it's the same number.
Essentially, if you wanted to replace 10 and 3 with 20 and 7,
I have to write out all the numbers.
To be honest, this is a real pain.
To eliminate this hassle, we need a way to memorize the numbers.
If there were a way to memorize a value and then retrieve and use it,
Simply substitute that memorable number and you can change all the numbers.
Variables as memory
The functionality to realize what we considered in the previous section exists in C.That is the variable.
Many people may feel annoyed when they hear the word variable, as it often brings back memories of math.
Programming variables and mathematical variables are completely different things.
A variable is assigning a name to a memory location used to store a value.
This is often explained in introductory texts as a place to put numbers, but...
This is probably how people would explain it to someone who's never used a computer.
For someone who uses computers even a little, memory sounds cleaner.
How to name and manage memory for storing numerical values.
Computer memory is like a vast array of lockers arranged in a single row.
They are numbered individually along the edge of the locker.
And the numbers that are handled by a computer reside somewhere within that locker.
Normally, we would enter or extract numbers based on the locker number.
It's incredibly tedious to constantly deal with long, complicated numbers.
I think most of you probably wouldn't want to differentiate lockers by a seven-digit employee ID.
Therefore, we will assign names to each locker.
This way, you can immediately tell what locker something belongs to just by looking at the name.
And it's also said to be incredibly easy to handle.
Many people may feel annoyed when they hear the word variable, as it often brings back memories of math.
Programming variables and mathematical variables are completely different things.
A variable is assigning a name to a memory location used to store a value.
This is often explained in introductory texts as a place to put numbers, but...
This is probably how people would explain it to someone who's never used a computer.
For someone who uses computers even a little, memory sounds cleaner.
Keyword
【variable】
How to name and manage memory for storing numerical values.
Computer memory is like a vast array of lockers arranged in a single row.
They are numbered individually along the edge of the locker.
And the numbers that are handled by a computer reside somewhere within that locker.
Normally, we would enter or extract numbers based on the locker number.
It's incredibly tedious to constantly deal with long, complicated numbers.
I think most of you probably wouldn't want to differentiate lockers by a seven-digit employee ID.
Therefore, we will assign names to each locker.
This way, you can immediately tell what locker something belongs to just by looking at the name.
And it's also said to be incredibly easy to handle.
Variable declaration
As explained in the previous section, naming and managing memory is possible.
In other words, to use a variable, you must give it a name.
In C, the act of assigning a name to a variable is called declaring the variable.
To declare a variable, you can use the following syntax:
Name is a name that describes a type of number you want to remember.
For now, just remember int means integer.
A variable name is, as the name suggests, the name given to a variable.
There are certain rules for naming like this.
Haven't we seen this before?
Yes, this is exactly the same as how to name functions in function names.
Now that you know this much, you can declare variables.
The following program declares a variable named value of type int (integer).
Variable declarations can generally only be done at the beginning of a function.
For example, you cannot declare variables like this.
This program will result in an error when run.
Moving the variable declarations to the beginning will make it work.
In other words, to use a variable, you must give it a name.
In C, the act of assigning a name to a variable is called declaring the variable.
To declare a variable, you can use the following syntax:
Declare variables.
Type name variable name;
Name is a name that describes a type of number you want to remember.
For now, just remember int means integer.
A variable name is, as the name suggests, the name given to a variable.
There are certain rules for naming like this.
Naming Conventions
You can use half-width alphanumeric characters and underscores.
2. Numbers cannot be used as the first character.
3. You are also prohibited from using pre-defined reserved words.
2. Numbers cannot be used as the first character.
3. You are also prohibited from using pre-defined reserved words.
Haven't we seen this before?
Yes, this is exactly the same as how to name functions in function names.
Now that you know this much, you can declare variables.
The following program declares a variable named value of type int (integer).
Source code
#include <stdio.h>
int main(void)
{
int value; /* Variable declaration section */
return 0;
}
Variable declarations can generally only be done at the beginning of a function.
For example, you cannot declare variables like this.
Source code
#include <stdio.h>
int main(void)
{
printf("Hello\n");
int value; /* Variable declaration section */
return 0;
}
This program will result in an error when run.
Moving the variable declarations to the beginning will make it work.
Compiler Features
Actually, this program will run on many compilers.
That's because it's valid in C++, an extension of the C language.
It's also valid in C99, the newer standard for the C language defined in recent years.
However, please remember that it is not valid in the original C language.
That's because it's valid in C++, an extension of the C language.
It's also valid in C99, the newer standard for the C language defined in recent years.
However, please remember that it is not valid in the original C language.
Assigning values to variables
Once a variable is declared, it can be freely used within its scope.
Since it's declared within the main function this time, you can use it freely within the main function.
For now, it's not very relevant as we're only using the main function.
There are two ways to use variables.One of them is assignment.
Assignment means giving a value to a variable.
Storing a number in a variable.
To assign a number to a variable, you use a notation like this:
Please don't misunderstand.This = is a symbol with an entirely different meaning than the equals sign in mathematics.
Here, the '=' sign means assigning the value on the right to the variable on the left.
Just think of this = as a substitute for the ← symbol.
With this much knowledge, you can assign (store) a number to a variable.
The following program assigns the value 10 to an integer variable named value.
Since it's declared within the main function this time, you can use it freely within the main function.
For now, it's not very relevant as we're only using the main function.
There are two ways to use variables.One of them is assignment.
Assignment means giving a value to a variable.
Keyword
【Assignment】
Storing a number in a variable.
To assign a number to a variable, you use a notation like this:
Assign a number to a variable.
variable = number;
Please don't misunderstand.This = is a symbol with an entirely different meaning than the equals sign in mathematics.
Here, the '=' sign means assigning the value on the right to the variable on the left.
Just think of this = as a substitute for the ← symbol.
With this much knowledge, you can assign (store) a number to a variable.
The following program assigns the value 10 to an integer variable named value.
Source code
#include <stdio.h>
int main(void)
{
int value; /* Variable declaration section */
value = 10; /* Substitution part */
return 0;
}
Using variables instead of numbers
Another use for variables is to use them as a substitute for numbers.
There's no particular way to write about this.
When variable names are written in a formula, they are replaced with the numerical values they store.
It can be applied in all situations where formulas have been used, such as displaying numbers and performing calculations.
The following program is an example of displaying a value stored in a variable.
The output of this program will be as follows:
This variable will solve the problem we addressed at the beginning of this chapter.
Essentially, you're storing numbers like 10 and 3 in variables, and then performing calculations using those variables.
If you need to change a number, you only need to modify it in one place.
The following program demonstrates performing arithmetic calculations using variables.
The output of this program will be as follows:
The beauty of this program is that you only need to change the numbers assigned to the variables.
It's because all the numerical values in subsequent calculations will also be replaced, allowing for a single change.
Actually, try changing the numbers assigned to left and right.
There's no particular way to write about this.
When variable names are written in a formula, they are replaced with the numerical values they store.
It can be applied in all situations where formulas have been used, such as displaying numbers and performing calculations.
The following program is an example of displaying a value stored in a variable.
Source code
#include <stdio.h>
int main(void)
{
int value; /* Variable declaration section */
value = 10; /* Substitution part */
printf("%d\n", value); /* Display Section */
return 0;
}
The output of this program will be as follows:
Results
10
This variable will solve the problem we addressed at the beginning of this chapter.
Essentially, you're storing numbers like 10 and 3 in variables, and then performing calculations using those variables.
If you need to change a number, you only need to modify it in one place.
The following program demonstrates performing arithmetic calculations using variables.
Source code
#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 output of this program will be as follows:
Results
13
7
30
3
1
7
30
3
1
The beauty of this program is that you only need to change the numbers assigned to the variables.
It's because all the numerical values in subsequent calculations will also be replaced, allowing for a single change.
Actually, try changing the numbers assigned to left and right.
Simultaneous assignment and operation.
Variables can be directly assigned the result of a calculation.
In the following program, the value will be assigned the result of 10 + 30.
The output of this program is as follows:
Furthermore, calculations can also be performed directly on the values already stored in variables.
The following program is an example of adding 30 to the number stored in the variable value.
The output of this program is as follows:
The key to this program lies in the += operator.
This operator has the functionality of incrementing the value of the variable on the left by the number on the right.
Previously, the value 30 was added to what was already assigned to 10.
This part of the operator can also be rewritten as follows.
This way of writing is quite unusual for people who are not familiar with programming.
Because it says that value and value+30 are equal.
However, the solution lies in remembering that, in C, = represents the meaning of ←.
This equation means adding 30 to the value and then assigning that result back to value.
If this method can achieve incrementing variable values, operators like += might seem unnecessary.
Using the += operator allows you to write the variable only once, which offers the benefit of easier coding.
Operators with similar functionality are also available for other calculations.
Furthermore, there are operators specifically for incrementing a variable by one or decrementing it by one.
The operator to increment a variable's value by one is the ++ operator, which is called an increment.
Conversely, the decrement operator is denoted by --, and is called the decrement operator.
The following program demonstrates examples using increment and decrement.
The output of this program is as follows:
In programming, incrementing a variable's value is so common that this operator is frequently used.
In the following program, the value will be assigned the result of 10 + 30.
Source code
#include <stdio.h>
int main(void)
{
int value;
value = 10 + 30;
printf("%d\n", value);
return 0;
}
The output of this program is as follows:
Results
40
Furthermore, calculations can also be performed directly on the values already stored in variables.
The following program is an example of adding 30 to the number stored in the variable value.
Source code
#include <stdio.h>
int main(void)
{
int value;
value = 10;
value += 30;
printf("%d\n", value);
return 0;
}
The output of this program is as follows:
Results
40
The key to this program lies in the += operator.
This operator has the functionality of incrementing the value of the variable on the left by the number on the right.
Previously, the value 30 was added to what was already assigned to 10.
This part of the operator can also be rewritten as follows.
Alternatively
value = value + 30;
This way of writing is quite unusual for people who are not familiar with programming.
Because it says that value and value+30 are equal.
However, the solution lies in remembering that, in C, = represents the meaning of ←.
This equation means adding 30 to the value and then assigning that result back to value.
If this method can achieve incrementing variable values, operators like += might seem unnecessary.
Using the += operator allows you to write the variable only once, which offers the benefit of easier coding.
Operators with similar functionality are also available for other calculations.
| Operator | Function |
|---|---|
| += | Assigning the result of adding a variable's value to the variable |
| -= | Assigning the result of subtracting a variable's value to another variable |
| *= | Assigning the result of multiplying a variable's value to another variable |
| /= | Assigning the result of division by a variable's value to a variable |
| %= | Assign the remainder of the division by the variable value to the variable |
Furthermore, there are operators specifically for incrementing a variable by one or decrementing it by one.
The operator to increment a variable's value by one is the ++ operator, which is called an increment.
Conversely, the decrement operator is denoted by --, and is called the decrement operator.
The following program demonstrates examples using increment and decrement.
Source code
#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;
}
The output of this program is as follows:
Results
10
11
10
11
10
In programming, incrementing a variable's value is so common that this operator is frequently used.
About This Site
Learning C language through suffering (Kushi C) isThis 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.




