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

Relationship between variables and memory

Variables exist in memory
When explaining variables in Chapter 5, we explained that variables are created in memory.
However, we have not explained its specifics at all.
This is because if you are just dealing with variables, you did not need to know that much.

But to understand the concept of pointers, which we'll deal with in a moment.
You need to know how variables are stored in memory.

As explained in the previous section, computer memory is structured like a giant single-column locker.
And each of these lockers stores two states: on and off.
We call one of these lockers a bit, and all eight together a byte.
In memory, each of these bytes is numbered and distinguished.

Variables declared in a program are also stored in this memory with a number.
However, it is difficult to distinguish them by number, so they are given names.
When compiled into an executable file, variable names are converted to numbers.

In short, all variables are created in memory and
Keep in mind first that there, they are numbered and distinguished.

C is such a language
Thereafter, this chapter contains many developments that bring us closer to the reality of the C language.
Many things that have not been explained in the previous chapters are revealed in this chapter.
In this chapter, there are many developments that criticize the C language syntax and other aspects of the language.

To begin with, the C programming language was developed by a man named Dennis Ritchie in the early days of the C programming language.
It is a programming language that we created for our own use only.
They had no idea that it would be used all over the world.
Therefore, it was seen as a convenience to those who created it.
It is a language with a syntax that is unfriendly to ordinary people.

Nevertheless, the C programming language is used throughout the world.
It is the most versatile language, even if it is a bit baffling in places.
They are used in a wide range of applications, from small components to game programs.

Display numbers in memory
In the previous section, we explained that all variables are created in memory and are numbered to distinguish them.
In fact, the numbers assigned to variables can be looked up programmatically.

Finding out the number is surprisingly easy, just use the %p specifier in the printf function.
However, the variable name must be preceded by &.
The following program is an example of displaying the number of an int variable i.

source code
 #include <stdio.h>

int main(void)
{
    int i;
    printf("%p\n", &i);
    return 0;
}

The result of executing this program might look like this

Execution Result
0012FF80

This was run in the author's environment, and the resulting numbers are
It depends on the computer and the compiler used.

The allocation of a given variable to any number of memory locations is determined by the
The compiler (or linker, to be precise) automatically decides, and the
In addition, its value also varies depending on the virtual memory function of the OS.

The number is in hexadecimal, which is 1245056 in decimal.
Why is it displayed in such a confusing way as hexadecimal numbers, you ask?
As explained in the previous section, computer numbers are stored in binary.
In binary numbers, the digits are 2, 4, 8, 16, ・・・・, and so on.
In decimal numbers, the digits go up to 10, 100, 1000, and so on.
In hexadecimal, the digits are 16, 256, 4096, and so on.

As you may notice from this, decimal and binary numbers do not mesh well.
Hexadecimal numbers are convenient because they mesh well with binary numbers.

Why are computers binary?
Suddenly, how many numbers can you count with 10 fingers of both hands?
Usually up to 10, but actually, theoretically, you can count up to 1024.
All you have to do is count one finger corresponding to one binary digit.
In this way, counting in binary rather than decimal
The circuit can be made smaller.

Nevertheless, hexadecimal numbers are not so important here.
What is important is that the numbers attached to variables can be easily examined.
This number assigned to a variable is called an address.

Keywords.
[address].

A number attached to a variable in memory.


It is safe to assume that this means exactly the same thing as an address, such as a URL address.
The number indicating the address of the variable, i.e., the address.
Multiple variable numbers
In the previous section, we examined only one, so this time we will examine the addresses of multiple variables.
Again, the locker number attached to a variable is called its address.
The following program is an example that displays the addresses of three int type variables.

source code
 #include <stdio.h>

int main(void)
{
    int i1, i2, i3;
    printf("i1(%p)\n", &i1);
    printf("i2(%p)\n", &i2);
    printf("i3(%p)\n", &i3);
    return 0;
}

The result of executing this program might look like this

Execution Result
i1(0012FF78) 1245048 in decimal
i2(0012FF7C) in decimal: 1245052
i3(0012FF80) in decimal: 1245056

This is an example run in the author's environment. The values vary depending on the environment.
It appears that int type variables are assigned sequential numbers off by 4.
This is only because the size of an int type is 4 bytes.

Four bytes is 4x8=32, so 32 bits, or 32 binary digits.
Not including the minus sign, it can store numbers from 0 to 4294967295 (about 4.3 billion).
For 2 bytes (16 bits), the range is from 0 to 65535.

Variables other than int type also have different sizes for each compiler.
However, only the char type is always 1 byte.
To keep the character numbers in the range 0-255 in the char type, the
This is because it is inconvenient to have up to 255 without fail.
It is said that there were computers in ancient times in which char was 7 bits.

In this case, the addresses of variables are now sequentially numbered, but variable addresses are not always sequentially numbered.
Because these three variables are variables that are used separately.
This is because there is nothing wrong with being in different places.
Depending on the compiler, the order may be reversed.
Sequence Number
Like variable numbers, arrays can also display addresses.
As a reminder, an address is a locker number attached to a variable.
In the case of arrays, there is no need to prefix them with &.
However, individual elements of an array are treated the same as variables, so they are naturally marked with &.
The following program is an example of displaying an array and the addresses of its elements.

source code
 #include <stdio.h>

int main(void)
{
    int array[10];
    printf("array___(%p)\n", array);
    printf("array[0](%p)\n", &array[0]);
    printf("array[1](%p)\n", &array[1]);
    printf("array[2](%p)\n", &array[2]);
    return 0;
}

The result of executing this program might look like this

Execution Result
array___(0012FF5C) 1245020 in decimal
array <0>(0012FF5C) in decimal: 1245020
array <1>(0012FF60) in decimal: 1245024
array <2>(0012FF64) in decimal: 1245028

Apparently, arrays are also assigned sequential numbers of 4 bytes each.
Furthermore, if an array name is specified, it appears to be the same number as the first element in the array.

In fact, this is where all of the array tr icks are exposed.
In fact, the array name represented the address of the first element of the array.
When we referred to each element, we gave it an element number, say [0],[1], which means
It means to refer to the memory of the address of the array name + element number.

In other words, if you determine the initial address, you can add the numbers to it to get
It is possible to represent the state of many variables in a row.

However, what is a little strange here is that when element number 1 is used, the actual address is increased by 4.
This mystery can be quickly solved if you recall the size of the int type.

In 32-bit compilers, the size of an int type is 4 bytes.
In other words, the address of the variable of type int one ahead is the first address plus 4.
Similarly, the address of the variable of type int two ahead is the first address plus 8.

We will explain this array mechanism in more detail later.


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.