当前位置:网站首页>初识C语言(下)
初识C语言(下)
2022-07-06 09:19:00 【犇犇犇犇犇犇】
关键字
typedef
typedef unsigned int uint;
typedef struct Node
{
struct Node* next;
int data;
}Node;
int main()
{
unsigned int a = 0;
//当我们书写一个无符号的整型时 unsigned int 很长
//我们不好书写 这是我们可以使用过typedef 简化
uint num = 0;
struct Node n;
Node N;
return 0;
}
static(静态的)
- 修饰局部变量
- 修饰全局变量
- 修饰函数
// 修饰局部变量
void test()
{
static int a = 1;
a++;
printf("%d ", a);
//打印2~11
}
int main()
{
int a = 0;
while (a < 10)
{
test();
a++;
}
return 0;
}
可以思考一下不加static 会打印什么
后面修饰全局变量和函数我就不用代码演示了,因为需要创建另一个源文件,就是当static修饰全局变量和函数时会让他们的外部链接属性转变为内部链接属性,也就是在另一个源文件中我们extern不到它们的值,可以这样理解就是我们降低了它们的作用域。
define(不是关键字是一种预处理指令)
//define 定义宏
//ADD 宏名 (a,,b)宏的参数 参数无类型 ((a)+(b)) 宏体
#define ADD(a,b) ((a)+(b));
int main()
{
int a = 10;
int b = 20;
int c = ADD(a, b);//其实就是替换,把ADD(a,b)替换成 int c = ((a)+(b));
printf("%d\n", c);
return 0;
}
还有的作用就是前面所说的定义常量
#define M 100
int main()
{
int a = M;
int M = 0;//这是错误的
printf("%d \n", a);
printf("%d \n", M);
}
register(寄存器)
int main()
{
//因为cpu现在处理数据的速度越来越快 所以需要读写文件速度快的内存
//不然cpu再快也没有用,寄存器就很快可以满足cpu
//电脑存储数据的地方 由高到低-速度越来越慢-内存越来越大-价格越来越贵
// 寄存器
// 高速缓存(cache)
// 内存
// 硬盘
//register 是建议编译器把num = 3的值放在寄存器中,适用于大量要用到num的值
register num = 3;
return 0;
}
指针
讲指针就需要先讲讲内存
内存(Memory)是计算机的重要部件,也称内存储器和主存储器,它用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换的数据。它是外存与CPU进行沟通的桥梁,计算机中所有程序的运行都在内存中进行,内存性能的强弱影响计算机整体发挥的水平。只要计算机开始运行,操作系统就会把需要运算的数据从内存调到CPU中进行运算,当运算完成,CPU将结果传送出来。
所以内存就被划分为一个一个内存单位,然后又对这些内存单位进行编号,编号也就是它们的地址,而指针就是地址
int main()
{
//内存单元
//编号 - 地址 - 指针也就是地址
int num = 10;//内存是4个字节,里面存储10
// 0000 0000 0000 0000 0000 0000 0000 1010
// 0 0 0 0 0 0 0 10
// 0x0000000a
int* p = #// p表示指针变量 int表示指针变量存储的地址类型为int
//* 表示定义指针变量 &就是把num这个内存单元的编号取出来给p
printf("%p\n", p);
printf("%d\n", *p);
//*是解引用操作符 就是通过存储的这个地址找到这个地址里面的值
return 0;
}
指针变量的大小
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*));
//全部输出4
return 0;
}
32位的电脑,指针变量大小为4个字节
64位的电脑,指针变量大小位8个字节
选择结构
int main()
{
int n = 0;
printf("选择1/0:");
scanf("%d",&n);
if (n == 0)
printf("你放弃学习了,只能去卖红薯\n");
else
printf("你坚持学习了,可以找到一份好工作\n");
return 0;
}
循环结构
int main()
{
//有效代码量超过2w行 你就是大牛
int line = 0;
while (line < 20000)
{
printf("仍需努力继续加油%d\n",line);
line++;
}
printf("你是大牛!\n");
return 0;
}
结构体
int ,float,char,long,short,double这些都是C语言内置的数据类型,但是当我们要表达一个复杂对象时,这些类型似乎不能够很好的表达,这是我们就自定义一个类型struct结构体类型,结构体类型是把这些单一的类型组合在一起的做法。
struct Stu
{
char name[20];
int age;
char sex[10];
char tele[12];
};
void test(struct Stu* ps)
{
//结构体指针变量->成员名
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);
//结构体变量.成员名
struct Stu* p = &s;
test(&s);
printf("%s %d %s %s\n", (*p).name, (*p).age, (*p).sex, (*p).tele);
return 0;
}
补充:define 是一个预处理指令不是关键字,后面讲到预处理指令时再做详细讲解
extern是声明外部符号的
边栏推荐
- KF UD分解之伪代码实现进阶篇【2】
- VLSM variable length subnet mask partition tips
- 【无标题】
- 服务未正常关闭导致端口被占用
- [untitled]
- On March 15, the official version of go 1.18 was released to learn about the latest features and usage
- 2022 National Games RE1 baby_ tree
- 基于rtklib源码进行片上移植的思路分享
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- 【RTKLIB 2.4.3 b34 】版本更新简介一
猜你喜欢
微信小程序开发心得
记录:初次cmd启动MySQL拒接访问之解决
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
[untitled]
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
Record: the solution of MySQL denial of access when CMD starts for the first time
121道分布式面试题和答案
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
Matlab读取GNSS 观测值o文件代码示例
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
随机推荐
微信小程序开发心得
Record: Navicat premium can't connect to MySQL for the first time
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
几道高频的JVM面试题
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
一文搞定 UDP 和 TCP 高频面试题!
错误:排序与角标越界
记录:newInstance()过时的代替方法
What are the advantages of using SQL in Excel VBA
[untitled]
Fairygui bar subfamily (scroll bar, slider, progress bar)
闇の連鎖(LCA+树上差分)
[算法] 劍指offer2 golang 面試題2:二進制加法
Knowledge system of digital IT practitioners | software development methods -- agile
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
最短Hamilton路径 (状压DP)
Wechat applet development experience
染色法判定二分图
NovAtel 板卡OEM617D配置步骤记录
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景