1. Overview of C language

Diamond C++ of the Year 2020: XB2K

1.1 What is C language

When the word “language” is mentioned, the natural language like English and Chinese will come to mind, because it is an indispensable tool for people to exchange information.

And today the computer is all over every corner of our life, in addition to the mutual communication between people, we must and the computer corner.

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

Language is for communication. The most important function of language is that one party speaks and the other listens, and both parties have to buy a ticket: the speaker transfers a message, while the listener listens. Chatting, the person gives commands, while the listener listens to something, according to the arrangement. V(cmL46679910)

Language is about people talking to people, and C is about people talking to machines. It is just that a person can not listen to another person, but a computer is unconditionally obedient.

Languages have unique grammatical rules and definitions that must be followed by both parties in order to truly communicate.

1.2 Why do I need to learn C

1.2.1 FEATURES of C language

  1. advantages

(3) Small code (4) Fast execution (5) Powerful (6) Programming freedom (7

  1. disadvantages

(3) Long code implementation cycle (4) Poor portability (5) Too much freedom, lack of experience and error-prone (6) More reliance on platform library (7)

1.2.2 Reasons for learning C language

 

1.2.3 Application field of C language

The application of C language is extremely extensive, from website background, to the bottom operating system, from multimedia applications to large-scale online games, C language can be used to develop:

C programming language can write website backstage   C language can be designed for a topic to write powerful libraries can write big game engine   C C language to write another language to  C language to write the operating system and driver, and can only be written in C language  any equipment as long as the microprocessor, They all support C. Everything from microwave ovens to cell phones is powered by C language technology

 

1.2.4 Simplicity of C language

C language only 32 keywords, 9 control statements, 34 operators, but can accomplish countless functions:

 

 

 

 

1.2.5 Common puzzles in LEARNING C language

 

1.3 The first C program: HelloWorld

1.3.1 Writing C language code: Hello.c


#include <stdio.h>

Int main() {// This is the first C code printf(“hello world\n”); return 0; } The C source code file is a plain text file, but the extension must be.c.

1.3.2 Compiling C Code using GCC

  1. GCC compiler introduction \

Editor (such as VI, Notepad) means I use it to write programs (edit code), and we write code statements, the computer does not understand, we need to convert it into statements that the computer can understand, compiler is such a conversion tool. That is, we use an editor to write a program that is compiled by a compiler before it can run! A compiler is a program that translates high-level computer languages that are easy to write, read, and maintain into low-level machine languages that computers can read and run. GCC (GNU Compiler Collection) is a programming language Compiler developed by GNU. GCC was originally the official compiler for the GNU operating system, but has now been adopted as the standard compiler for most Unix-like operating systems (such as Linux, BSD, Mac OS X, etc.). GCC is also suitable for Microsoft Windows.

GCC was originally used to compile C language. With the development of the project, GCC has become a large family of compilers capable of compiling C, C++, Java, Ada, FORTRAN, Object C, Object C++, and Go languages.

GCC [-option1]… g++ [-option1] …  command, option, and the source file, separated by Spaces between  a line in the command can have zero, one or more options  absolute path filename can include files, you can also use the relative path  if the command output does not contain executable file name, in the executable file name will automatically generate a default name, Linux platform for a.out, The Windows OS is A. exe

GCC, g++ compiler common options are described as follows: option meaning -o file specifies the name of the generated output file. File -e only preprocesses. -s (in uppercase) only preprocesses and compiles

  1. Windows platform GCC environment configuration \

Windows command line interface, the default is no GCC compiler, we need to configure the environment. Since we have Qt installed, Qt is an integrated development environment that integrates the GCC compiler internally, so you can configure environment variables to use GCC.

A) Go to the Qt installation directory:

C:\Qt\Qt5.5.0\Tools\mingw492_32\binV(cmL46679910)

C) Set Windows environment variable computer (right click) -> Properties:

 

If Windows 7, double-click Path, add the copy of the Path to the back, between the Path need English “; Space:

If win10, the interface to do a friendly, new add path can be:

D) test GCC command:

 

  1. Windows compiles code

 

1.3.3 Code analysis

  1. Include Includes header files

Anyway, the C library functions need to include the corresponding header file, for example, using the printf() function. You need to include the stdio.h header file to buy a ticket. You can use man 3 printf to view the required header file for printf

The difference between #include< > and #include: either the system automatically retrieves a header file at a certain path (no written path represents the current path), or, if it can’t find a header file, it retrieves it at the system’s designated directory

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

A complete C program costs itself to buy a program by combining one, and only one, main() function (which is mandatory) with several other functions (optional). A program executes from main, which acts as an entrance to a C program.

  1. {} parentheses, program body and code block

A block of code can have one or more statements running the according to the PRINCIPLE of buying a ticket. In C, each line of executable code is “; Anyway, all lines that start with a # queue queue for a precompiled instruction, which ends in a precompiled line without a semicolon. All the executable statements have to be in a block of code anyway

  1. annotation

Anyway, the compiler ignores something in the way of a line comment, which essentially adds some explanation to the code, according to a new C standard, which lends itself to a line comment, either unconsciously /**/

  1. The printf function

A library function functions for the C language to output a string, either automatically printf(” Hello world\n “); //\n means return to line feed

  1. Return statement

According to the definition, main is defined using an int, so you write an integer after the return. Anyway, if main is defined using void, you don’t need to write something at the end of the return process. According to the definition, either the program executes successfully by returning 0, or the program fails to execute according to return-1, which works the same way in C. But C++ only accepts int main

1.4 the system function

1.4.1 Use of the System function

#include

int system(const char *command); Function: Executes another external program in an already running program Parameter: External executable program Name Returned value: Success: 0 Failed: any number

#include

#include

int main()

{

//system(“calc”); //windows平台

system(“ls”); //Linux平台, 需要头文件#include <stdlib.h>

return 0;

}

1.5 C language compilation process

1.5.1 C program compilation procedure

C code compiled into an executable program through four steps: 1) pretreatment: macro definition deployment, header files, and conditional compilation, delete the comments in the code at the same time, it will not check syntax 2) compilation: check the syntax, the compiler generated assembly file 3) after pretreatment assembly: will compile files are generated object file (binary) 4) link: Programs written in C need to rely on various libraries, so after compiling the library also need to link to the final executable program

1.5.2 GCC Compilation process

  1. Step by step to compile

GCC -e hello. C -o hello. I 错 误 : GCC -s hello. I -o hello

-e Only preprocessing -s (uppercase) only preprocessing and compiling -c(lowercase case) Only preprocessing, compiling and assembling -o file Specifies the name of the generated output file

I Preprocessed C language file. S Compiled assembly file. O Compiled target file

 

GCC hello.c -o demo:

 

1.5.3 The dynamic library that the finder relies on

For Windows, you need the corresponding software (Depends. Exe) :

 

1.6 CPU Internal Structure and Registers (Understanding)

1.6.1 Differences between 64-bit and 32-bit systems

According to the SYSTEM, a CPU uses an 8-bit bus (address, control, and data) to interact with an external device. The CPU also has 8-bit registers, which act according to the BUS. **V(cmL46679910)** You buy a 32-BIT CPU according to the system’s internal registers, but the bus is 16-bit, so the quasi-32 acts for the CPU. All 64-bit cpus work with 32-bit instructions, which have to be compatible with 16-bit instructions. So, a 64-bit CPU recognizes that something runs a 64-bit software operating system according to the system’s principle of running a 64-bit software operating system at the same time. According to the system, the 64-bit software can’t run on a 32-bit CPU either by running a 64-bit software operating system at the same time

1.6.2 Register Name (Understanding)

8 bit 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 Relation among register, Cache and memory

The nearest is the register, then the cache (CPU cache), and finally the memory.

When the CPU computes, it first reads the data to be used from the hard disk to the memory, and then reads the data to be used to the register. So the CPU<– > register <– > memory, this is the information exchange between them.

So why is there a cache? Because if you frequently manipulate data in the same location in memory, it will affect speed. A cache is set up between the register and memory.

Because extraction from cache is much faster than memory. Of course, the price of cache must be much higher than memory, otherwise there would be no memory in the machine.

As can be seen from a distance: CPU <– > register <– > cache <– > memory.

1.7 Assembly Language

1.7.1 C nested assembly Code in VS (Understanding)


#include <stdio.h>

Int main() {// define the integer variable a, b, c int a; int b; int c;

__asm {mov a, 3 //3 mov b, 4 //4 mov eax, a // put the value of a memory in the eax register add eax, b //eax and b add, Printf (“%d\n”, c); printf(“%d\n”, c); // print the value of c

return 0; // Completed successfully}

1.7.2 VS disassembly


#include <stdio.h>

Int main() {// define the integer variable a, b, c int a; int b; int c;

a = 3; b = 4; c = a + b; printf(“%d\n”, c); // print the value of c

return 0; // Completed successfully}

1) Set breakpoint F9

2) Select the disassembly button

3) Analyze programs according to assembly code

1.8 Integrated development Environment IDE

An IDE (Integrated Development Environment) is an application that provides a program Development Environment, typically including a code editor, compiler, debugger, and graphical user interface tools. Integrated code writing function, analysis function, compilation function, debugging function and other integrated development software service set. Any software or suite of software that has this feature can be called an integrated development environment (IDE).

, version 1.8.1 Qt Creator

Qt Creator is a cross-platform Qt IDE. V(cmL46679910) Qt Creator is a new lightweight integrated development environment (IDE) launched after Qt was acquired by Nokia. The IDE runs cross-platform on Linux (32-bit and 64-bit), Mac OS X, and Windows. According to the official description, Qt Creator is designed to enable developers to use Qt as an application framework for faster and easier development tasks.

Ctrl + I Auto format code Ctrl + / Comment/uncomment Alt + Enter Auto complete class function definition F4.h file and corresponding. CPP file Switch F9 Set breakpoint F5 Debug Run Ctrl + R compile, But do not debug run Ctrl + B compilation, do not run F10 next debugging F11 step debugging