content
static modifies global variables
Decorate a function with static
Introduction to the register keyword
#define define constants and macros
struct Stu is a structure type, which is a structure variable
keyword typedef
Typedef As the name implies, it is a type definition, which should be understood as a type renaming.
PS: Just like a person's nickname, typedef is to put a nickname for the variable type
Here, the int type is re-named a, that is, at this time a is equal to the int type, we can use a to create a variable and print it
#include<stdio.h>
int main()
{
typedef int a; //类型重命名
a i = 10; //新名字来定义变量
printf("%d", i);
return 0;
}
keyword static
In C language: static is used to modify variables and functions 1. Modified local variables - called static local variables
2. Decorate global variables - called static global variables
3. Decorated functions - called static functions
decorate local variables
Before static is not used
After using static
We found that after using static, the printed result is different from before. This is because before the static function is not used, every time i is printed, the space of i will be released, and it will be re-entered when it is entered again. Create a space for i and assign the initial value of i to 1, so 10 1s will be printed
Why can I print 1-10 after using static?
This is because: when static is used to modify local variables, the local variables will be placed in the static area. At this time, whenever i is printed, that is, after the function is executed, the i in the static area will not be released, but will continue here. Save it. When i is used again next time, i will still use the value of the last function as the initial value for the next operation.
The i before static is not used will be released immediately after each function execution ends, because this temporary variable i is created in the stack area, and is released as soon as the function exits, and after the static modification, i is placed in the static area, extending The life cycle of i is made so that the life cycle of i is almost as long as the life cycle of the main function
static modifies global variables
When we create a variable in a .c file and want to use it in another .c file, we need to add extern to declare it in the .c file to be used before it can be used
As follows:
Here we create variable i in test1.c, use variable i in test.c, and use extern in test.c to declare and use
After modifying external variables with static
We found that the program will report an error
At this point, the program cannot resolve the variable i from the outside, but it can be used normally when we use i in the test1.c file
This is because: after modifying the global variable with static, it will change the external link attribute of the local variable to the internal link attribute, which means that after modifying the global variable with static, when you want to use this variable in another .c file, will not be used.
Decorate a function with static
Before a .c file references a function of another .c file and static is not used
prints fine
After using static
Cannot print normally at this time
And after we use static in the .c file
You can print normally at this time
This is because: when static modifies an external function, it will cause the external function to lose external connectivity and maintain internal connectivity, so an error will be reported
Summarize:
When static modifies a local variable, the variable will be placed in the static area, and the memory used by the temporary variable will not be released after each use, and the life cycle will be delayed until the end of the program.
When static modifies global variables and functions, global variables and functions will lose external linkage, but maintain internal linkage, so when static modifies global variables and functions, they cannot be referenced in another .c file
Introduction to the register keyword
register keyword when registering
- The register keyword can only act on local variables, not global variables.
- The register keyword specifies to store local variables in registers.
- register is only a request to store this variable in the register, but it may not be able to request successfully
#define define constants and macros
Define identifier constants with define
Define macros with define
Note: macro parameters are untyped
pointer
RAM
The memory is a particularly important memory on the computer, and the operation of the programs in the computer is carried out in the memory. Therefore, in order to use the memory efficiently, the memory is divided into small memory units, and the size of each memory unit is 1 byte . In order to effectively access each unit of memory, the memory unit is numbered, and these numbers are called the address of the memory unit.
print the address of a
#include<stdio.h>
int main()
{
int a=10; //定义变量a,开辟四个字节的空间
int *c=&a; //定义指针变量c,并将a的地址放入其中
printf("%d",*c); // 打印c地址中所1存放的数值
printf("%p",c) //打印c的地址
return 0;
}
&a meaning: take out the address of a, & is the address symbol
Define a variable of type int * to assign the number stored in the address of a to the variable c of type int *,
The meaning of int * c=&a, int indicates that the object pointed to by c is of type int , * indicates that p is a pointer variable ,
No matter what type of pointer it is, it is creating a pointer variable, such as char *a, which is a pointer variable
Pointer variables are used to store addresses
The size of the pointer variable depends on how much space is needed when an address is stored
size of pointer variable
The size of the pointer variable is related to the number of operands in the system.
Note: *c refers to the variable stored in the address where the variable c of type int * is located. c refers to the address pointed to by a pointer variable of type int*.
structure
Structure is a particularly important knowledge point in C language. Structure makes C language capable of describing complex types. For example, when describing a student, the student includes: name + age + gender + student number. Only structures can be used to describe them here.
define structure
Here the phone uses 12 spaces because the \0 in the string also occupies a space
#include <stdio.h>
struct Stu
{
char name[100]; //姓名
int age; //年龄
char sex[10]; //性别
char tele[12]; //电话
};
struct Stu is a structure type, which is a structure variable
assign value to structure
Assignment method: assign corresponding values in order according to the variable types in the defined structure.
struct Stu a is to define a variable aa is a structure type struct Stu
reference structure variable
Direct reference: add one more structure variable a. Then select the corresponding structure variable
Reference as a pointer: use the structure name -> apply