[C language - first understanding of C language (4)]


foreword

Continuing the above content, this article is still the preparatory knowledge of the basic content of C language.


11. Common keywords

insert image description here
The above are common keywords, first introduce the following keywords:

11.1 Keyword typedef

As the name implies, typedef is a type definition, which should be understood as type renaming. For example, renaming unsigned int to uint_32, so uint_32 is also a type name. Observe num1 and num2, the types of these two variables are the same.

//将unsigned int 重命名为uint_32, 所以uint_32也是一个类型名
typedef unsigned int uint_32;
int main()
{
    
    
	//观察num1和num2,这两个变量的类型是一样的
	unsigned int num1 = 0;
	uint_32 num2 = 0;
	return 0;
}

11.2 The keyword static

In the C language, static is used to modify variables and functions, and there are three usages:

  1. Decorated local variables - called static local variables
  2. Decorated global variables - called static global variables
  3. Decorated function - called static function

11.2.1 Decorating local variables

Static modifies local variables to extend the action period:

Compare the effect of code 1 and code 2 to understand the meaning of static modification of local variables.
Code 1 prints : 1 1 1 1 1 1 1 1 1 1
Code 2 prints: 1 2 3 4 5 6 7 8 9 10

In code 1, the local variable i is in the test() function. When the function ends, the value of i is gone, and the memory space for the variable i is released. When the function test() is called next time, the variable i will be re-initialized and assigned a value of 0.
Its scope of action: in the test() function, the function value is destroyed; the
action period: , the function starts to be called to the end of the function call.

In code 2, the static modification of the local variable i, in the test() function, when the function ends, the value of i still exists, and the memory space for the variable i is not released. When the function test() is called next time, the static local variable i does not need to be reassigned to 0. This code has expired, and the variable i will retain the last one, and the statement will be executed on this basis.
Its scope of action: out of the test() function, the value still exists;
action period: , at the end of the entire project.

//代码1
#include <stdio.h>
void test()
{
    
    
	int i = 0;
	i++;
	printf("%d ", i);//输出10个1
}
int main()
{
    
    
	int i = 0;
	for(i=0; i<10; i++)
	{
    
    
		test();
	}
	return 0;
}
//代码2
void test()
{
    
    
	//static修饰局部变量
	static int i = 0;
	i++;
	printf("%d ", i);
}
int main()
{
    
    
	int i = 0;
	for(i=0; i<10; i++)
	{
    
    
		test();//输出1-10
	}
	return 0;
}

Conclusion:
The static modification of local variables changes the life cycle of the variables,
so that the static local variables still exist out of scope, and the life cycle does not end until the end of the program.

11.2.2 Decorating global variables

Code 1 is normal, code 2 will have a connectivity error when compiling.

Conclusion:
A global variable is modified by static, so that this global variable can only be used in this source file and cannot be used in other source files.

//代码1
//add.c
int g_val = 2018;//函数定义在add.c的源文件中

//test.c
int main()//主函数在test.c的源文件中
{
    
    
	printf("%d\n", g_val);
	return 0;
}

//代码2
//add.c
static int g_val = 2018;//函数定义在add.c的源文件中

//test.c
int main()//主函数在test.c的源文件中
{
    
    
	printf("%d\n", g_val);
	return 0;
}
//代码3
//extern 是用来声明外部符号的
extern int global;//在add源文件中定义的全局变量可以在test中使用

int main()
{
    
    
	printf("%d\n", global);
	return 0;
}

11.2.3 Decorated functions

Code 1 is normal, and code 2 will have a connectivity error when compiling.
Conclusion:
A function is modified by static, so that this function can only be used in this source file and cannot be used in other source files.

//代码1
//add.c
int Add(int x, int y)
{
    
    
	return c+y;
}
//test.c
int main()
{
    
    
	printf("%d\n", Add(2, 3));
	return 0;
}

//代码2
//add.c
static int Add(int x, int y)//只能在本源文件中使用
{
    
    
	return c+y;
}
//test.c
int main()
{
    
    
	printf("%d\n", Add(2, 3));
	return 0;
}
//代码3
//将在其他源文件定义的函数,经过声明以后可以在本源文件中直接使用
extern int add(int x, int y);

int main()
{
    
    
	int num1 = 9;
	int num2 = 5;
	int sum = 0;

	sum = add(num1, num2);
	printf("%d\n", sum);
	return 0;
}

12. #define defines constants and macros

Macro definition, the name is generally all uppercase

//define定义标识符常量
#define MAX 1000
#define STR "hello bit"//字符串直接用双引号

//define定义宏
#define ADD(x, y) ((x)+(y))

#include <stdio.h>
int main()
{
    
    
	int sum = ADD(2, 3);
	printf("sum = %d\n", sum);
	sum = 10*ADD(2, 3);
	printf("sum = %d\n", sum);
	return 0;
}

13. Pointer

13.1 Memory

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:

insert image description here
Variables are created in memory (space is allocated in memory), and each memory unit has an address, so variables also have addresses. Take out the variable address as follows:

int main()
{
    
    
	int num = 10;
	&num;//取出num的地址
	//这里num的4个字节,每个字节都有地址,取出的是第一个字节的地址(较小的地址)
	printf("%p\n", &num);//打印地址,%p是以地址的形式打印
	return 0;
}

insert image description here
How to store the address, you need to define a pointer variable, and the pointer is the address .
The memory unit has a number, this number is the address, and we call this address a pointer.

//内存单元都有编号,这个编号就是地址,我们把这个地址也叫指针
int main()
{
    
    
	int a = 10;
	//%p取地址操作,存放变量a数值的地址
	printf("%p\n", &a);
	return 0;
}
int num = 10;
int *p;//p为一个整形指针变量
p = &num;//地址传给指针p,  

#include <stdio.h>
int main()
{
    
    
	int num = 10;
	//pa是用来存放地址的变量,所以pa为指针变量
	int *p = &num;
	//*是解引用操作,*pa,就是将pa存放的地址所对应的值取出来
	//*pa原来对应的值是10,现在重新赋值,就是20了
	*p = 20;//重新给地址存放的变量赋值
	printf("%d\n", a);
	return 0;
}

insert image description here
Examples of characters using pointers:

int main()
{
    
    
	char ch = 'w';
	char* pc = &ch;
	*pc = 'q';
	printf("%c\n", ch);
	return 0;
}

13.2 Size of pointer variables

The size of the pointer variable depends on the size of the address. The address
under the 32-bit platform is 32 bits (that is, 4 bytes)
, and the address under the 64-bit platform is 64 bits (that is, 8 bytes)

//指针变量的大小取决于地址的大小
//32位平台下地址是32个bit位(即4个字节)
//64位平台下地址是64个bit位(即8个字节)
int main()
{
    
    
	printf("%d\n", sizeof(char *));
	printf("%d\n", sizeof(short *));
	printf("%d\n", sizeof(int *));
	printf("%d\n", sizeof(double *));
	return 0;
}

In the following example, the address of a character occupies 4 bytes, the integer array is also 4 bytes, and both output results are 4.

The pointer size is a fixed 4 bytes . The size of the address has nothing to do with the numeric type stored in the address

int main()
{
    
    
	char ch = 'w'; //0x0012ff48
	int a = 10;//0x0012ff40

	int * pa = &a;
	char * pc = &ch;
	
	printf("%d\n", sizeof(pa));//地址占4个字节
	printf("%d\n", sizeof(pc));	
	//两个输出结果都是4,指针大小就是固定的4个字节
	//大小和地址存放的数值类型没有任何关系
	//32位平台就是x86, 指针大小是4个字节
	//64位平台就是x64, 指针大小是4个字节
	return 0;
}

14. Structure

Operator -> is the structure member access operator, the usage is as follows:
structure pointer -> structure member
operator. It is the structure member access operator, the usage is as follows:
structure variable.structure member

//结构体变量
struct stu
{
    
    
	char name[20];
	int age;
	char sex[5];
	char id[20];
};

int main()
{
    
    
	struct stu s = {
    
     "张三",20,"男", "20180101" };
	struct stu s2 = {
    
     "李四",19,"女", "20180102" };
	
	//printf("name=%s age=%d sex=%s id=%s\n", s.name, s.age, s.sex, s.id);
	printf("%s %d %s %s\n", s.name, s.age, s.sex, s.id);
	
	struct stu *ps = &s2;
	printf("%s %d %s %s\n", ps->name, ps->age, ps->sex, ps->id);
	return 0;
}

Use typedef to shorten the name of the structure:
stu is struct stu

typedef struct stu
{
    
    
	char name[20];
	int age;
	char sex[5];
	char id[20];
}stu;

void print(struct stu* ps)//结构体类型的指针变量
{
    
    
	//printf("%s %d %s %s\n", (*s3).name,(*s3).age, (*s3).sex, (*s3).id);
	printf("%s %d %s %s\n", ps->name, ps->age, ps->sex, ps->id);
	//-> 结构成员访问操作符
	//结构体指针->结构体成员
};

int main()
{
    
    
	//结构体的初始化
	struct stu s1 = {
    
     "张三",20,"男", "20180101" };
	struct stu s2 = {
    
     "周六",21,"女", "20180103" };
	stu s3 = {
    
     0 };//这是使用typedef的效果
	//stu就是struct stu
	
	输入结构体数据    name是数组名,就是首地址,age是整数,必须取地址
	scanf("%s %d %s %s", s1.name, &(s1.age), s1.sex, s1.id);
	//直接打印的
	printf("%s %d %s %s\n", s1.name, s1.age, s1.sex, s1.id);
	//. 结构成员访问操作符
	结构体变量.结构体成员

	//使用函数打印
	print(&s2);//传入地址,则函数定义必须使用指针了

	return 0;
}

Summarize

This article is the last point of the initial C language series. At present, it is a quick preview of most of the content of the C language. It can be regarded as the preparatory knowledge of the basic content of the C language. Next, we will enter the basic stage of the C language. The blog update content completely refers to the learning video of station B.

After that, it is followed by the advanced stage of C language learning, and finally the primary data structure and algorithm content. This completes the C language stage. Follow-up will follow to learn C++.

Guess you like

Origin blog.csdn.net/taibudong1991/article/details/123714121