当前位置:网站首页>初识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是声明外部符号的
边栏推荐
- 十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
- 记录:动态Web项目servlet访问数据库404错误之解决
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- 平衡二叉树详解 通俗易懂
- 记录:初次cmd启动MySQL拒接访问之解决
- GNSS定位精度指标计算
- C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
- [algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
- 121 distributed interview questions and answers
- On March 15, the official version of go 1.18 was released to learn about the latest features and usage
猜你喜欢

FairyGUI增益BUFF數值改變的顯示

XV Function definition and call

系统设计学习(一)Design Pastebin.com (or Bit.ly)

Rt-ppp test using rtknavi
![[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers

Mixed use of fairygui button dynamics

Halcon knowledge: gray_ Tophat transform and bottom cap transform

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface

String类

Wechat applet development experience
随机推荐
How to reduce the shutdown time of InnoDB database?
Several high-frequency JVM interview questions
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
使用rtknavi进行RT-PPP测试
Record: newinstance() obsolete replacement method
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
[algorithm] sword finger offer2 golang interview question 1: integer division
第一人称视角的角色移动
记录:Navicat Premium初次无法连接数据库MySQL之解决
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
NovAtel 板卡OEM617D配置步骤记录
What are the advantages of using SQL in Excel VBA
系统设计学习(三)Design Amazon‘s sales rank by category feature
How do architects draw system architecture blueprints?
图书管理系统小练习
RTKLIB: demo5 b34f. 1 vs b33
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
MySQL shutdown is slow