MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Memorizing numbers.

The Necessity of Memory
With the content covered in the previous chapter, if it involves calculations using the four basic arithmetic operations, you can compute even the most complex expressions.
But settling for 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.

キーワード
【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:

Declare variables.
Type 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."

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の部minute */
    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の部minute */
    return 0;
}

This program will result in an error when run.
Moving the variable declarations to the beginning will make it work.

Compiler Features
実は、このプログラムは多くのCompilerでは動いてしまいます。
それは、C languageの拡張版である、C++(シープラプラ)では使えるからis.
また、近year決められたC languageの新規格であるC99でも使えます。
しかし、元々の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.

キーワード
【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の部minute */
    value = 10; /* Assignmentの部minute */
    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.

Source code
#include <stdio.h>

int main(void)
{
    int value;             /* Variable declarationの部minute */
    value = 10;            /* Assignmentの部minute */
    printf("%d\n", value); /* 表示の部minute */
    return 0;
}

The output of this program will be as follows:

Execution 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:

Execution results
13
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.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution 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 results of this program's execution are as follows:

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

演算子 機能
+= variableの値との加算をvariableにAssignment
-= variableの値との減算をvariableにAssignment
*= variableの値との乗算をvariableにAssignment
/= variableの値との除算をvariableにAssignment
%= variableの値との余算をvariableにAssignment

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 results of this program's execution are as follows:

Execution results
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) is
This 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.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19