learn_C_deep_1 (Supplementary knowledge of C programs, declaration and definition of variables, difference between declaration and definition)

Table of contents

C program supplementary knowledge

Variable declaration and definition

1. What is a variable? 

2. What is the nature of variables? - All variables must open up space somewhere in memory

3. The difference between variable definition and declaration form, initialization and assignment

4. Why define variables

Difference Between Declaration and Definition


C program supplementary knowledge

        Let us first look at a piece of C language code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<windows.h>
int main()
{
	printf("hello world!\n");
	system("pause");
	return 0;
}

I believe that the code written by those who have C language foundation for the first time is hello world. This program is also very simple. The result of running is to output a sentence of hello world on the terminal, but have we really understood what is behind this code? What about knowledge? How does it work, and which part of the computer is loaded into it when it runs? Next, let's find out.

        In our opinion, this program is first of all a piece of code in text form . When we run it in the integrated development environment, it forms an executable binary file, that is, an executable program. We can find that we have a Debug file in the file where we created the code, click in and we will find an application .exe (as shown in the figure below), which is our executable program. It is similar to our QQ and WeChat, which can be run by double-clicking.

 We all know that applications need to be double-clicked to open, but have we ever thought about why our programs need to be double-clicked?

Double-clicking an application is an interaction between the user and the computer, and is essentially a function of the operating system. When a user double-clicks an application icon, the operating system looks for the program's executable file , executes it, loads the program into memory , and starts running. 

From this we can conclude that any program must be loaded into memory before execution

Here I ask two questions:

        1. Where is the program stored before it is loaded into memory? -hard  disk

Programs are usually stored on a computer's hard disk or other storage media before being loaded into memory. Specifically, program files are usually stored in the form of binary files or executable files, including information such as compiled codes and data.

When the user runs the program, the operating system will find the program file from the hard disk or other storage media, load it into the memory, and then set the pointer of the CPU as the entry point of the program to make it start to execute. While the program is running, the CPU is constantly reading instructions and data from memory, processing them, and writing the results back to memory.

It should be noted that the operating system does not directly load the entire program file into the memory, but loads the program in segments, dividing the program into multiple segments (such as code segment, data segment, stack segment, etc.), and then one by one. These segments are loaded into memory. This can improve memory utilization and allow programs to use memory more flexibly.

        2. Why must the program be loaded into memory? fast access

When a computer executes a program, program code and data need to be loaded into memory before the computer can execute those program operations. This is because memory has a faster read speed than hard disks, so loading programs into memory allows the computer to execute programs faster.

In addition, the program needs to constantly access and modify the data stored in memory when it is running, and if the program is not loaded into memory, it cannot perform these operations.

Therefore, loading a program into memory is a necessary prerequisite for the program to run on the computer. When a program is loaded into the memory, the operating system will allocate a certain amount of memory space for the program, and allocate memory space for all library files and system resources required by the program, so as to ensure that the program can access these resources when it is running. When the program finishes running, the operating system will release these memory spaces for other programs to use.

Here we need to understand the von Neumann system, first look at a picture

Input device: refers to various devices used to input external data into the computer system. Common input devices include: mouse, keyboard, touch screen, scanner, digital camera, microphone, tablet, etc. These devices can input different forms of data into the computer, such as text, images, audio, video, etc.

Memory: A hardware device used in a computer system to store data and instructions. It can be divided into two types: primary memory (also known as random access memory or RAM) and secondary memory (also known as secondary memory or external memory).

Main memory is usually the most important type of memory in a computer system and is used to store currently executing programs and data. It consists of a set of chips that can access any location within it at any time. The capacity of main memory is usually measured in bytes and expressed in megabytes (MB) or gigabytes (GB).

Auxiliary storage is usually a removable storage device in a computer system, such as a hard disk, optical disk, U disk, magnetic tape, and the like. They are usually larger than main memory, but use them relatively slowly. Data from secondary memory can be read into primary memory for processing when needed.

Arithmetic unit: It is one of the main components of a computer, also known as the arithmetic logic unit (ALU). It is a hardware device specially designed to perform arithmetic and logic operations. An arithmetic unit usually includes multiple arithmetic logic units (ALUs) for performing various arithmetic operations, such as addition, subtraction, multiplication, division, etc., and logic operations, such as AND, OR, NOT, XOR, etc.

Controller: An important part of the computer, it is an electronic circuit responsible for coordinating the operation of various parts of the computer and controlling the operation and processing of the computer.

The controller mainly includes two parts: instruction register and instruction decoder . The instruction register is used to store the instruction currently being executed by the CPU, and the instruction decoder is used to parse the instruction and convert it into an operation code that the CPU can execute. When the CPU needs to execute an instruction, it reads it from main memory and stores it in the instruction register. Then, the instruction decoder will analyze the instruction, convert it into a series of operation codes, and send control signals to each component to coordinate the operation of each component to complete the execution process of the instruction. In addition, the controller is also responsible for processing various interrupt signals, such as input and output requests from external devices, hardware failures, etc., to ensure the stable operation of the computer.

Output device: It is a device used by the computer system to display calculation results, output information and data to the user. It can output the data and results processed by the computer in the form of visualization or sound. Common output devices include: monitors, printers, speakers or speakers, projectors, etc.

        When we input data from the input device (keyboard), and then write the code, the code file will be compiled by the compiler to generate an executable file, and the data will first be put into the memory (hard disk). When we double-click the executable file , the operating system loads the file into memory. When loading, the operating system will allocate a section of memory space to the program, and assign a virtual memory address to the code file. When the CPU executes the program, it will read the program code and data from the memory, and execute instructions inside the CPU. When the CPU executes an instruction, it stores the result in memory, or outputs the result to an output device such as a monitor or a printer. When the program is executed, the operating system will release the memory space allocated to the program and unload the program from the memory. This is the entire program execution process of our hello world!

Variable declaration and definition

There are still a few questions here:

1. What is a variable? 

In computer programming, a variable refers to a memory location represented by a name , which is used to store data needed by the program at runtime. Variables can store various types of data such as numbers, strings, booleans, etc.

The name of the variable is defined by the programmer and optionally specifies the data type of the variable. The data type of a variable determines the type of data that the variable can store and the size of the memory space it occupies.

For example, variables of type Integer can store integer values, while variables of type String can store string values.

During the running of the program, the program can continuously assign values ​​to variables and use the values ​​of variables for calculation, comparison or other operations. The value of a variable can change, but its memory location does not.

Therefore, the program can store and access various data through variables, and realize the logic of the program through data changes. In short, a variable is a mechanism for storing data needed by a program at runtime. It can store various types of data, and access and manipulate data in the program through its name and data type.

2. What is the nature of variables? - All variables must open up space somewhere in memory

Inside a computer, a variable is essentially a memory space used to store data . Through variables, we can give this memory space a name, which is convenient for us to refer to and modify the data in this memory in the program. 

3. The difference between variable definition and declaration form, initialization and assignment

type variableName = default; --define

type variable name; -declaration

int a;//声明

double d = 20.0;//定义

char c = 'c';//初始化

c = 'a';//赋值

Variable initialization refers to assigning an initial value to a variable when defining it . In most programming languages, the initial value of a variable is some specific value by default, for example, the initial value of a numeric variable is 0, and the initial value of a Boolean variable is false. If an initial value is explicitly assigned when the variable is defined, we call it variable initialization.

Variable assignment refers to reassigning a value to an existing variable during program execution . We can assign a new value to a variable, or assign the value of a variable to another variable, thereby updating the value of the variable.

In general, variable initialization is to assign an initial value to a variable when it is defined, and variable assignment is to change the value of an existing variable during program execution.

4. Why define variables

The main purpose of defining variables is to store and process data in the program . In many computer programs, we need to store and manipulate various types of data, such as numbers, strings, booleans, etc. Without variables, we would not be able to store and reference this data in our programs.

By defining variables, we can assign a name or identifier to the data in the program. This identifier can represent a specific value, and this value can be updated and modified by the program at any time. In this way, we can use these data flexibly in the program to update and process the data according to different situations. At the same time, using variables can also improve the readability and maintainability of the program, making the code easier to understand and modify.

Defining variables can also be used to save memory space. When we need to use some larger data, we can create a variable to store the data instead of recreating it all the time. This reduces memory overhead and makes programs more efficient. And the data in the program is not currently needed, so we need variables to store it.

Program running: need to be loaded into memory

Program calculation: need to use variables

Define the essence of variables: allocate a storage space for data in memory, and access and modify data through the address of this storage space.

Difference Between Declaration and Definition

Definition and declaration are two important concepts in programming, they have some similarities, but also differences.

Declaration usually refers to telling the compiler the existence and basic information of a certain variable, function, etc. in the code without specific implementation. When declaring, we only need to specify the type and name of the variable, and there is no need to allocate memory space or perform other operations for it.

For example, if we want to use an integer variable named x in C language, we can declare it like this:

```int x;```

In this declaration, we are just telling the compiler that x is an integer variable, which will be used later in the code. But this statement does not allocate memory space or perform other operations, so the specific value of x is unknown.

Definition refers to the allocation of memory space for variables or functions in the code , and corresponding initialization or implementation . When defining, we need to allocate memory space for variables, give them initial values ​​or perform other operations.

Going back to the above example, if we need to define this variable, we need to implement it on the basis of the declaration:

```int x = 0;```

In this definition, we allocate an integer memory space for x and set its initial value to 0. In this way, in the subsequent code, we can use this variable and operate on it.

In general, both declaration and definition are to tell the compiler the existence and basic information of a certain variable or function in the program, but the declaration will not allocate memory space or perform other operations, while the definition will be based on the declaration. Realize, allocate memory space, and initialize or implement.

Here we use a short story to explain the difference

        The four people in the dormitory all liked a female classmate named Xiao Hua, and there was one person in the dormitory who was the most daring and first asked Xiao Hua to be my girlfriend, and Xiao Hua also said that I would. In order to prevent other people in the dormitory from confessing to Xiao Hua again, you will definitely run to the dormitory and say that Xiao Hua is my girlfriend, you must keep a good distance, and the rest of the dormitory will silently feel sad after hearing this.

In this story: Confession Xiaohua is the definition, which can only be defined once,

                        Informing the roommates is a statement, and the statement can be made multiple times.

Haha, this blog sharing is over.

Guess you like

Origin blog.csdn.net/qq_64446981/article/details/130162525