MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Memorize values

Memory Needs
With the previous chapters, you can calculate any complex expression using the four arithmetic operations.
However, it is too low-minded to be satisfied with this.

In the previous chapters, the numbers were written directly as a program each time, but
At that time, even if the same numbers matched, they were all written in a ridiculously honest manner.
In the program we just created that calculates 10+3, 10-3, 10×3, and 10÷3, the
The numbers 10 and 3 are the same, yet they were written many times.

In other words, this way, if you wanted to substitute 20 and 7 for 10 and 3
All the numbers have to be rewritten all the way back to the beginning.

This is a hassle, to say the least.
To eliminate this hassle, we need a means of memorizing numerical values.
If there was a way to memorize a number once and then retrieve it and use it to
All the values can be changed simply by substituting the values to be stored.
Memory called variable
There is a function in the C language that realizes what we considered in the previous section. That is variables.
Many people may not like to hear the word "variable" because it reminds them of mathematics.
Variables in programming are completely different from variables in mathematics.

A variable is a name given to an area of memory for storing numerical values.
This, most introductory books explain as a box to hold numbers, but
This would be a way of explaining things at a time when no one had ever touched a computer.
For those who use computers to a certain extent, it is cleaner to say "memory.

Keywords.
Variables]

How to name and manage memory that stores numerical values


Computer memory is a locker-like structure arranged in huge horizontal rows.
Starting from the end of the locker, each one is numbered.
The numbers handled by the computer are then placed somewhere in those lockers.

Normally, we would put numbers in and out based on that locker number.
It would be a hassle to use long, complicated numbers each and every time.
I am sure that everyone would not want to distinguish lockers by seven-digit employee numbers.

Therefore, each locker should have a name.
This way, by looking at the name, you can quickly see what it is for and
It is also very easy to handle.
Declaration of Variables
Naming and managing memory was explained in the previous section.
In other words, to use a variable, you must give it a name.

In C, naming a variable is called declaring a variable.
To declare a variable, use the following writing style

Declare variables
Type name Variable name;

The type name is the name of the type of number you want to remember.
For now, remember int, meaning integer.

A variable name, as the name implies, is the name you give to a variable.
The naming convention is as follows.

How to name variables
1. Half-width alphabets, half-width numbers, and half-width _ can be used.
2. Numbers cannot be used as the first character.
3、Pre-determined reserved words cannot be used.

Hey, doesn't this look familiar?
Yes, this is exactly the same as how function names are named.

Once you know all this, you can declare variables.
The following program declares a variable value of type int (integer value).

Source Code
 #include <stdio.h>

int main(void)
{
    int value; /* variable declaration part */
    return 0;
}

Variables can basically only be declared at the beginning of a function.
For example, you cannot declare a variable as follows

source code
 #include <stdio.h>

int main(void)
{
    printf("Hello\n");
    int value; /* variable declaration part */
    return 0;
}

This program will cause an error if run.
If you bring the variable declaration part to the top, it will work.

Compiler Functions
Actually, this program will work with many compilers.
That is because it can be used with C++ (C++), an extended version of the C language.
It can also be used with C99, a new standard for the C programming language that was decided upon in recent years.
However, please remember that it cannot be used in the original C language.

Assigning values to variables
Once a variable is declared, it can be used freely within its range.
In this case, since it is declared in the main function, it can be used freely within the main function.
For the time being, however, only the main function will be used, so it does not matter much.

There are two ways to use variables. One of them is assignment.
Assignment means to store a numerical value in a variable.

Keywords.
Substitution

To store a numerical value in a variable.


To assign a number to a variable, use the following writing style

Assign a numerical value to a variable
Variable name = number;

Never make the mistake of misunderstanding. The = is a completely different symbol from the mathematical equal sign.
The = here means that the number on the right side is stored in the variable on the left side.
In other words, think of this = as a substitute for the ← symbol.

Once you know all this, you can assign (store) numbers to variables.
The following program assigns 10 to the variable value of type int (integer value).

source code
 #include <stdio.h>

int main(void)
{
    int value; /* variable declaration part */
    value = 10; /* the assignment part */
    return 0;
}

Using variables in place of numbers
Another use of a variable is as a substitute for a number.
There is no particular way to write this.
When you write the name of a variable in a formula, it is replaced by the number that the variable remembers.
It can be applied to all situations where you have been using mathematical expressions, such as displaying numerical values or performing calculations.
The following program is an example of displaying the value stored in a variable.

source code
 #include <stdio.h>

int main(void)
{
    int value; /* variable declaration part */
    value = 10; /* the assignment part */
    printf("%d\n", value); /* display part */
    return 0;
}

The result of executing this program is as follows

Execution Result
10

This variable solves the problem discussed at the beginning of this chapter.
In other words, if you store a number, say 10 or 3, in a variable, and use that variable to calculate
If you want to change a number, there is only one place to rewrite it.
The following program is an example of using a variable to perform a four arithmetic operations calculation.

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 result of executing this program is as follows

Execution Result
13
7
30
3
1

The nice thing about this program is that all you have to do is rewrite the numbers you assign to the variables.
All the numbers in subsequent calculations are also replaced, so only one change is needed.
In fact, try changing the numerical values assigned to left and right.
Assignments and operations at the same time
Variables can be assigned directly to the results of formula calculations.
In the following program, value is assigned the result 10+30.

source code
 #include <stdio.h>

int main(void)
{
    int value;
    value = 10 + 30;
    printf("%d\n", value);
    return 0;
}

The result of executing this program will be as follows

Execution Result
40

In addition, it is possible to calculate directly on the value already remembered by the variable.
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 result of executing this program will be as follows

Execution Result
40

The point of this program is in the += operator.
This operator has the function of increasing the value of the variable on the left by the number on the right.
Earlier, a value of 30 was added to the already assigned value of 10.

This operator part can also be rewritten as follows

another example
value = value + 30;

This writing style is quite strange to someone unfamiliar with the program.
Because it says that value and value+30 are equal.
However, this can be resolved by remembering that in C, = means ←.
This expression means that value plus 30 is assigned to value.

If this way of writing can be used to increase the value of a variable, it seems unnecessary to use the += operator.
The += operator has the advantage that only one variable needs to be written, making it easier to write.
Operators with similar functionality are available for other calculations.

operator Function
+= Assigns the addition to a variable with the value of the variable
-= Assigns a variable to subtract from the value of the variable
*= Assigns a variable to be multiplied by the value of the variable.
/= Assign the division with the value of the variable to the variable.
%= Assigns the remainder of the variable's value to the variable.

In addition, there are operators dedicated to increasing or decreasing the value of a variable by 1, or decreasing it by 1.
The operator that increases the value of a variable by one is the ++ operator and is called increment.
Conversely, the operator to decrease by one is the -- operator and is called decrement.
The following program is an example of 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 result of executing this program will be as follows

Execution Result
10
11
10

In programming, this operator is often used because it is very common to increase the value of a variable by 1.


About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box