C language learning [2]-storage classes and operators

1. Storage

The storage class defines the scope (visibility) and life cycle of variables/functions in a C program.
Storage classes available in C programs:

  • auto----The default storage class of all local variables. Auto can only be used in functions, that is, auto can only modify local variables.
{
    
    
   int mount;
   auto int month;
}
//定义了两个带有相同存储类的变量
  • register----Used to define local variables stored in registers instead of RAM.

This means that the maximum size of the variable is equal to the size of the register (usually a word), and the unary'&' operator cannot be applied to it (because it has no memory location).

Registers are only used for variables that require fast access , such as counters. It should also be noted that the definition of'register' does not mean that the variable will be stored in the register, it means that the variable may be stored in the register, depending on the hardware and implementation limitations.

  • static----instructs the compiler to maintain the existence of local variables during the life cycle of the program, without the need to create and destroy each time it enters and leaves the scope. Therefore, using static to modify local variables can maintain the value of local variables between function calls.

The static modifier can also be applied to global variables. When static modifies global variables, the scope of the variable is limited to the file in which it is declared.

A static variable or method declared globally can be called by any function or method, as long as these methods appear in the same file as the static variable or method.

  • extern----used to provide a reference to a global variable, which is visible to all program files.

When using extern, for variables that cannot be initialized, the variable name will point to a previously defined storage location.

When you have multiple files and define a global variable or function that can be used in other files, you can use extern in other files to get a reference to the defined variable or function. It can be understood that extern is used to declare a global variable or function in another file.

The extern modifier is usually used when two or more files share the same global variable or function

These specifiers are placed before the types they modify.

2. Operator

Operators are symbols that tell the compiler to perform specific mathematical or logical operations. The C language has a wealth of built-in operators, and provides the following types of operators:

  1. Arithmetic Operator
    Suppose the value of variable A is 10 and the value of variable B is 20
    Insert picture description here

  2. Relational operator
    Assume that the value of variable A is 10 and the value of variable B is 20
    Insert picture description here

  3. Logical operator
    Assume that the value of variable A is 1, and the value of variable B is 0
    Insert picture description here

  4. Bitwise operators
    Bitwise operators act on bits and perform operations bit by bit. The truth table of &, | and ^ is as follows:
    Insert picture description here

  5. Assignment operator
    Insert picture description here

  6. Other operators Operator
    Insert picture description here
    precedence in C The precedence of
    operators determines the combination of terms in an expression. This will affect how an expression is calculated.
    Operators are listed in descending order of operator precedence. Operators with higher precedence appear above the table, and operators with lower precedence appear below the table. In expressions, operators with higher precedence will be evaluated first.
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_46009608/article/details/110204456