当前位置:网站首页>[C language from elementary to advanced] Part 2 Initial C language (2)
[C language from elementary to advanced] Part 2 Initial C language (2)
2022-08-11 05:45:00 【Xiaolu's road to programming】

目录
前言
Here follows the previous solution to the initialC语言讲,The premise is still correctC语言有一个大概的了解,形成一个框架.
<
一.常见的关键字
注意:The keywords here are all set by the language itself,自己不能创建.
Below we briefly pick up the usage of some keywords
1.关键字typedef
typedef 顾名思义是类型定义,这里应该理解为类型重命名.
Below we use a piece of code to explain the meaning of the above sentence.
//将unsigned int 重命名为uint_32, 所以uint_32也是一个类型名
#include<stdio.h>
typedef unsigned int uint_32;
int main()
{
unsigned int num1 = 10;
uint_32 num2 = 10;
printf("%d %d", num1, num2);
return 0;
}
输出结果为 10 10
2.关键字static
在C语言中:
static是用来修饰变量和函数的
- 修饰局部变量-称为静态局部变量
- 修饰全局变量-称为静态全局变量
- 修饰函数-称为静态函数
Let's first understand the distribution of memory

2.1修饰局部变量
代码演示:

The difference between the two codes is thatstatic,So what will be the difference in the output?
左边输出:2 2 2 2 2 2 2 2 2 2
右边输出:2 3 4 5 6 7 8 9 10 11
总结
Why is one missingstaticThis result will appear?Below I will summarize for you,static 修饰局部变量的时候,改变了变量的存储类型,Ordinary local variables are stored in the stack area,而被staticThe decorated variable changes the storage type,Become a variable in the static area,The out scope will not be destroyed,The code on the left above,a被赋值为1,后置++为2,aIt is destroyed when it goes out of scope,Calling againtest函数时,ais reassigned to 2,直到循环结束.而static修饰的变量,The out scope will not be destroyed,依然存在.
So the life cycle of static variables is the life cycle of the program,程序结束的时候,静态变量才回收空间.
2.2修饰全局变量
代码演示


输出:报错
总结
全局变量是具有外部链接属性
static修饰全局变量的时候
这个全局变量的外部链接属性就变成Internal link properties
这个全局变量只能在自己所在的.c文件中看到,No other source files can be seen
不能在其他源文件中使用.
2.3修饰函数
代码演示

输出:还是报错
总结
The function itself has an external linkage property
如果static修饰函数,The external linkage property of the function becomes
Internal link properties
这个函数只能在自己所在的源文件内部使用,其他
Source files do not use this function
二.️#define定义常量和宏
代码演示

The essence of the macro is tox和y的值替换成a和b,And the function is willa和b传给x和y,Then return the last value to the output,He has a process,You can type this code,感受一下.
这里的#defineDefining constants will not be discussed here,I am startingC语言(一)讲到过
大家可以去看看这个
初始C语言(一)
三.指针
1.内存
内存是电脑上特别重要的存储器,计算机中程序的运行都是在内存中进行的 .
所以为了有效的使用内存,就把内存划分成一个个小的内存单元,每个内存单元的大小是1个字节.
为了能够有效的访问到内存的每个单元,就给内存单元进行了编号,这些编号被称为该内存单元的地址
代码演示


变量是创建内存中的(在内存中分配空间的),每个内存单元都有地址,所以变量也是有地址的.
2.指针的使用
代码演示:

以整形指针举例,可以推广到其他类型,如:
#include <stdio.h>
int main()
{
char ch = 'w';
char* pc = &ch;
*pc = 'q';
printf("%c\n", ch);
return 0;
}
3.指针变量的大小
代码演示:
x86(32位机器)output in the environment4,x64(64位机器)output in the environment8
总结
32位机器 - 一个地址是32个二进制位,存储需要32个bit位的空间,所32位机器上,指针变量的大小是4个字节
//64位机器 - 一个地址是64个二进制位,存储需要64个bit位的空间,所64位机器上,指针变量的大小是8个字节
四.结构体
结构体是由一批数据组合而成的结构型数据.组成结构型数据的每个数据称为结构型数据的“成员” ,其描述了一块内存区间的大小及解释意义,在cLanguage is also more important.
比如描述学生,学生包含: 名字+年龄+性别+学号 这几项信息.
代码演示:
代码一
#include<stdio.h>
//创建一个结构体
struct Stu
{
char name[20];//姓名
int age;//年龄
char sex[5];//性别
char id[12];//学号
};
int main()
{
struct Stu s = {
"liming", 20, "nv", "2022010578"};//初始化
printf("%s %d %s %s\n", s.name, s.age, s.sex, s.id);//打印
//结构体变量.结构体成员
return 0;
}
代码二
#include<stdio.h>
struct Stu
{
char name[20];
int age;
char sex[5];
char id[12];
};
void print(struct Stu* ps)
{
printf("%s %d %s %s\n", (*ps).name, (*ps).age, (*ps).sex, (*ps).id);
printf("%s %d %s %s\n", ps->name, ps->age, ps->sex, ps->id);
//结构体指针->结构体成员
}
int main()
{
struct Stu s = {
"zhangsan", 20, "nan", "2022010578" };
print(&s);//函数调用,取出s的地址.
return 0;
}
Above we talked about the printing of three structures.
总结
今天,初始CLanguage is over,下期预告【CLanguages from Beginner to Advanced】第三篇,CDetailed explanation of language branch loops.
感谢各位观看,码文不易,Three hits to go🥰.
边栏推荐
- C语言——函数的使用
- 博客目录管理 :机器学习 深度学习 nlp
- (一)性能实时监控平台搭建(Grafana+Influxdb+Jmeter)
- 【背包】采药题解
- 【C语言从初阶到进阶】第二篇 初始C语言(二)
- 搭建PX4开发环境
- (3) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Node_explorer+Jmeter)
- 基于 TF-IDF 文本匹配实战详细教程 数据+代码 可直接运行
- (1) Docker installs Redis in practice (one master, two slaves, three sentinels)
- Minecraft
猜你喜欢

(2) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Jmeter)

selenuim使用cookie登录京东

(1) Construction of a real-time performance monitoring platform (Grafana+Influxdb+Jmeter)

【win10+cuda7.5+cudnn6.0安装caffe⑥】报错及处理方式

Django--20 implements Redis support, context, and interaction of context and interface

task04 Pytorch进阶训练技巧

【网站小白】mySQL数据库异常断开

【win10+cuda7.5+cudnn6.0安装caffe②】安装Visual Studio 2013和caffe

(1) Docker installs Redis in practice (one master, two slaves, three sentinels)

并发编程之线程基础
随机推荐
吃瓜教程task03 第4章 决策树
LeetCode刷题Top100之两数之和
Qt 字符串截取 查找字符串
【win10+cuda7.5+cudnn6.0安装caffe④】安装pycaffe
第5章 循环和关系表达式
信息学奥赛
华为od德科面试数据算法解析 2022-8-10 迷宫问题
2021研究生数学建模D题,BP神经网络和卷积神经网络解题代码(基于pytorch)
深入理解线程、进程、多线程、线程池
Flask framework learning: trailing slashes for routes
QT GrabWindow截取屏幕
【网站小白】mySQL数据库异常断开
flask框架学习:debug与配置项
imx6 yocto编译备忘
普林斯顿概率论读本读书笔记(阅读中......)
吃瓜教程task02 第3章 线性模型
博客帮助文档
原生态mongo连接查询代码
The most complete installation tutorial of Pytorch (one step)
输入字符串,替换其中敏感词进行输出
