当前位置:网站首页>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
边栏推荐
猜你喜欢

MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series

Fairygui bar subfamily (scroll bar, slider, progress bar)

Excel导入,导出功能实现

【无标题】

Problems and solutions of robust estimation in rtklib single point location spp
![[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组

The port is occupied because the service is not shut down normally

平衡二叉树详解 通俗易懂

Code example of MATLAB reading GNSS observation value o file

How to ensure data consistency between MySQL and redis?
随机推荐
RTKLIB: demo5 b34f.1 vs b33
记录:newInstance()过时的代替方法
Dark chain lock (lca+ difference on tree)
服务未正常关闭导致端口被占用
KF UD decomposition pseudo code implementation advanced [2]
第一人称视角的角色移动
Problems and solutions of robust estimation in rtklib single point location spp
Matlab读取GNSS 观测值o文件代码示例
KF UD分解之伪代码实现进阶篇【2】
What are the advantages of using SQL in Excel VBA
继承和多态(下)
图书管理系统小练习
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
How do architects draw system architecture blueprints?
初识C语言(上)
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
2022 National Games RE1 baby_ tree
地球围绕太阳转
Compile GDAL source code with nmake (win10, vs2022)
TYUT太原理工大学2022软工导论考试题型大纲