In-Depth Analysis of C Language Variables: What Happens in Memory?

In-Depth Analysis of C Language Variables: What Happens in Memory?

In the last lesson, we briefly introduced the usage of variables, but many students expressed that they still find the question, “What exactly happens in the computer when we define a variable?” a bit “peculiar” and hard to understand. Don’t worry, in this lesson we will delve into this seemingly simple yet very core question—what exactly happens in memory when we define an int type variable in C language?

Before we start today’s hardcore explanation, I remind everyone to ensure sufficient rest while listening to the class! If you enjoy our videos, remember to like, share, and you can also support us by subscribing through the platform..

01

Review of C Language Variable Definition Syntax

First, let’s quickly review the basic syntax for defining variables in C language. I believe everyone is already quite familiar with it:

// Type VariableName = Value;int sunflower = 9673;

Syntax Analysis:int:This is the type (Type), indicating that we want to store an integer.

sunflower: This is the variable name (Identifier), which is the name we give to this block of memory space for easy reference and manipulation later.

=: This is the assignment operator, meaning we assign the value on the right to the variable on the left.

9673: This is the value (Value).

; This is the statement termination symbol, which is essential.

In addition to int (which is called “integer” in Chinese, usually referred to as “整形” or “整数类型”), C language also has other data types, such as the float type used to store decimals:

float wallet_balance = 99.99f; // Note that floating-point assignments often have an f suffix

In this lesson, we will focus on int type, and subsequent lessons will gradually introduce other data types. In the last lesson, we also learned how to use the printf function to output the value of an int type variable:

printf("%d\n", sunflower);

Here, %d is a format specifier that tells the printf function that the next output will be a decimal integer. If you need to output multiple int type variables, you can also use multiple %d and separate the variable names with commas.

02

The “Behind the Scenes” of Variable Definition: How Memory Works

Now, let’s turn our focus to the main event of today: when we write the line of code int sunflower = 9673; what exactly happens in the computer’s “brain”—the memory?

To understand this, we first need to distinguish between two very important concepts: Memory (Memory/RAM) and Disk.

Memory vs. Disk: The Two Giants of Storage

  • Disk (Hard Disk Drive / Solid State Drive): The disk is where data is permanently stored. Movies you download, software you install, documents you save, etc., are all written to the disk. Even when the computer is turned off, this data will not be lost. It is like your personal library; once the books are placed inside, they will remain there unless you actively take them out.

  • Memory (Memory / RAM – Random Access Memory): Memory, also known as “running memory,” is used for temporarily storing data. When you open a program (App), such as a browser, game, or run your C language program, the program’s data and instructions are loaded into memory. The characteristic of memory is that it has a very fast read and write speed, but it is volatile storage—once the computer is turned off or the program is closed, the data in memory will be cleared. It is like your workbench; all the files you are working on are on it, and once you finish or leave work, the workbench is cleared to prepare for the next task or tomorrow’s work.

We can check the current memory usage through the task manager (Windows users can right-click the taskbar, select “Task Manager” -> “Performance” -> “Memory”). When you open an App, memory usage increases; when you close the App, memory usage decreases. The “one-click clean” function on your phone cleans the running memory.

The Computer’s “Language”: Binary 0 and 1

The reason computers can work is that they can understand and execute instructions. These instructions are ultimately converted into the simplest two states: On (1) and Off (0). This is the underlying language of computers— binary.

Imagine the movable type printing performance at the Olympic opening ceremony, where each person represents a “dot”: standing up represents “1”, sitting down represents “0”. The countless combinations of “dots” can form various complex patterns and texts.

The working principle of computer memory is similar. Memory can be imagined as countless tiny “switches” or “cells”, each of which can only store one of the two states, 0 or 1. It is difficult for us humans to directly understand and manipulate these 0s and 1s, so we need programming languages (like C language) as “translators” to convert the instructions we can understand into binary code that computers can understand.

Overview of Computer Storage Units

When understanding memory, it is crucial to know the storage units:

  • Bit: The smallest unit of storage in a computer, which can only be 0 or 1.

  • Byte: 1 Byte = 8 bit. This is the basic unit we use when processing data. One English character typically occupies 1 byte.

  • Kilobyte (KB): 1 KB = 1024 Byte.

  • Megabyte (MB): 1 MB = 1024 KB.

  • Gigabyte (GB): 1 GB = 1024 MB.

  • Terabyte (TB): 1 TB = 1024 GB.

Professional Correction and Supplement: You may notice that apart from one byte being equal to eight bits, the conversion between other units is all 1024. This is because 1024 is 2 to the power of 10, which aligns very well with the binary characteristics of computers. In some scenarios, for convenience, people also often use 1000 as an approximate value, but the standard conversion of computer storage units is always 1024.

03

Int Variable’s Close Encounter with Memory

What exactly happens when we declare int sunflower = 9673;?

  • Allocating Memory Space: When the program runs, it requests a block of memory space from the operating system to store the value of the int type variable sunflower. In most 32-bit systems, an int type usually occupies 4 bytes (Byte), which is 4 * 8 = 32 bits in binary.

  • Allocating Address: The operating system will find a contiguous and sufficiently large space in memory ( 4 bytes), and mark the starting position of this space with a unique “house number”, which is the memory address. You can imagine memory as a huge grid, where each cell has an address.

  • Storing Data: Finally, the decimal number 9673 is converted into binary form (a 32-bit binary number), and these 0s and 1s are stored in the 4 bytes of memory space just allocated. The variable name sunflower becomes the “alias” or “label” for this block of memory space.

So, when you define an int variable, the computer actually “draws” a small cell in memory, gives it a name, and then “writes” the integer value you want to store in the form of 0s and 1s into this small cell. This is the entire process of a variable “settling down” in memory!

04

Summary

Through today’s explanation, we not only reviewed the definition syntax of C language variables but also gained a deeper understanding of the differences between memory and disk, as well as how computers store and process data using binary 0s and 1s. Most importantly, we understood the entire process of allocating space, assigning addresses, and storing data in memory when defining an int type variable.

In-Depth Analysis of C Language Variables: What Happens in Memory?

Leave a Comment