<> A famous flower has its own name

C There are some predefined strings in the language , They are endowed with their own functions . And when we define variables , You can't steal their names . They are the protagonists today : keyword

First of all, let's simply meet these keywords ( The highlighted keywords are what we will introduce today )

autobreakcasecharconstcontinuedefaultdo
doubleelseenumexternfloatwhilegotoif
staticlongregisterreturnshortsignedsizeofint
structswitchtypedefunionunsignedvoidvolatilefor
There are well-known data types :int,char,float,double…

There are also control statements used :if,for,do…

There are also some key words mainly introduced today .

As for some new keywords , Not mentioned in the above table , If you want to know , You can find it yourself .

<> Introduction to individual terms ( You can skip it first , If you don't understand , But come back to understand )

Automatic variable : Refers to local scope variables , Specifically, when the control flow enters the scope of variable, the system automatically allocates memory space for it , And release a class of variables in the space when they leave the scope

Storage class : yes C Language and C++ In the standard of language , Accessibility of variables and functions ( That is, scope ) Life and survival

register : Register is CPU A small internal storage area for storing data , It is used to temporarily store the data and results involved in the operation .

​ The data in the computer can be stored in the : register , High speed buffer , Memory , Hard disk , Network disk

​ ( The above storage is from left to right , More and more memory , Counterfeiting is getting lower and lower , The transmission speed is getting slower and slower )



<>auto

auto: Declare automatic variables , It is generally omitted

​ By default , Variables declared within a code block are automatic variables , However, the keyword of automatic variable can also be used auto Clearly identify the storage class

The code is as follows :
int a=0; // The upper and lower variables are actually the same , It's just that it's omitted auto auto int a=0;
<>register

register: Declare register variables

​ This keyword defines the variable , Will ask the compiler to keep the variable as long as possible CPU In the internal register , Instead of searching through memory
Address access , To improve efficiency .( Because the register is very small , If you define a lot register variable , Maybe more than CPU Your deposit Number of filters , Over capacity , Therefore, variables are not necessarily stored in registers )

<>signed and unsigned

signed: Signed ( General definition constant, default to signed)------- You can store positive and negative numbers

unsigned: Unsigned ----- Negative numbers cannot be stored
signed int a=10; // The default value is signed, So just write it :int a=10; unsigned float=-75.3------> Wrong definition
float=-75.3------> Correct definition signed float=-75.3-------> Correct definition
<>typedef

typedef: Type redefinition ------- Generally, for data types that are too complex , A simple or discernible use can be redefined

​ typedef < data type > < Your type name >
unsigned long int a=10; // The data type is too complex , have access to typedef Redefining typedef unsigned long int
u_l_int; u_l_int a=10; // And unsigned long int a=10 equivalence
<>extern

extern: Used before the declaration of a variable or function , Used to say this variable / Functions are defined elsewhere , To reference here

​ By default, a global variable can only be used in the file that defines it ( From the beginning of defining the global variable to the end of the file ),
But if you declare this variable as an external variable in another file , Then the scope of this variable will be extended to another text In progress .

The code is as follows :

First create a project named :main.c
#inlcude <stdio.h> extern int a; extern int Add(int x,int y); int main() {
printf("%d\n",a); printf("%d\n",Add(3,2)); }
Then create a project named :test.c------- As a global variable , Function definition file
int a=10;// global variable int Add(int x,int y)// { return x+y;// Function definition }
The results will be output 10,5

expand :

*
There are two cases of variable declaration
​ 1. One is to build storage space . for example :int a The storage space is established at the time of declaration .
​ 2. The other is that there is no need to build storage space , through the use of extern Keyword declares a variable name without defining it .
for example :extern int a Where variables a It can be defined in other files .
​ extern int i; // statement , It's not a definition
​ int i; // statement , It's also a definition

*
Define allocated storage space , The statement will not

*
How to distinguish declaration from variable

* extern Tell the compiler that the variable is defined elsewhere
* If the declaration has an initializer , It's taken as a definition , Even if the front is added extern
* The difference between function declaration and definition is relatively simple , have { } That's the definition , Otherwise it's a statement
* No, extern Keywords are all variable definitions
<>static

static: Declare static variables

* Modifying local variables Make its life cycle longer ( It essentially changes the storage type of variables )
* Modify global variables Changes the scope of global variables , Let static global variables only be used in their own source files
Global variables can be used inside other source files , Because global variables have external link properties . But by static After modification , It becomes an internal link property , Other source files cannot be linked to this static global variable
* Modifier function Changed the link property of the function , Changes the external link property of a function to an internal link property , So that the function can only be used inside the source file where it is
explain 1, The code is as follows :
#include <stdio.h> void Add(int x) { static int a=2; a++; printf("%d ",x+a); }
int main() { int i=0; for(i=0;i<3;i++) { Add(1); } return 0; }
The result is 4 5 6, You know that int a=2 It's an automatic variable , At the end of his scope , Space will be released ,a The value of cannot be saved .

Plus static Changed his life cycle , To keep him alive until the end of the project , therefore a The value of is iterated

explain 2,3, The code is as follows :( quote extern Content of , You can compare them by yourself )

First create a project named :main.c
##inlcude <stdio.h> extern int a; extern int Add(int x,int y); int main() {
printf("%d\n",a); printf("%d\n",Add(3,2)); }
Then create a project named :test.c------- As a global variable , Function definition file
static int a=10;// global variable static int Add(int x,int y) { return x+y;// Function definition }
The results are as follows :

Last note :#define and #include Not a keyword , Is the preprocessing instruction

Here are some keywords , But it is not completely clear , If you have any questions, you can inquire by yourself . As for the rest of the keywords , I will give you a detailed introduction in the following chapters .

I hope you have porridge \(^ _ ^)/

Technology