2020 diamond C + + C learning notes (1) erudite Valley

Time:2021-12-5

1. C Language Overview

 

1.1 what is C language

When you mention the word language, you will naturally think of natural languages such as English and Chinese, because it is an indispensable tool for people to exchange information.

Today, computers are all over every corner of our life. In addition to the communication between people, we must communicate with computers.

What is the most direct way to communicate with the computer? People naturally think of the oldest and most convenient way – language, and C language is a language for people to communicate with computers.

Language is used for communication. When one party speaks and the other listens, two parties must participate. This is the most important function of language:
 the speaking party transmits information and the listening party receives information;
 the speaking party gives instructions, and the listening party follows the orders.

Language is the communication between people, and C language is the communication between people and machines. However, people can not listen to another person, but the computer is unconditionally obedient.

Language has unique grammatical rules and definitions. Both sides must follow these rules and definitions in order to realize real communication.

1.2 why learn C language

1.2.1 C language features

1) Advantages
• small amount of code
• fast execution speed
• powerful
• Programming Freedom

2) Shortcomings
• long code writing cycle
• poor portability
• too free, inexperienced and error prone
• more dependence on platform library

1.2.2 reasons for learning C language

 

 

 

1.2.3 application field of C language

C language is widely used. It can be developed in C language from website background to bottom operating system, from multimedia applications to large-scale online games:
 C language can write website background program
 C language can write a powerful program library specifically for a topic
 C language can write the engine of large-scale games
 C language can write another language
 C language can write operating system and driver, and can only be written in C language
 as long as any device is equipped with a microprocessor, it supports C language. From microwave oven to mobile phone, it is promoted by C language technology

 

 

 

1.2.4 conciseness of C language

C language has only 32 keywords, 9 control statements and 34 operators, but it can complete countless functions:

 

 

 

 

 

 

 

 

1.2.5 common puzzles in learning C language

 

 

1.3 the first C language program: HelloWorld

1.3.1 writing C language code: Hello. C

#include

int main()
{
//This is the first C language code
printf(“hello world\n”);
return 0;
}
The source code file of C language is an ordinary text file, but the extension must be. C.

 

1.3.2 compiling C code through GCC

1) Introduction to gcc compiler
Editors (such as VI and notepad) refer to the programs I use to write programs (edit code), and the code statements we write are not understood by the computer. We need to turn them into statements that the computer can understand. The compiler is such a conversion tool. In other words, we use the editor to write the program, which can be run only after being compiled by the compiler!
A compiler is a program that translates a high-level computer language that is easy to write, read and maintain into a low-level machine language that can be interpreted and run by a computer.
GCC (GNU compiler collection) is a programming language compiler developed by GNU. As the official compiler of GNU operating system, GCC has been adopted as the standard compiler by most UNIX like operating systems (such as Linux, BSD, Mac OS X, etc.), and GCC is also applicable to Microsoft Windows.

GCC was originally used to compile c language. With the development of the project, GCC has become a large family of compilers that can compile C, C + +, Java, ADA, FORTRAN, Object C, Object C + +, go languages.

Compile command format:
gcc [-option1] …
g++ [-option1] …
 commands, options and source files are separated by spaces
 there can be zero, one or more options in one line of command
 the file name can contain the absolute path of the file or the relative path
 if the command does not contain the file name of the output executable file, the file name of the executable file will automatically generate a default name, a.out for Linux platform and a.exe for Windows platform

Description of common options for GCC and G + + compilation:
Options meaning
-o file Specifies that the generated output file name is file
-E Pretreatment only
-S (in words) Only preprocessing and compilation
-C (in figures) Only preprocessing, compilation and assembly

2) GCC environment configuration on Windows platform
In the windows command line interface, there is no gcc compiler by default. We need to configure the environment. Since QT is installed, QT is an integrated development environment. Gcc compiler is integrated internally. You can use GCC by configuring environment variables.

a) Enter QT installation directory:

b) Copy the path where GCC is located (different installation directories)
C:\Qt\Qt5.5.0\Tools\mingw492_32\bin

c) Setting Windows environment variables
Computer (right click) – > properties:

 

If it is win7, double-click path and append the copied path to the back. English is required between the paths ” separate:

In case of win10, make the interface friendly and create a new path:

d) Test GCC command:

 

3) Compiling code under Windows platform

 

1.3.3 code analysis

1) Include header file contains
 #include means that the header file contains, #includeRepresents the header file containing stdio. H
 when using the C language library function, you need to include the header file corresponding to the library function in advance. For example, if the printf() function is used here, you need to include the stdio. H header file
 you can view the header file required by printf through man 3 printf

#Difference between include < > and #include “”:
 < > indicates that the system searches directly according to the directory specified by the system
 “” means that the system first looks for the header file in the path specified by “” (the unwritten path represents the current path). If it cannot be found, it will be retrieved according to the directory specified by the system

Stdio. H is in the system directory of the operating system:

2) Main function
 a complete C language program is composed of one and only one main() function (also known as the main function, which must be present) and several other functions (optional).
 the main function is the entrance of the C language program, and the program is executed from the main function.

3) {} parentheses, program body and code block
 {} is called code block. A code block can have one or more statements
 every executable code in C language ends with a semicolon “
 all the # beginning lines represent precompiled instructions, and there is no semicolon at the end of the precompiled instruction line
 all executable statements must be in code blocks

4) Notes
 / / call line comments. The content of comments is ignored by the compiler. Comments are mainly used to add some descriptions and explanations to the code, which is conducive to code reading
 / * * / block comment
 block annotation is the standard annotation method of C language
 line comments are borrowed from C + + language

5) Printf function
 printf is a C language library function, and its function is to output a string to the standard output device
printf(“hello world\n”);//\ N means enter and line feed

6) Return statement
 return represents the completion of the function, and return represents the termination of the function
 if int precedes the definition of main, an integer needs to be written after return; If void precedes main when it is defined, there is no need to write anything after return
 in the main function, return 0 represents successful program execution, and return – 1 represents failed program execution
 int main() and void main() are the same in C language, but C + + only accepts the definition of int main

1.4 system function

1.4.1 use of system function

#include
int system(const char *command);
Function: execute another external program in the already running program
Parameter: external executable name
Return value:
Success: 0
Failed: any number

Example code:
#include
#include

int main()
{
//system(“calc”); // Windows platform
system(“ls”); // Linux platform, header file #include required

return 0;
}

1.5 C language compilation process

1.5.1 C program compilation steps

The C code is compiled into an executable program through 4 steps:
1) Preprocessing: macro definition expansion, header file expansion, conditional compilation, etc., and delete comments in the code. Syntax will not be checked here
2) Compile: check the syntax and compile the preprocessed file to generate an assembly file
3) Assembly: generate assembly file into object file (binary file)
4) Link: programs written in C language need to rely on various libraries, so the libraries need to be linked to the final executable program after compilation

1.5.2 GCC compilation process

1) Step by step compilation
Preprocessing: GCC – e hello. C – O hello. I
Compilation: GCC – s hello. I – O hello. S
Assembly: GCC – C hello. S – O hello. O
Link: GCC hello.o – O hello

option meaning
-E Pretreatment only
-S (in words) Only preprocessing and compilation
-C (in figures) Only preprocessing, compilation and assembly
-o file Specifies that the generated output file name is file

file extension meaning
.c C language file
.i Preprocessed C language file
.s Compiled assembly file
.o Compiled object file

 

2) One step compilation
GCC hello.c – O demo (or the process of preprocessing, compiling, assembling and linking):

 

1.5.3 find the dynamic library that the program depends on

 

Under Windows platform, corresponding software (depends. Exe) is required:

 

1.6 CPU internal structure and registers (understand)

1.6.1 differences between 64 bit and 32-bit systems

 register is the most basic storage unit in CPU
 the CPU interacts with external devices through the bus (address, control and data). The width of the bus is 8 bits, and the CPU register is also 8 bits, so this CPU is called 8-bit CPU
 if the bus is 32-bit and the register is 32-bit, then this CPU is a 32-bit CPU
 there is a kind of CPU whose internal register is 32 bits, but the bus is 16 bits, and the standard 32 is CPU
 all 64 bit CPUs are compatible with 32-bit instructions, and 32-bit should be compatible with 16 bit instructions, so 32-bit instructions can be recognized on 64 bit CPUs
 if a 64 bit software operating system is running on a 64 bit CPU architecture, the system is 64 bit
 if a 32-bit software operating system is running on a 64 bit CPU architecture, the system is 32-bit
 64 bit software cannot run on a 32-bit CPU

1.6.2 register name (understand)

8 bits 16 bit 32 bit 64 bit
A AX EAX RAX
B BX EBX RBX
C CX ECX RCX
D DX EDX RDX

1.6.3 relationship among register, cache and memory

According to the distance from the CPU, the nearest is the register, then the cache (CPU cache), and finally the memory.

During CPU calculation, first read the data to be used from the hard disk to the memory, and then read the data to be used to the register. So the CPU registers memory, which is the information exchange between them.

So why cache? Because if you often operate on the data at the same address in memory, it will affect the speed. Therefore, a cache is set between register and memory.

Because extracting from the cache is much faster than memory. Of course, the price of cache must be much higher than that of memory. Otherwise, there will be no memory in the machine.

It can be seen that from a distance: CPU <— > register <— > cache memory.

 

1.7 assembly language

1.7.1 C language nested assembly code in vs (understand)

#include

int main()
{
//Define integer variables a, B, C
int a;
int b;
int c;

__asm
{
mov a, 3 // The value of 3 is placed in the memory corresponding to a
mov b, 4 // The value of 4 is placed in the memory corresponding to B
mov eax, a // Put the value of a memory in the eax register
add eax, b // Eax and B are added, and the result is placed in eax
mov c, eax // The value of eax is placed in C
}

printf(“%d\n”, c);// Output the value of C

return 0;// Successfully completed
}

1.7.2 vs disassembly

#include

int main()
{
//Define integer variables a, B, C
int a;
int b;
int c;

a = 3;
b = 4;
c = a + b;

printf(“%d\n”, c);// Output the value of C

return 0;// Successfully completed
}

1) Set breakpoint F9

2) Select the disassembly button

3) According to assembly code analysis program

1.8 integrated development environment IDE

Integrated development environment (IDE) is an application program used to provide program development environment, which generally includes code editor, compiler, debugger and graphical user interface tools. It integrates the functions of code writing, analysis, compilation and debugging. All software or software suite (Group) with this feature can be called integrated development environment.

1.8.1 Qt Creator

QT creator is a cross platform QT ide. QT creator is a new lightweight integrated development environment (IDE) launched after QT was acquired by Nokia. This ide can run across platforms. Supported systems include Linux (32-bit and 64 bit), Mac OS X and windows. According to the official description, the design goal of QT creator is to enable developers to use QT as an application framework to complete development tasks more quickly and easily.

Shortcut key meaning
Ctrl + i Auto format code
Ctrl + / Comment / uncomment
Alt + Enter Autocomplete class function definition
F4 . H file and corresponding. Cpp file switching
F9 Set breakpoint
F5 Commissioning operation
Ctrl + r Compile without debugging
Ctrl + b Compile, do not run
F10 Next debugging
F11 Step debugging