learn through suffering C language learn through suffering 
C language

Variables and Memory

Variables exist in memory.

In Chapter 5, when explaining variables, we stated that variables are created in memory.
However, they didn't explain the specific details at all.
It was because you didn't need to know that much if you were just dealing with variables.

However, in order to understand the concept of pointers that we will be dealing with shortly,
You need to understand how variables are stored in memory.

As explained in the previous section, computer memory is like a giant, single-row locker.
And each of these lockers remembers a state of on or off.
We refer to a single locker as one bit, and a group of eight lockers as one byte.
Memory locations are numbered and distinguished on a byte-by-byte basis.

Variables declared in a program are also stored in this memory with assigned numbers.
It's just that it would be difficult to distinguish them just by number, so we're giving them names.
When compiled into an executable, variable names are converted to numbers.

In short, all variables are created in memory.
First, please keep in mind that they are being distinguished by numbers.

C is a programming language like this.
From here on, this chapter frequently delves into the substance of the C language. Many aspects not covered in previous chapters will be clarified here. This includes numerous critiques of C's syntax and other aspects.

Fundamentally, C was a programming language created by a man named Dennis Ritchie
solely for his own team's use,
never imagining it would be used worldwide.
Consequently, aspects tailored to the creator's convenience are evident,
resulting in a language with unfriendly syntax for ordinary users.

Yet despite this, C is used worldwide.
While it has its puzzling aspects, it remains the most versatile language,
widely employed for everything from small components to game programs.

Display the memory address.

In the preceding section, we explained that all variables are created in memory and distinguished by their assigned numbers.
Actually, the numbers assigned to variables can be checked within the program.

Finding the number is surprisingly easy; you just need to use the %p specifier with the printf function.
However, you must prepend an ampersand (&) before variable names.
The following program is an example that displays the number of the int variable i.

Source code
#include <stdio.h>

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

The output of this program might be as follows.

Results
0012FF80

These are the results obtained in the author’s environment, although the numerical values may vary.
It depends on the computer and compiler being used.

Which memory address is assigned to a given variable.
which is automatically determined by the compiler (or, more accurately, the linker),
Furthermore, this value can also change due to the operating system's virtual memory functionality.

This number is in hexadecimal notation.It is 1245056 in decimal.
That's because it's displayed in a hard-to-understand format like hexadecimal.
As explained in the preceding paragraph, computer numbers are stored in binary.
In binary, the number of digits increases by powers of two (2, 4, 8, 16, and so on).
In base-10, the number of digits increases as you move along (e.g., 10, 100, 1000, and so on).
In hexadecimal, the number of digits increases with 16, 256, 4096, and so on.
As you may notice, decimal and binary don't mesh well.
Hexadecimal is convenient because it works well with binary.

Why do computers use binary?
Suddenly, but how high can you count using all ten fingers on both hands?
Usually up to ten, but theoretically, you can actually count up to 1024.
You just need to assign each finger to one digit in the binary number system.
In this way, counting in binary rather than decimal allows for
smaller circuitry.

However, here, hexadecimal representations aren't particularly important.
The key is that the assigned numbers for the variables are easily accessible.
The number assigned to this variable is called an address.

Keyword
【address】

The numerical address in memory assigned to a variable.

You can think of it as having the exact same meaning as a URL address.
The number that indicates the location of a variable is what we call an address.

Numbered variables

In the previous section, we only examined one variable, but this time we will examine the addresses of multiple variables.
Just to reiterate, what we're calling the locker number assigned to a variable, we're referring to as the address.
The following program is an example of displaying the addresses of three integer 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 output of this program might be as follows.

Results
i1(0012FF78) 1245048 in decimal
i2(0012FF7C) 1245052 in decimal
i3(0012FF80) 1245056 in decimal

This is an example run in the author's environment.The numbers vary depending on the environment.
It appears that the sequential numbers are assigned with an offset of 4 to int variables.
It's simply because the size of an int is 4 bytes.

Since 4 bytes is 4x8=32, it's 32 bits, or 32 digits in binary.
It can store numbers from 0 to 4294967295 (approximately 4.3 billion) when negative values are not included.
In the case of 2 bytes (16 bits), the range is from 0 to 65535.

Variables of types other than int also have different sizes depending on the compiler.
However, a char data type is always exactly 1 byte.
to store character codes within the range of 0 to 255,
That's because you need to make sure it's always up to 255, or you'll run into problems.
It seems that in the old days, some PCs even had 7-bit characters...

This time they are consecutive, but the addresses of variables do not always have to be consecutive.
Because these three variables are used separately.
It doesn't matter if we're in different places.
Some compilers may produce reversed order.

Array indices

Like variable numbers, arrays can also display addresses.
Tedious as it may be, an address is the locker number assigned to a variable.
For arrays, you don't need to add & at the beginning.
However, since individual array elements are treated like variables, it naturally requires the & operator.
The following program is an example that displays 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 output of this program might be as follows.

Results
array___(0012FF5C) Decimal: 1245020
array<0>(0012FF5C) Decimal: 1245020
array<1> (0012FF60) In decimal: 1245024
array<2>(0012FF64) In decimal: 1245028

It appears that arrays are also allocated in consecutive blocks of 4 bytes.
Furthermore, it seems that when an array name is specified, it resolves to the index of the array's first element.

In fact, all the tricks of the array are revealed here.
In fact, the array name represented the address of the array's first element.
When referring to each element, I labeled them with numbers like [0], [1]. This means...
This means to refer to the memory address of array name + element number.

Essentially, if you establish an initial address, you can increment it by adding a number to it.
It allows you to express a state where numerous variables are lined up.

However, what's a bit peculiar here is that the actual address increases by 4 when the element number is 1.
This mystery should be solvable immediately if you remember the size of an int.

In a 32-bit compiler, the size of an int is 4 bytes.
In essence, the address of the next integer variable is the initial address plus 4.
Similarly, the address of the int variable two positions ahead is the initial address + 8.

We will explain the workings of this array in more detail later.


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. line break
  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

Loading comment system...