Getting to know C language for the first time (5) - Getting to know C language for the first time

"Scattered things get better, and in the end they will shine like a galaxy." Let's start learning keywords today!

11. Common keywords

In the previous article, I have introduced to you a brief introduction of the common keywords. Today we will briefly introduce several common keywords.
1. typedef

As the name suggests, typedef is a type definition, which is understood here as type renaming.

#include <stdio.h>
//typedef-类型重命名
typedef unsigned int uint;
typedef  int INT;
int main()
{
    
    
	//以下两种表达方式相同:
	unsigned int num1 = 10;
	uint num2 = 10;

	int num3 = 10;
	INT num4 = 10;
	return 0;
}

2.static

In C language, static is used to modify variables and functions.
1. Modified local variables - called static local variables
2. Modified global variables - called static global variables
3. Modified functions - called static functions

(1). Modify local variables

#include <stdio.h>
test()
{
    
    
	static int i = 1;//static修饰局部变量使得该局部变量为静态局部变量
	//一个普通的局部变量,进入函数创建,出函数销毁
	//但被static修饰后,进入函数时已经创建好,出函数的时候也不销毁,多次调用函数时,共享一个变量。
	//主观感受就是生命周期变长了,但是作用域不变,只能在局部范围内使用
	//普通局部变量放在栈区,而被static修饰后放在了静态区
	i++;
	printf("%d ", i);
}
int main()
{
    
    
	int j = 0;
	while (j < 5)
	{
    
    
		test();
		j = j + 1;
	}
	return 0;
}

insert image description here
(2). Modified global variables
Global variables have external link attributes, which determine that global variables can be used among multiple files.
insert image description here
Why doesn't the code in the picture above work properly? The answer is that each .c file is processed separately by the compiler, so how do we solve this problem? The keyword extern is used here, extern is a symbol declared from an external file, telling the compiler: year comes from other files.
insert image description here
Here, let's take a look at what happens if global variables are modified with static?
insert image description here
Here, we can find that an error is reported again. When static modifies the global variable, the external link attribute is changed to the internal link attribute, so that year can only be used in the current .c file. Gives us the feeling that the scope has changed.
(3). Modified function
Through the following code, we can find that the function also has the attribute of external link, which determines that the function can be used across functions.
insert image description here
But if we don't want the function to be used across files, we can modify the function with static at this time, so that the function can only be used in the .c file where it is located.
insert image description here

12. #define defines constants and macros

As mentioned earlier, constants are divided into literal constants, constant variables modified by const, identifier constants defined by #define, and enumeration constants. Let me introduce the identifier constants defined by #define:

#include <stdio.h>
//#define定义的标识符常量
#define M 100
#define C 'A'
int main()
{
    
    
	printf("%d\n", M);
	int a = M;
	printf("%d\n", a);
	printf("%c\n", C);
	return 0;
}

The identifier constant defined by #define can be understood as an alias for the constant. For example, in the above code, 100 is M and 'A' is C. When the program is running, the M in the code is changed back to 100, and C is then Switch back to 'A', which makes the procedure easier.

Next, let's take a look at the macro defined by #define :
Through the previous study, we know that if we want to use a function to find the sum of two numbers, we can write it like this:

#include <stdio.h>
int Add(int x, int y)
{
    
    
	return x + y;
}
int main()
{
    
    
	int a = 3;
	int b = 5;
	int c = Add(a, b);
	printf("%d\n", c);
	return 0;
}

insert image description here

So, how can we write the macro defined by #define?

#include <stdio.h>//宏是替换的作用
#define Add(x,y) (x+y) //#define 宏的名字(宏的参数)宏的内容
int main()
{
    
    
	int a = 3;
	int b = 5;
	int c = Add(a, b);
	printf("%d\n", c);
	return 0;
}

insert image description here
It can be seen that the results obtained by the two methods are the same. By comparison, it can be found that the parameters of the macro do not need to write types, and there are no curly braces in the function body, and the parameters do not have formal and actual parameters.

Thirteen. Pointer

13.1 Memory

To learn pointers well, first of all, let's understand memory together. So, what is memory? Memory is a particularly important memory on a computer. Programs in a computer run in memory. Therefore, in order to use memory more effectively, we divide memory into small memory units. The size of each memory unit is 1 byte . In order to be able to effectively access each unit of the memory, the memory unit is numbered, and these numbers are called the address of the memory unit . Addresses are also called pointers in C language.
insert image description here
Through debugging, we can find that a occupies 4 bytes, and each byte has its corresponding address, as shown in the figure below:
insert image description here

But when taking the address of a, only the smallest address will be accessed:
insert image description here

13.2 Pointer variables

Pointer variable : A variable used to store an address. (The value stored in the pointer is treated as an address)

#include <stdio.h>
int main()
{
    
    
	int a = 10;
	//&取地址操作符
	//这里就是将a的4个字节的第一个字节的地址存放在pa变量中。
	int * pa = &a;//数字,假设为0x0012ff48(起始位置的地址) -内存的编号==地址==指针,pa叫指针变量
	//*是在说明pa是指针变量,int是在说明pa指向一个int类型的变量
	*pa = 20;//*解引用操作符 - 通过地址找到地址所指的对象 *pa等价于a
	printf("%d\n", a);
	return 0;
}

insert image description here
Here we can find that pa is equivalent to a, so we can change the value of a by changing the value of pa.

Summary :
1. The memory will be divided into memory units in units of bytes.
2. Each memory unit has a number, number = address = pointer.
3. The variables created in the C language actually apply for a space in the memory, such as int a = 10; that is, apply for 4 bytes of space in the memory, and each byte has an address.
4. At the time of &a, the address (number) of the byte with the smaller address among the 4 bytes is taken out.
5. This address needs to be stored, and a variable is given. This variable is used to store the address (pointer), so it is called a pointer variable: int * pa = &a;
6. The address of a is stored in pa. The address finds a, and *pa is equivalent to a.

The same is true for character variables:

#include <stdio.h>
int main()
{
    
    
    char ch = 'b';
	char * pc = &ch;
	*pc = 'c';
	printf("%c\n", ch);
	return 0;
}

insert image description here

13.3 Size of Pointer Variables

#include <stdio.h>
int main()
{
    
    
    //指针变量的大小:
	char* pc;
	short* pv;
	int* pi;
	printf("%d\n", sizeof(pc));
	printf("%d\n", sizeof(pv));
	printf("%d\n", sizeof(pi));
	return 0;
}

insert image description here
From the running results, we can see that no matter what type of pointer variable, the size is 4 bytes. Since pointer variables are used to store addresses, the size of pointer variables depends on how much space is required to store an address. Therefore, the size of a pointer variable is 4 bytes on a 32-bit platform and 8 bytes on a 64-bit platform.
insert image description here

Fourteen. Structure

Here you can first use the structure to declare a student type to give you a preliminary understanding:

//用结构体声明了一个学生类型:
struct Stu
{
    
    
	char name[20];
	int age;
	float score;
};
#include <stdio.h>
int main()
{
    
    
	int num;
	struct Stu stu1 = {
    
    "张三",20,88.8f};
	struct Stu stu2 = {
    
    "李四",19,90.5f};
	struct Stu stu3 = {
    
    "王五",21,95.5f};
	printf("%s %d %f\n", stu1.name, stu1.age, stu1.score);
	printf("%s %d %f\n", stu2.name, stu2.age, stu2.score);
	printf("%s %d %f\n", stu3.name, stu3.age, stu3.score);
	return 0;
}

insert image description here

Similarly, we can use the pointers we learned above to optimize:

int main()
{
    
    
	int num;
	struct Stu stu1 = {
    
    "张三",20,88.8f};
	struct Stu stu2 = {
    
    "李四",19,90.5f};
	struct Stu stu3 = {
    
    "王五",21,95.5f};
	struct Stu* ps1 = &stu1;
	struct Stu* ps2 = &stu2;
	struct Stu* ps3 = &stu3;
	printf("%s %d %f\n", ps1->name, ps1->age, ps1->score);
	printf("%s %d %f\n", ps2->name, ps2->age, ps2->score);
	printf("%s %d %f\n", ps3->name, ps3->age, ps3->score);
	return 0;
}

insert image description here
Note : 1. Structure variable. Structure member
2. Structure pointer -> structure member

Alright, that’s all for today. Welcome to follow, like and comment!

Guess you like

Origin blog.csdn.net/qq_73121173/article/details/131622140