C language —— keyword

purpose : Technology learning is limited , The spirit of sharing is infinite .

【 Remember : Try not to use it printf function , To see the value of a variable , Value of memory .】


【 Definitions and declarations : The definition creates an object and allocates memory for it , Claim no memory allocated .】

auto                // Declare automatic variables , By default, the compiler generally defaults to auto

int                 // Declaring integer variables double             // Declare a double variable long               // Declaring long variables char               // Declare character variables float               // Declare floating point variables short               // Declaring short variables signed             // Declare a signed type variable unsigned           // Declaring unsigned type variables struct              // Declare structure variables union              // Declare union type data enum              // Declaring enumeration types static              // Declare static variables switch             // For switch statements case              // Switch statement branch default            // Other branches in switch statements break             // Jump out of current cycle register           // Declare register variables const             // Declare read-only variables volatile           // Explain that variables can be changed implicitly during program execution typedef           // Used to alias data types extern            // Declaring a variable is being declared in another file ( It can also be regarded as a reference variable )

return            // Subroutine return statement ( Can take parameters , Or without parameters )

void             // Declare that the function has no return value or no parameters , Declare null type pointer continue         // End current cycle , Start next cycle do              // Loop body of a loop statement while            // Loop condition of loop statement if               // Conditional statement else             // Conditional statement negative branch ( And if Combined use )

for              // A circular statement goto            // Unconditional jump statement , Use as little or as little as possible sizeof           // Calculate the memory space occupied by the object , This is not a function

I'll just say some common and important keywords here

1. register:

Request the compiler to save variables to CPU Internal registers rather than memory addressing access for efficiency .. You cannot use the address operator to obtain the address of this variable , Because it is no longer in memory . Not many register variables , But you still have to know what it means .

2. static:--- Modify variables and functions ( Qualified scope )

1. Modifier variable

// Variables are divided into local and global variables , But they all exist in the static area of memory .

// static global , Scope is limited to the file in which the variable is defined , Other files are used extern The statement is not usable .

To be exact, the scope starts from the definition , End at end of file , The preceding lines of code in the definition are also not allowed to be used .

// Static local variable , Defined in the function body , Can only be used in this function , Other functions in the same document cannot be used .

Due to being static Decorated variable Matsushi exists in the static area of memory , So even if this function ends ,

The value of this static variable will not be destroyed , Function can still be used next time .

2. Decorating function

// Add before function static Make the function static .

But here static Storage mode does not mean storage mode , The scope of the function is limited to this document ( So it is also called internal function ).

The benefits of using intrinsic functions are : When different people write different functions , Don't worry about the functions you define , Will it have the same name as a function in another file .

// stay C Keyword introduced in static Is to represent local variables that still exist after exiting a block .

// subsequently ,static stay C Has a second meaning in : Used to represent global variables and functions that cannot be accessed by other files .

To avoid introducing new keywords , So still use static Keyword to indicate the second meaning .

3. Basic data type :short,long,int,char,float,double( common 6 species )

Construction data type : array , structural morphology struct, Common body union, Enum type enum

Empty type void

【short by 2 Bytes ,int by 4 Bytes ,long by 4 Bytes ,float by 4 Bytes ,

double by 8 Bytes ,char by 1 Bytes ( Normally )】

【 Implicit type conversion :short,char → int →unsigned  int → long → double( Main road ) ←float】

4. sizeof keyword : Space occupied by calculated variables  –  Array as function parameter , Degenerate to pointer ,4 byte

sizeof (int)*p  -----  sizeof(int) * p = 4p

Emphasize again ,sizeof Not a function , It's a keyword .

5. signed and unsigned keyword :

//32 Bitwise signedint Type integer whose value range is :-2^31~2^31-1

//8 Bitwise signed char Type whose value range is :-2^7~2^7-1

//32 Bitwise unsigned int Type integer whose value range is :0~2^32-1

//8 Bitwise unsigned char Type whose value range is :0~2^8-1

In the computer , Values are stored as complements : Use complement , The symbol bit can be unified with other bits , At the same time, subtraction can also be treated as addition .

6. if and else keyword :

write if Statement time , Generally, normal conditions are handled first and then abnormal conditions , To improve efficiency , Good readability ;bool Normally initialized to false Quite good ;int,float, Pointer variable   And 0 Comparison problem of ( Shaping can be done directly with 0 compare ;float Is real , Not accurate to 0; Pointer variables and NULL)

if(flag) --- bool

if(0 == i) --- int

if(i > -0.000001 && i <0.000001)  --- float

if(NULL == p) ---  Pointer variable

7. switch/case keyword :【 Solving multi branch problems 】

each case Add at the end of the statement break; Finally, you must use default branch .

case Only integer or character constants or constant expressions can be followed .

8. do/while/for keyword :(3 Cyclic structure )

while: Judge first while Values in parentheses , If true, execute the following code ; Otherwise do not execute

do-while: Execute first do Later code , Then judge while Values in parentheses , If true , Cycle start ; otherwise , Cycle does not start . Usage and while No difference , But it is seldom used .---   At least once

for: The number of cycles can be easily controlled , It is usually used when the number of cycles is known in advance .

【 Multiple cycle time , The longest cycle is placed in the innermost layer , Put the shortest on the outermost layer , To improve execution efficiency ; Cycle no more than 3 Layer nesting 】

9. break Terminate the cycle of this layer ;continue Terminate this cycle , Start next cycle ;return Termination function .

10. void keyword :“ The immaterial is the material. ”--- Very powerful

---  Qualification of function return value ; Qualification of function parameters .

Any pointer type can be directly assigned to void * type , Not the other way around ; Cannot define a void variable .

//void *memcpy(void *dest, const void *src, size_tlen);

//void *memset(void *buffer, int c, size_t num);

Less to say , however void Keywords are really powerful , There is no doubt about that .

11. return keyword :

---  Terminates a function and returns the value that follows it

【 Do not return a pointer to the stack memory , Because the function body is automatically destroyed at the end , Will cause program errors 】

12. const keyword :---- no “ constant ”, But “ read-only variable ”; If used well, it will greatly improve the efficiency of the program

const Decorated with read-only variables , Its value cannot be used at compile time , Because the compiler does not know what it stores at compile time .

const Initial purpose of launch , To replace precompiled instructions , Eliminate its shortcomings , While inheriting its advantages .

const and define Differences between :const Decorated read-only variable , And initialize while defining ;const Can save space , Avoid unnecessary memory allocation , Improve efficiency at the same time ; Decorate general variables , General variables are read-only variables of simple type ; Decorated array ; Decorated pointer ; Parameters of decorating function , Used when you do not want this parameter value to be accidentally changed in the function body ; Return value of decorating function , Indicates that the return value cannot be changed ; Reference in another connection file const read-only variable .

【 Compilers are usually not normal const Read only variables allocate storage space , They are saved in the symbol table , This makes it a compiler component value , No memory storage and read operations , Which makes it very efficient ;const Defined read-only variables from an assembly Perspective , Only the corresponding memory address is given , Not like define Same immediate number given 】

const M = 3;  int a[M]; ---  FALSE , Array cannot be a variable , Even read-only

#define M 3   int a[M]; ---  correct , Macro replacement during preprocessing

const int *p;//p variable ,p The object pointed to is immutable

int const *p;//p variable ,p The object pointed to is immutable

int *const p;//p Immutable ,p The object pointed to is variable

const int *const p;// Pointer p and p Objects pointed to are immutable

13. volatile keyword :--- Distinguish between ordinary engineer and embedded Engineer

Prevent compiler optimization , Embedded people don't know this keyword , I'm going home to practice more !

14. extern keyword :

extern Modify variables or functions , To represent the definition of variables or functions in other files . This prompts the compiler to look for definitions in other modules when it encounters this variable and function .

15. struct keyword :--- Try not to use global variables when writing programs , It's packaged into a structure

Package the associated data into a whole ; The size of the empty structure is not 0, But 1;

Flexible array : Structure last array size unknown , More flexible arrays are used in the kernel linked list .

Type *p=( Type*)malloc(sizeof(Type)+100*sizeof(int));

【 Flexible array has nothing to do with structure , Not a full member of a structure .】

struct and class: Different default attributes ,struct yes public,class yes private.

16. union keyword :

Size is the size of the largest member in the Federation , Instead of the size and size of all members , This point is different from the structure ; A federation can only access one of the data members at a time , Cannot access multiple .

The problem of large and small end modes can be judged by the Consortium :

void check(void)






} stu;

stu.i= 5;

return(5 == stu.j):1,0;


17. enum keyword :

---  Enumeration defines a stack of constants , Like week , Color, etc

18. typedef keyword :--- Alias , Attention and define Differences between , Personally, I think that define Easy to use

typedef unsigned char uchar;

#define uchar unsigned char

(typedef Semicolon at the end ,define unwanted )

Recommended typedef, Because when replacing pointers ,define Often not effective ;

#define char* PPP

PPP a,b;// only a yes char* of ,b no , use typedef Different