C language-global variables, static local variables, macro definitions

Enrich the third 8 hours of the day!

It turns out that most of the variables I have seen before are local variables defined in the function (it only appears when entering the function, and it does not exist after the function is out)

Global variable

1. Variables defined outside the function-global variables

1>The lifetime and scope are global!

2>All functions can be used and accessed.

2. Initialization of global variables:

1>The compiler will automatically assign a value of 0 to global variables that are not initialized. (The local variable is a random value!)

2>A global pointer that is not initialized will get a NULL value.

3>Only use known values ​​at compile time to initialize global variables

#include <stdio.h>

int gall = 1;//定义了一个全局变量gall 值为1

int gall2 = gall;//这句不行!!!是错误的,因为编译时gall算是未知量


int main(void)
{
    ......
    return 0;
}

4> Its initialization occurs before the main() function

5>Don't use global variables. There are some assignments, operations, etc. between them. Because we may have file calls, there may be confusion (order or something)

For example, before main:

const int gall = 12;//After adding const here, the value of gall is known at compile time

int gall2 = gall;

do not do that! ! ! !

2. If there is a variable with the same name as the global variable inside the function, the global variable will be hidden inside the function, and the global variable will reappear after coming out of the function.

It can be understood this way: Redefining variables that have appeared in larger places in a smaller place will hide them.

Static local variables

 1. Adding a static before the local variable will become a static local variable

2. The variable that leaves the function static will retain its value, and the next time you enter the function will still be the last value, and will not be reinitialized (it can be understood that its initialization is a one-time)

3. Static local variables are actually global variables (you can customize a global variable, a static local variable, and a local variable, and then output their address, you will find that the address of the global variable is very close to the address of the static local variable, but is similar to the local variable The address is so bad)

4. Static local variables have a global lifetime, but are local scope.

Points to note when using global variables

1. For a function that returns a pointer

1>Returning the address of a local variable is dangerous, because the local variable does not exist after it leaves the function

2>It is legal to return the addresses of global variables and static local variables

2. Don't use global variables to pass parameters and results between functions! !

3. Try to avoid using global variables

4. Functions that use global variables and static local variables are thread-unsafe (you don’t need to understand for the time being)

 

Macro

1. Compile preprocessing instructions:

#......

1>The # sign at the beginning is to compile preprocessing instructions

2>Compilation and processing instructions are not available; semicolon (because it is not a component of C language, so understand it/dog head, but C language cannot do without it, and it is not a keyword of C language than the attached include)

3> #define defines the macro (the older c is not const, the macro definition is used to define the definition)

# define PI 3.14159

# define <name> <value>//The value can have spaces, characters, etc.

4>There will be a pre-processing step before compilation

5> If the macro definition exceeds one line, use \ backslash

# define PRINT1 printf("hong");\
                printf("hong");//这里的分号是PRINT常量里的!!!

6>Macro definitions can have no value and are often used as conditional compilation

7>There are some predefined macros

_FILE_
_LINE_
_DATE_
_TIME_

Macro with parameters

1. Macros like functions

#define cube(x) ((x)*(x)*(x))

In this way, you can have: printf("%d",cube(5));

In fact, the output is ((5)*(5)*(5))

2. It should be noted that:

Everything has parentheses! :

Parentheses must be placed wherever the parameter appears

The entire value (formula) must have parentheses

3. You can take multiple parameters (remember to have parentheses in the parameters!)

4. Don't add semicolons! ! !

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/115311132
Recommended