Introduction to C language - keywords - operators - pointers - structures

Table of contents

1. Keywords

1.1 typedef (type redefinition)

1.2 static

2. Operators

2.1 Conditional Operators

2.2 Comma expressions

3. pointer

4. Structure


1. Keywords

1.1 typedef (type redefinition)

Look at the following code: typedef can rename a type. To give an inappropriate example, int can be customized as ZX, so that the integer type has two names, one is int and the other is ZX, and integer variables can be declared .

This usage is mostly used in complex variables:

typedef struct Node {
	int data;
	int price
}Node;
int main()
{
	//重定义后,我们声明变量的工作量就变小了!
	struct Node b = { 11,15 };
	Node a = { 11,55 };
}

1.2 static

static can be modified: local variables, global variables, functions

1.2.1 Modify local variables

First look at the result of a simple code run:

Static modification of the local variable a changes the storage type of the variable. Originally, local variables were placed in the stack area, and after being modified by static , they were stored in the static area of ​​memory. Because of the change of storage type, the life cycle became longer. But it does not affect the scope , a can only be used inside the function.

1.2.2 Modifying global variables

Global variables have external link attributes, and after static modification of global variables, the external link attributes become internal link attributes. At this time, the global variable can only be used in the .c file where it is located, and other files cannot be linked and used.

1.2.3 Modified functions

The function has an external link attribute. After the function is statically modified, the external link attribute is changed to an internal link attribute, so that this function can only be used in the .c file where it is located, and other .c files cannot be linked.

2. Operators

2.1 Conditional Operators

        The conditional operator is also called the ternary operator: exp1?exp2:exp3

If the result of the exp1 expression is true, output the value of the exp2 expression, otherwise output the value of the exp3 expression.

Good use of the ternary operator can simplify the code logic.

2.2 Comma expressions

(exp1, exp2, exp3, exp4) are evaluated from left to right, and the result is the result of the last expression.

3. pointer

A pointer is an address, and an address is a pointer

The most basic usage of pointers is as follows:

4. Structure

When we modify complex data types, we can consider using structures, such as: describing a person's name and age.

Note: *pa is equivalent to a.

Guess you like

Origin blog.csdn.net/weixin_45153969/article/details/128582125