当前位置:网站首页>First acquaintance with C language (Part 2)
First acquaintance with C language (Part 2)
2022-07-06 13:07:00 【犇犇犇犇犇犇犇】
keyword
typedef
typedef unsigned int uint;
typedef struct Node
{
struct Node* next;
int data;
}Node;
int main()
{
unsigned int a = 0;
// When we write an unsigned integer unsigned int For a long
// We can't write well This is what we can use typedef simplify
uint num = 0;
struct Node n;
Node N;
return 0;
}
static( Static )
- Modify local variables
- Modify global variable
- Modify function
// Modify local variables
void test()
{
static int a = 1;
a++;
printf("%d ", a);
// Print 2~11
}
int main()
{
int a = 0;
while (a < 10)
{
test();
a++;
}
return 0;
}
Think about it without static What will be printed
I don't need code to demonstrate after modifying global variables and functions , Because you need to create another source file , Is that when static Modifying global variables and functions will turn their external link attributes into internal link attributes , That is, in another source file, we extern Less than their value , It can be understood that we have reduced their scope .
define( Not a keyword, but a preprocessing instruction )
//define Defining macro
//ADD Macro name (a,,b) Macro parameters Parameter has no type ((a)+(b)) Macrobody
#define ADD(a,b) ((a)+(b));
int main()
{
int a = 10;
int b = 20;
int c = ADD(a, b);// It's actually replacement , hold ADD(a,b) Replace with int c = ((a)+(b));
printf("%d\n", c);
return 0;
}
Another function is to define constants as mentioned above
#define M 100
int main()
{
int a = M;
int M = 0;// This is wrong
printf("%d \n", a);
printf("%d \n", M);
}
register( register )
int main()
{
// because cpu Now the speed of processing data is faster and faster So we need memory with high speed to read and write files
// Otherwise cpu No matter how fast it is , Registers will soon meet cpu
// Where computers store data From high to low - Slower and slower - More and more memory - The price is getting more and more expensive
// register
// Cache (cache)
// Memory
// Hard disk
//register It is recommended that the compiler put num = 3 Put the value of in the register , It is applicable to a large number of num Value
register num = 3;
return 0;
}
The pointer
When talking about pointers, we need to talk about memory first
Memory (Memory) Is an important part of a computer , Also called memory and main memory , It's for temporary storage CPU Operation data in , And data exchanged with external memory such as hard disk . It is external storage and CPU Bridge for communication , All programs in the computer run in memory , Memory performance affects the overall level of computer play . As soon as the computer starts running , The operating system will transfer the data to be calculated from memory CPU In the process of operation , When the operation is complete ,CPU Send the results out .
So memory is divided into memory units , Then number these memory units , The number is their address , And the pointer is the address
int main()
{
// Memory unit
// Number - Address - A pointer is an address
int num = 10;// Memory is 4 Bytes , Inside the store 10
// 0000 0000 0000 0000 0000 0000 0000 1010
// 0 0 0 0 0 0 0 10
// 0x0000000a
int* p = #// p Represents a pointer variable int Indicates that the address type of pointer variable storage is int
//* Represents the definition of a pointer variable & Is to put num Take out the number of this memory unit and give it to p
printf("%p\n", p);
printf("%d\n", *p);
//* Dereference operator It is through the stored address to find the value in this address
return 0;
}
The size of the pointer variable
int main()
{
printf("%d\n", sizeof(char*));
printf("%d\n", sizeof(int*));
printf("%d\n", sizeof(short*));
printf("%d\n", sizeof(long*));
printf("%d\n", sizeof(float*));
printf("%d\n", sizeof(double*));
// All output 4
return 0;
}
32 The computer of bit , The pointer variable size is 4 Bytes
64 The computer of bit , Pointer variable size bits 8 Bytes
Selection structure
int main()
{
int n = 0;
printf(" choice 1/0:");
scanf("%d",&n);
if (n == 0)
printf(" You gave up studying , I can only sell sweet potatoes \n");
else
printf(" You keep learning , You can find a good job \n");
return 0;
}
Loop structure
int main()
{
// The number of valid codes exceeds 2w That's ok You are Daniel
int line = 0;
while (line < 20000)
{
printf(" Still need to work hard to continue refuelling %d\n",line);
line++;
}
printf(" You are Daniel !\n");
return 0;
}
Structure
int ,float,char,long,short,double These are all C Language built-in data types , But when we want to express a complex object , These types don't seem to express well , This is our custom type struct Type of structure , Structure type is the method of combining these single types .
struct Stu
{
char name[20];
int age;
char sex[10];
char tele[12];
};
void test(struct Stu* ps)
{
// Structure pointer variable -> Member name
printf("%s %d %s %s\n", ps->name, ps->age, ps->sex, ps->tele);
}
int main()
{
struct Stu s = {
"zhangsan", 18, "man", "15833299302" };
printf("%s %d %s %s\n", s.name, s.age,s.sex,s.tele);
// Structural variable . Member name
struct Stu* p = &s;
test(&s);
printf("%s %d %s %s\n", (*p).name, (*p).age, (*p).sex, (*p).tele);
return 0;
}
Add :define It is a preprocessing instruction, not a keyword , We will explain in detail later when we talk about preprocessing instructions
extern Is to declare external symbols
边栏推荐
- RTKLIB: demo5 b34f.1 vs b33
- Record: newinstance() obsolete replacement method
- [算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
- What are the functions and features of helm or terrain
- Problems and solutions of robust estimation in rtklib single point location spp
- Containers and Devops: container based Devops delivery pipeline
- XV Function definition and call
- Itext 7 生成PDF总结
- Role movement in the first person perspective
- Record: Navicat premium can't connect to MySQL for the first time
猜你喜欢
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Implementation of Excel import and export functions
闇の連鎖(LCA+树上差分)
记录:初次cmd启动MySQL拒接访问之解决
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
NovAtel 板卡OEM617D配置步骤记录
抽象类和接口
Mixed use of fairygui button dynamics
[Chongqing Guangdong education] Shandong University College Physics reference materials
随机推荐
All in one 1405: sum and product of prime numbers
微信小程序开发心得
[GNSS data processing] Helmert variance component estimation analysis and code implementation
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
Record: the solution of MySQL denial of access when CMD starts for the first time
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
Containers and Devops: container based Devops delivery pipeline
The port is occupied because the service is not shut down normally
Record: newinstance() obsolete replacement method
2-year experience summary, tell you how to do a good job in project management
如何保障 MySQL 和 Redis 的数据一致性?
Exception: ioexception:stream closed
最短Hamilton路径 (状压DP)
国企秋招经验总结
[untitled]
【干货】提升RTK模糊度固定率的建议之周跳探测
PRIDE-PPPAR源码解析
记录:动态Web项目servlet访问数据库404错误之解决
平衡二叉树详解 通俗易懂
Music playback (toggle & playerprefs)