3.29 notes

Array: The elements in the array are of the same type.
When using an array, the array must be defined and then initialized. It is []not necessary to write it during initialization . If it is written, it must be a constant instead of a variable. It can be sizeof(arr) / sizeof(arr[0]);used when finding the length of an array: to find, generally the latter is arr[0], this can also avoid the solution error when there is only one element in the array. Accessing each element of the array is called traversal.
Operator:

  1. Arithmetic operation marks:+ - * / %(求余运算符)
  2. Shift operators:, >>和<<shift right and shift left (by binary shift). Such as:
	int x = 10;              //二进制为1010
	printf("%d\n", x >> 1); //x向右移1个单位,二进制为0101,此时结果为5
	printf("%d\n", x << 1); //x向左移一个单位,二进制为10100,此时结果为20
	printf("%d\n", x);  //对x移位,但未改变其本身的值

The result is:
Insert picture description here
shifting the integer to the right by 1 bit is equivalent to dividing the integer by 2 and rounding.

  1. Bit operator:
	&    按位与运算,1&1=1 1&0=0 0&1=0 0&0=0
	|    按位或运算,1|1=1 1|0=1 0|1=1 0|0=0
	^    按位异或运算,相同为0,相异为1
  1. Assignment operator: = += -= *= /= &= ^= |= >>= <<=
    define variables:
    ①Open up the address space, the size is determined by the type.
    ②Place the data content in the space.
    The difference between initialization and assignment:
    Initialization: When defining a variable, give a value.
    Assignment: Give another value to an existing variable.
    5. Monocular operator:
	!             逻辑反操作
	-             负值
	+             正值
	&             取地址
	sizeof        操作数的类型长度
	~             对一个数的二进制按位取反
	--            前置--,后置--
	++            前置++,后置++
	*             间接访问操作符(解引用操作符)
	(类型)        强制类型转换

The difference between ++i and i++: i++ is to execute the program first and then increment. And ++i is to execute the program after self-increment. Such as:

	int i = 10;
	int x = 0;
	x = i++;
	printf("%d %d\n", i, x);

The result is:
Insert picture description here

	int i = 10;
	int x = 0;
	x = ++i;
	printf("%d %d\n", i, x);

The result is:
Insert picture description here

6. Relational operator:

>
>=
<
<=
!=
==

7. Logical operators: && and ||
logical AND and logical OR are the results when two expressions are judged true or false.
In logical AND, two expressions must be true at the same time to be true. When the first expression is false, the following expressions are not executed, because the first one is false, the result must be false, so Will continue to execute.
In logical AND, an expression is true if it is true, but when the first expression is true, the following expressions will not be executed, because the first one is true, the result must be true. Will continue to execute. Such as the logic and test:

	int x = 1;
	x && printf("%d\n", x);

The result is:

	int x = 0;
	x && printf("%d\n", x);

The result is:
Insert picture description here
for logical OR test:

	int x = 0;
	x || printf("%d\n", x);

The result is:
Insert picture description here

int x = 1;
x || printf("%d\n", x);

The result is:
Insert picture description here

  1. Conditional operator (ternary operator): exp1? exp2: exp3
    such as seeking a maximum value:
int x = 10;
int y = 20;
printf("%d\n", x > y ? x : y);

If the condition is met, the former will be output, and if the condition is not met, the latter will be output.
The result is:
Insert picture description here
Keywords: There are 32 keywords defined in the c language:

auto                       声明自动变量
break                      跳出当前循环
case                       开关语句分支
char                       声明字符型变量
const                      声明只读常变量
continue                   结束当前循环并开启下一轮循环
default                    开关语句中的默认分支
do                         循环语句的循环体
double                     声明双精度变量
else                       条件分支语句的分支
enum                       声明枚举常量
extern                     声明变量是从其他文件中引用来的
float                      声明浮点型变量
for                        循环语句
goto                       无条件跳转语句
if                         条件语句
int                        声明整形变量
long                       声明长整型变量
register                   声明寄存器变量
return                     子程序返回语句(参数可有可无)
short                      声明短整型变量
signed                     声明有符号类型变量
sizeof                     计算对象所占内存空间大小(字节)
static                     声明静态变量
struct                     声明结构体变量
switch                     开关语句
typedef                    给数据类型取别名
union                      声明联合体(联合数据)变量
unsigned                   声明无符号类型变量
void                       声明函数无返回值或无参数,声明空类型指针
volatile                   说明变量在程序执行中可能会被隐式地改变
while                      循环语句

1.auto: Under normal circumstances, it can be ignored, and it is not very common in normal times, (uncommon is just that we can't see it, which does not mean that it is not commonly used). As a process-oriented language, C language will inevitably appear many function blocks. The variables declared in each function module are local variables, which also have another name: automatic variables.
As in a function:

int x=10;
auto int x=10;这两者是相等的

The appearance of auto means that the current variable is a local variable and will be allocated on the memory stack (I don't understand this yet), so as to use memory more efficiently.

2. const : Variables modified by const are called constants.
This sentence is not accurate. This value cannot be used at compile time, because the compiler at this time does not know what it stores. So there is still a small difference from constants.
"Read-only variable": Open a place in the memory to store its value, but this value is limited by the compiler and cannot be modified. (A value during compilation), without storage and memory operations, efficiency becomes very high.
const modified variables may be called read-only variables.

const int n=5int arr[n]

This string of codes cannot run in C language, which proves that n is not a "constant", because the length of an array defined in C language must be "constant", "read-only variable" is also not acceptable, and "constant" does not mean "unchangeable" variable". In C++, local arrays can use variables as their length, and the above code string can run.

const int *p;       p的指向可以改变,但p指向的目标不能改变
int const *p;       p的指向可以改变,但p指向的目标不能改变
int *const p;       p的指向不可以改变,但是指针p指向的目标可以改变。
const int *const p; p的指向和p指向的目标都不可改变。

3. extern: extern can be placed before variables and functions to indicate that this function or variable may come from another file, and is quoted here. There is a special case, if the line of code before the static global variable wants to use this variable, it must also be quoted using extern.

4. Enum enumeration type: the constant in enum can be assigned, but if you don’t assign it, it will be incremented by 1 from the constant to which the initial value is assigned. If no value is assigned, the first value At the beginning, 0 is automatically assigned, and each variable is incremented by 1.
5.goto: It is recommended to disable. Although it can jump flexibly, it obviously destroys the structured design style.

6.register: The variable modified by register is called a register variable. As the name implies, the variable is directly stored in the internal register of the CPU. The access speed and the data in the memory must be different. For specific application scenarios, for example, there is a large loop, in which there are several variables that need to be manipulated frequently. At this time, using register for modification will greatly improve efficiency.
But even if it is as strong as register, it has its own limitations. It must be the type accepted by the register to be modified, that is, the register variable must be a single value, and its length must be less than or equal to the length of the integer. Although the register variable is not always in the register all the time, it is finally written into the memory, but we cannot guarantee that it will be in the memory at a certain moment, so we cannot take the address of the register variable.

7. Signed and unsigned: Their Chinese names are signed type and unsigned type. It involves the positive and negative problems of variables. We all know that the highest bit of a variable is the sign bit, and the unsigned type uses this highest bit to represent data, so the unsigned type starts from 0 to a certain positive number. The signed type is similar to auto, and we don't usually see it, but by default, a variable is considered to be of the signed type.

8. sizeof : sizeof is not a function, it is a keyword.
The parentheses can be removed when sizeof calculates the size of the space occupied by the variable, but it cannot be omitted when calculating the size of the type, otherwise an error will occur. It is customary to add parentheses.

9.static: The static keyword static has two main functions in the C language:
Function 1: Modification of variables
Variables are divided into local variables and global variables. After modification, they correspond to two kinds of static variables. They are all stored in a static area of ​​memory.
Static local variable:
The static local variable defined in the function can only be used by this function, and other functions cannot be used. At the same time, since it and the static global variable exist in the static area of ​​the memory instead of the stack, the value will not be destroyed after the function ends. The function will continue to use this value when it runs next time, which is a very important feature. The life cycle is modified, but the scope has not changed.
Static global variables:
The scope of this type of variable is limited to the defined file (from the point of definition to the end of the current file), and it cannot be used in other files even if the extern keyword is used. In the current file, the line of code before the static global variable definition can also not be used directly, and the extern keyword can be used. So generally we define static global variables at the top of the file. But it can be accessed indirectly. If a function in one file uses the static global variable, another file can call this function to achieve indirect access.
Role 2: Modified function
Adding static before the function makes the function a static function. At this time, the scope of the function is limited to this file, so it becomes an internal function. The advantage of this is that when different people write different functions, you don't have to worry about your own function with the same name written by others.
(Not complete, will be updated later).
Global variables and functions support cross-file access.

10. typedef: give an alias to an existing data type (not a variable). Its main use is that after taking the alias, we can understand the meaning of this data type more clearly at a glance. The common use case is when the structure is defined.

11.void: If you define a function in the C language, do not give the return type.
For example:
add (int a, int b)
{ return a+b; } What is the result of doing this? Is the return value void? Or does it fail to compile at all? The answer is that the compilation can pass, but the return value type is not void but int as you think. There is another question: If you define a variable of type void, how much memory will the compiler allocate to it? (Question, more later)




12.typedef: Type renaming, generally not applicable unless there are special requirements.

For the keyword part of this article, refer to this blog: https://blog.csdn.net/czc1997/article/details/80766138

Guess you like

Origin blog.csdn.net/w903414/article/details/105178904