当前位置:网站首页>[C language] keyword

[C language] keyword

2022-06-23 05:07:00 Ordinary people 1

author :@ Ordinary person 1

special column :《C Language from 0 To 1》

In a word : In the past , All is prologue

explain : The past is irreparable , The future can change


Understand keyword classification

C How many keywords are there in the language ?

In ordinary books , All are 32 individual ( Including this book ), But this is all C90(C89) Standards for . Actually C99 And then I added 5 Key words .

however , Current mainstream compilers , Yes C99 The support is not good , We will default later , Use C90 , The idea that 32 individual

Keywords and their descriptions

  1. auto

    Declare automatic variables

  2. short

    Declare short integer variables or functions

  3. int

    Declare an integer variable or function

  4. long

    Declare long integer variables or functions

  5. float

    Declare floating-point variables or functions

  6. double

    Declare a double precision variable or function

  7. char

    Declare character type variables or functions

  8. struct

    Declare a structural variable or function

  9. union

    Declare shared data type

  10. enum

    Declare enumeration type

  11. typedef

    Used to alias data types

  12. const

    Declare read-only variable

  13. unsigned

    Declare an unsigned type variable or function

  14. signed

    Declare a symbolic type variable or function

  15. extern

    Declare variables are being declared in other files

  16. register

    Declare register variables

  17. static

    Declare static variables

  18. volatile

    Explain that variables can be implicitly changed during program execution

  19. void

    Declares that the function returns no value or takes no arguments , Declare no type pointer

  20. if

    Conditional statements

  21. else

    Conditional statement negates Branch ( And if Continuous use )

  22. switch

    For switch statements

  23. case

    Switch statement Branch

  24. for

    A circular statement

  25. do

    The body of a loop statement

  26. while

    The loop condition of a loop statement

  27. goto

    Jump statements without conditions

  28. continue

    End the current cycle , Start next cycle

  29. break

    Jump out of current loop

  30. default

    In the switch statement “ other ” Branch

  31. sizeof

    Calculate the data type length

  32. return

    Subroutine return statement ( With parameters , Or without parameters ) The loop condition


Understand variables 、 Definitions and statements

Question 1 : What is a variable

Open up a specific size of space in memory , To hold data

keyword : Memory

Question two : How to define variables ( How to use it? )

int a = 10;

char b = ‘a’;

double c = 3.14;

type Variable name = The default value is

Question 3 : Why define variables ( Why? )

Computers are born to solve the problem of people's lack of computing power . namely , Computers are for calculation .

And calculation , You need data .

And to calculate , Any moment , Not all data should be calculated immediately .

Like : To eat , Not all the meals will be eaten by you immediately . The rice should be eaten mouthful by mouthful , So what you haven't eaten yet , You need to put it on the plate for the time being .

The plates here , Just like variables , Meals are like data in variables .

let me put it another way , Why variables are needed ? Because there is data that needs to be saved temporarily , Waiting for further processing .

that , Why eat with plates ? I'd like to have a bite , Can't you just look in the pot ? Of course. , But it's inefficient .

Because where we eat , And a place to cook , Is the more " far " Of

The essence of variable definition

We now know :

  1. The program runs , It needs to be loaded into memory

  2. Program calculation , You need to use variables

that , Define the nature of variables :

Open up a space in memory , To hold data .( Why must it be memory : Because defining variables , It is also a part of program logic branch , The program has been loaded into memory )


Three keywords

The most generous keyword - auto

Due to introduction auto It's about variables , So we want to start with variables

Classification of variables —— Supplementary content
// local variable : The variables contained in the code block are called local variables . Local variables are temporary . Enter code block , Automatically form local variables , Exit the code block automatically   Release .[ Many on the Internet say that the variables in the function are local variables , You can't say wrong , But the statement is inaccurate ] 
// Global variables : Variables defined outside all functions , It's called a global variable . Global variables are global . 
// Code block : use {} The enclosed area , It's called a code block  
#include <stdio.h> 
#include <windows.h> 
int g_x = 100; // Global variables  
int main() 
{
     
    int x = 10; // local variable ,main Functions are also functions , There are also code blocks {} 
    printf("x:%d\n", x); 
    system("pause"); 
    return 0; 
}
Scope of variable —— Supplementary content
// The concept of scope : It refers to the code area of the variable that can be accessed normally  
#include <stdio.h> 
#include <windows.h> 
int main() 
{
     
    int x = 10; 
    if (x == 10)
    {
     
        int y = 20; 
        printf(" Local : x: %d, y: %d\n", x, y);//y Only valid within this code block  
    }
    printf(" Local : x: %d, y: %d\n", x, y); // Report errors ,y Can't be visited  
    system("pause");
    return 0;
}
// local variable : Valid only within this code block  // The same as above  
// Global variables : During the whole program running , Are effective 
#include <stdio.h> 
#include <windows.h> 
int g_x = 100; // Global variables  
void show() 
{
     
    printf("show:  overall situation : %d\n", g_x); // Can be accessed in any code block  
}
int main() 
{
    
     show(); 
     printf("main:  overall situation : %d\n", g_x); // Can be accessed in any code block , Even modified  
     system("pause"); 
     return 0; 
} 

#include <stdio.h> 
#include <windows.h>
int g_x = 100; // Global variables  
int main() 
{
     
    int g_x = 10; // local variable , With the same name as the global 
    printf("g_x:%d\n", g_x); // The output is local , That is, when part and all have the same name , Preferential local . therefore , This is strongly discouraged 
    system("pause"); 
    return 0; 
}
Life cycle of variable —— Supplementary content

Life cycle concept : It refers to the time range from the definition of the variable to its release , The so-called release , It means that the space once opened up is released

local variable : Enter code block , Form local variables [ Open up space ], Exit code block ," Release " local variable

Global variables : After definition , Throughout the life cycle of the program , This variable is always valid

auto relevant
// How to use : Variables typically defined in code blocks , Local variable , The default is auto Embellished , But generally omit  
// By default, all variables are auto Do you ? No , Generally used to modify local variables  
// Give me a break : What we'll see later , local variable , Automatic variable , Temporary variable , It's the same thing . We collectively refer to local variables 
#include <stdio.h> 
#include <windows.h> 
int main() 
{
    
    for (int i = 0; i < 10; i++)
    {
    
        printf("i=%d\n", i); 
        if(1) 
        {
     
         auto int j = 0; // Automatic variable 
         printf("before: j=%d\n", j); 
         j += 1; 
         printf("after : j=%d\n", j); 
        }
    }
    system("pause"); 
    return 0; 
}

The fastest keyword —— register

Actually ,CPU It is mainly the hardware unit responsible for calculation , But for ease of calculation , Generally, the first step is to read the data from memory to CPU Inside , Then you need CPU It has certain temporary data storage capacity . Be careful :CPU It's not the time to calculate , To read specific data to CPU Inside , That's too slow , So modern CPU Inside , Are integrated with a set of hardware called registers , Used to save temporary data .

How to understand ? We can understand through a picture :

image-20220621215621089

distance CPU The closer the storage hardware , The faster the speed. .

The essence of register existence On the hardware level , Improve the computing efficiency of the computer . Because you don't need to read data from memory

register Modifying variables As far as possible The modified variable , Put in CPU In the deposit area , So as to achieve the purpose of improving efficiency

The problem is coming. :

So what kind of variables , May adopt register Well ?

  1. Local ( The overall situation will lead to CPU The register is occupied for a long time )
  2. Will not be written ( Writing requires writing back to memory , If you need to read the test later ,register What is the meaning of ?)
  3. High frequency read ( Improving efficiency lies in )
  4. If you want to use , Please don't use a lot of , Because the number of registers is limited
  5. Except for the above , One more thing , Namely register Decorated variable , Address not available ( Because it's already in the deposit area , Address is a memory related concept )
#include <stdio.h> 
#include <windows.h> 
int main() 
{
     
    register int a = 0; 
    printf("&a = %p\n", &a); // Compiler error : error  1 error C2103:  On a register variable “&” 
    // Be careful , Not all compilers report errors here 
    // Since the students are just beginning to learn , No burden on everyone , We will slowly introduce another compiler later . 
    system("pause"); 
    return 0; 
}

Let's test :

image-20220621220239930

But now we don't need to add the keyword manually , reason : The keyword , Never mind , Because today's compiler , Already very smart , Better code optimization than people . Early compilers needed to specify register, For manual optimization , No need now .

The most unworthy keyword —static

Two conclusions of global variables and functions - Supplementary content

  1. Global variables , Can cross file , Interviewed .

  2. Global function , Can cross file , Interviewed .

Modifying variables

1. Modify global variable , This global variable can only be used in this file .

summary :static Modify global variable , What affects is the concept of scope , Function similar to . The life cycle is constant .

2. Modify local variables

 void fun1() 
 {
     
     int i = 0; 
     i++; 
     printf("no static: i=%d\n", i); 
 }
void fun2()
{
     
    static int i = 0; 
    i++; 
    printf("has static: i=%d\n", i);
}
int main() 
{
     
    for (int i = 0; i < 10; i++)
    {
     
        fun1(); fun2(); }
    system("pause"); 
    return 0;
}
// Conclusion :static Modify local variables , The life cycle of the variable becomes the global cycle .( The scope remains unchanged )
// Modify function , This function can only be used within this file .

Conclusion

Ashamed to speak , Today is another rotten day , It was almost twelve o'clock before I knew it , It's really a fast day , Let's finish here first !

原网站

版权声明
本文为[Ordinary people 1]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230146074762.html