当前位置:网站首页>C language review 12

C language review 12

2022-06-09 07:54:00 Sakuragi

C Language Check 12

C Language learning , Here is a video of Weng Kai from Zhejiang University , Muke website :https://www.icourse163.org/learn/ZJU-9001?tid=9001#/learn/content. The author's review notes are also mainly based on the video . The main contents of this paper are as follows

  • Programming and design C Language
  • Calculation
  • Judge
  • loop
  • Cycle control
  • data type
  • function
  • Array
  • The pointer
  • character string
  • Structure type
  • Programming
    Global variables ; Compile preprocessing and macros ; Large program structure ; operate a computer 12;
  • file
  • Linked list

refer
1.


12.1 Global variables

One 、 Lifetime and scope

A global variable is a variable defined outside a function ( Include main function ), Generally, it has global lifetime and scope , They can be used inside any function .

Two 、 Global variable initialization

Uninitialized local variables get an odd value , But if the global variable is not initialized, you will get 0 value , The pointer will get NULL value . Global variables can only be initialized with values known at compile time ( I can't use scanf), They are initialized in main Function before .

3、 ... and 、 Static local variables

Add modifier static Get the static local variables . When the function leaves , Static local variables continue to exist and keep their values . The initialization of static local variables will only be done when entering the function for the first time , In the future, each time you enter the function, you will keep the value of the last time you left .
Static local variables are actually special global variables , Has a global lifetime and a local scope within a function .

Four 、 Use less global variables and static local variables

Global variables and static local variables make threads unsafe .

12.2 Compile preprocessing and macros

One 、 Compile preprocessing instructions and macros

In the first picture we talked about , from .c After the file is pretreated, it becomes .i file , among # The beginning is the compilation preprocessing instruction , and #define Used to define a macro .
#define No C The sentence of , His use is as follows :define < name > < value >
 Insert picture description here

12.3 Large program structure

One 、 Projects and compilation units

When our program is too large , There will be more than one .c file , And one .c The document is actually a Compilation unit , The compiler can only process one compilation unit at a time .
In a project project There will be multiple compilation units in the .c file , Compiler or IDE All source code files need to be compiled and linked .
yes , we have IDE Have separate compile and structure Two buttons , The former is compilation , The latter is to link the whole project .

Two 、 The header file

The function prototype Will be put into a header file (.h file ), You need to call this function in the source code file (.c file ) in #include This header file .

#include Is a compilation preprocessing instruction , Like macros , Before compiling, you need to deal with .
#include Yes #include <> and #include "" Two forms .
among #include "" The compiler will be asked to start in the current directory (.c File directory ) Look for this file , without , Go to the directory specified by the compiler ;
#include <> The compiler will be asked to look only in the specified directory ; Be careful , environment variable And compiler command-line arguments can specify the directory to look for header files .mac The loading order of environment variables for is :

a. /etc/profile			#  System level 
b. /etc/paths			#  System level 
c. ~/.bash_profile		#  User level , Often find a 
d. ~/.bash_login
e. ~/.profile
f. ~/.bashrc

#include It's not used to import and store , He just wanted the compiler to know printf Prototypes of functions , Ensure that the parameter values given during the call are of the correct type .
Generally any .c Each file has its corresponding file with the same name .h file , Put all function prototypes and global variable declarations in it .

Let's enjoy the source code of red alert :
https://github.com/electronicarts/CnC_Remastered_Collection/tree/master/REDALERT

原网站

版权声明
本文为[Sakuragi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090746457792.html