[C language basic study notes] 1. Initial C language (3)

Description: I am a beginner in C language. I don't understand many things very well, so I will type the code by myself to conduct corresponding reasoning and verification. (For example, Tucao: Why is this author like this? It’s so simple to have to type code to verify, it’s really good!… Me: Well, I’m not pretending anymore, I’m going to showdown, I’m really good, really good, what’s the matter? Beginner dishes are not allowed!)


EOF-- - end of file End of file flag value: - 1
Hint: This summary is the last small part of the initial C language, and then there will be an initial C language summary article, including the initial C language (1) (2) ( 3) Review the special mind map, some cases for programming practice, and personal learning experience and summary (for Xiaobai).

11. Common keywords--- - 32

insert image description here

Note: The variable name defined cannot be the same as the keyword.

Special instructions: regiter register cpu quick access register int a = 10;
struct structure keyword union union keyword
typedef type redefinition typedef unsigned int u_int;

Key details: static In C language, staic is used to modify variables and
functions
When the program ends, the life cycle ends.
insert image description here
insert image description here
When debugging with F10, you need to press F11 (Fn + F10, Fn + F11) when you need to enter the loop body or function body to
expand the description: static modification of local variables changes the life cycle of local variables (essentially changing the storage type of variables, from the stack area --> Static area)
insert image description here
2) Modified global variables - static global variables
A global variable is modified by static, so that this global variable can only be used in this source file, not in other source files. (Essentially changes the link attribute of the global variable, from the external link attribute to the internal link attribute)
insert image description here
insert image description here
After being modified with static
insert image description here
insert image description here
3) Modified function - static function
A function is modified by static, which changes the link attribute of the function, so that this function can only It is used in this source file and cannot be used in other source files. (Essentially changes the link property of the function, from external link property to internal link property)
insert image description here
insert image description here
After using the static modification function,
insert image description here
insert image description here
#define defines constants and macros
①#define defines identifier constants #define Max 100
②#define defines macros #define Max ( x, y) (x > y ? x : y)
insert image description here
expansion: what is a macro? Why are identifier constants and macros defined by #define said to be simple substitutions for characters?
① A macro in computer science is an abstraction that replaces certain text patterns according to a set of predefined rules. The interpreter or compiler will do this pattern substitution automatically when it encounters a macro;
② When we use #define Add(x,y) x+y to
calculate the 4 * Add(2,3) expression, guess what the result is, is it 4 * 5=20? In fact, it should be 4 * 2+3=11. If you understand this example, you should be able to understand why it is a simple replacement of characters!


12. Pointers

To understand the concept of pointers, we must first understand the meaning of memory.
insert image description here
Simple understanding: address is a numbering method for the effective use of memory space, which is similar to numbering space in our real life (for example, the world is divided into seven continents and China is divided into 34 provinces (including Taiwan Province), etc.), the variables that store these address information we give them a special type - pointer (a variable that stores address is essentially a variable, and a variable will have storage space and its corresponding address, that is to say, the pointer itself also has a corresponding address, although the content of the pointer itself also stores address information.), the size of the address is related to the numbering method, and the 32-bit platform has 32 address lines (physical lines in reality) , so 32 bits=4 bytes of address information can be stored, so the size of the address is 4 bytes, the 64-bit platform has 64 address lines, and the address size is 8 bytes.

% p print address
For a 32-bit platform, there are 32 address lines, so the number of bits corresponding to the address is 32 bytes, and the length is 4. Therefore, the length of the pointer for a 32-bit platform is 4 bytes.
For a 64-bit platform, there are 64 address lines, so the number of bits corresponding to the address is 64 bytes, and the length is 8, so the length of the pointer of the 64 -bit platform is 8 bytes
. variable name = & variable name; eg int a = 10; int* p = &a; //note that the pointer variable is named p, not *p char ch = 'a'; char* pc = &ch;
insert image description here

insert image description here
insert image description here




Use the pointer to find the corresponding variable dereference operator *
for example int a = 10;
int* p = &a;
*p = 20; //actually a = 20;


13. Structure

When describing some complex objects, we often have to describe them from multiple aspects and angles. At this time, we need to use the structure type. The structure type is a custom type, and its keyword is the struct
definition method. : struct structure type name
{ type structure member 1; type structure member 2; ... }



The way to initialize and define structure variables is:
struct structure type name structure variable name = { member 1 assignment, member 2 assignment... }

#include<stdio.h>
	//定义一个结构体类型
	struct Book
{
    
    
	char name[20];
	int price;
};
int main()
{
    
    
	//新建一个结构体Book类型的变量
	struct Book b1 = {
    
     "c语言程序设计",15 };
	printf("书名:%s\n", b1.name);
	printf("价格:%d\n", b1.price);
	return 0;
}

insert image description here

For structure variables, corresponding operations can also be performed, such as changing the value of its members, defining corresponding pointers, and so on.
insert image description here

#include<stdio.h>
//定义一个结构体类型
struct Book
{
    
    
	char name[20];
	int price;
};
int main()
{
    
    
	//新建一个结构体Book类型的变量
	struct Book b1 = {
    
     "c语言程序设计",15 };
	struct Book* pb = &b1;
	printf("书名:%s\n", b1.name);
	printf("价格:%d\n", b1.price);
	printf("书名:%s\n", (*pb).name);
	printf("价格:%d\n", (*pb).price);//. 结构体变量.成员
	printf("书名:%s\n", pb->name);// ->  结构体指针->成员
	printf("价格:%d\n", pb->price);
	b1.price = 20;
	printf("修改之后的价格为:%d\n", b1.price);
	pb->price = 25;
	printf("第二次修改之后的价格为:%d\n", b1.price);
	(*pb).price = 30;
	printf("第三次修改之后的价格为:%d\n", b1.price);

	return 0;
}

insert image description here

If you want to change the title of the book, it doesn't work to pass b1.name = { "c++" }, then why can b1.price change? This is because price is a variable and name is an array
. At this time, you can pass the strcpy() function To change the title of the book, strcpy-- string copy strcpy(b1.name, "c++"); the header file is string.h
insert image description here

Guess you like

Origin blog.csdn.net/QIYICat/article/details/115487259