当前位置:网站首页>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
边栏推荐
- Answer to "software testing" exercise: Chapter 1
- [算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
- 初识C语言(下)
- Usage differences between isempty and isblank
- Liste des boucles de l'interface graphique de défaillance
- 系统设计学习(三)Design Amazon‘s sales rank by category feature
- 10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
- Detailed explanation of balanced binary tree is easy to understand
- 堆排序【手写小根堆】
- What are the functions and features of helm or terrain
猜你喜欢
RTKLIB: demo5 b34f.1 vs b33
Excel导入,导出功能实现
系统设计学习(三)Design Amazon‘s sales rank by category feature
[untitled]
Heap sort [handwritten small root heap]
编辑距离(多源BFS)
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
2022国赛Re1 baby_tree
随机推荐
Dark chain lock (lca+ difference on tree)
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
XV Function definition and call
【无标题】
阿里云微服务(三)Sentinel开源流控熔断降级组件
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
系统设计学习(一)Design Pastebin.com (or Bit.ly)
几道高频的JVM面试题
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
最短Hamilton路径 (状压DP)
[rtklib 2.4.3 B34] version update introduction I
阿里云微服务(四) Service Mesh综述以及实例Istio
2022 National Games RE1 baby_ tree
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
GPS高程拟合抗差中误差的求取代码实现
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
音乐播放(Toggle && PlayerPrefs)
基于rtklib源码进行片上移植的思路分享
One article to get UDP and TCP high-frequency interview questions!
121道分布式面试题和答案