当前位置:网站首页>【C语言快速上手】带你了解C语言,零基础入门③
【C语言快速上手】带你了解C语言,零基础入门③
2022-06-30 09:36:00 【Whisper_2021】
前言
此文章接博主上篇文章【C语言快速上手】带你了解C语言,零基础入门②,没有看过博主上一篇的文章,可以先去看一下。希望各位大佬在阅读后能及时指出其中的问题,作者一定会积极采纳建议,不断改进,不断进步。在此感谢各位的指正。
本文目录
11. 常见关键字
C语言提供了丰富的关键字,这些关键字都是语言本身预先设定好的,用户自己是不能创造关键字的。下面我们简单介绍两个常见关键字,后期到关键字的章节再系统介绍。
11.1 关键字 typedef
typedef 是类型定义,这里应该理解为类型重命名。
//将unsigned int 重命名为uint_32, 所以uint_32也是一个类型名
typedef unsigned int uint_32;
int main()
{
//观察num1和num2,这两个变量的类型是一样的
unsigned int num1 = 0;
uint_32 num2 = 0;
return 0;
}
11.2 关键字 static
在C语言中:static是用来修饰变量和函数的
- 修饰局部变量-称为静态局部变量
- 修饰全局变量-称为静态全局变量
- 修饰函数-称为静态函数
11.2.1 static修饰局部变量
//代码1
#include <stdio.h>
void test()
{
int i = 0;
i++;
printf("%d ", i);
}
int main()
{
int i = 0;
for(i=0; i<10; i++)
{
test();
}
return 0;
}
//代码2
#include <stdio.h>
void test()
{
//static修饰局部变量
static int i = 0;
i++;
printf("%d ", i);
}
int main()
{
int i = 0;
for(i=0; i<10; i++)
{
test();
}
return 0;
}
static修饰局部变量改变了变量的生命周期
让静态局部变量出了作用域依然存在,到程序结束,生命周期才结束。
11.2.2 static修饰全局变量
//代码1
//add.c
int g_val = 2018;
//test.c
int main()
{
printf("%d\n", g_val);
return 0;
}
//代码2
//add.c
static int g_val = 2018;
//test.c
int main()
{
printf("%d\n", g_val);
return 0;
}
编译之后,代码1正常,代码2在编译的时候会出现连接性错误。
一个全局变量被static修饰,使得这个全局变量只能在本源文件内使用,不能在其他源文件内使用。
11.2.3 static修饰函数
//代码1
//add.c
int Add(int x, int y)
{
return x+y;
}
//test.c
int main()
{
printf("%d\n", Add(2, 3));
return 0;
}
//代码2
//add.c
static int Add(int x, int y)
{
return x+y;
}
//test.c
int main()
{
printf("%d\n", Add(2, 3));
return 0;
}
编译结果,代码1正常,代码2在编译的时候会出现连接性错误。
一个函数被static修饰,使得这个函数只能在本源文件内使用,不能在其他源文件内使用。
12. #define 定义常量和宏
define定义一般有两种情况,定义常量和定义宏
//1. define定义标识符常量
#define MAX 1000
//2. define定义宏
#define ADD(x, y) ((x)+(y))
#include <stdio.h>
int main()
{
int sum = ADD(2, 3);
printf("sum = %d\n", sum);
sum = 10*ADD(2, 3);
printf("sum = %d\n", sum);
return 0;
}
13. 指针
13.1 内存
内存是电脑上特别重要的存储器,计算机中程序的运行都是在内存中进行的 。
所以为了有效的使用内存,就把内存划分成一个个小的内存单元,每个内存单元的大小是1个字节。
为了能够有效的访问到内存的每个单元,就给内存单元进行了编号,这些编号被称为该内存单元的地址。
变量是创建在内存中的(在内存中分配空间的),每个内存单元都有地址,所以变量也是有地址的。
取出变量地址如下:
#include <stdio.h>
int main()
{
int num = 10;
#//取出num的地址
//注:这里num的4个字节,每个字节都有地址,取出的是第一个字节的地址(较小的地址)
printf("%p\n", &num);//打印地址,%p是以地址的形式打印
return 0;
}
那么地址该如何存储呢?这时我们就需要定义指针变量了。
int num = 10;
int *p;//p为一个整形指针变量
p = #
指针的使用实例:
#include <stdio.h>
int main()
{
int num = 10;
int *p = #
*p = 20;
return 0;
}
以整形指针举例,可以推广到其他类型,如:
#include <stdio.h>
int main()
{
char ch = 'w';
char* pc = &ch;
*pc = 'q';
printf("%c\n", ch);
return 0;
}
13.2 指针变量的大小
#include <stdio.h>
//指针变量的大小取决于地址的大小
//32位平台下地址是32个bit位(即4个字节)
//64位平台下地址是64个bit位(即8个字节)
int main()
{
printf("%d\n", sizeof(char *));
printf("%d\n", sizeof(short *));
printf("%d\n", sizeof(int *));
printf("%d\n", sizeof(double *));
return 0;
}
结论:指针大小在32位平台是4个字节,64位平台是8个字节。
14. 结构体
结构体是C语言中特别重要的知识点,结构体使得C语言有能力描述复杂类型。
比如描述学生,学生包含: 名字+年龄+性别+学号 这几项信息。
这里只能使用结构体来描述了。
例如:
struct Stu
{
char name[20];//名字
int age; //年龄
char sex[5]; //性别
char id[15]; //学号
};
结构体的初始化:
//打印结构体信息
struct Stu s = {
"张三", 20, "男", "20180101"};
//.为结构成员访问操作符
printf("name = %s age = %d sex = %s id = %s\n", s.name, s.age, s.sex, s.id);
//->操作符
struct Stu *ps = &s;
printf("name = %s age = %d sex = %s id = %s\n", ps->name, ps->age, ps->sex, ps- >id);
感谢你们的耐心阅读,博主本人为一名大一学生,也还有需要很多学习的东西。写这篇文章是以本人所学内容为基础,日后也会不断更新自己的学习记录,我们一起努力进步,变得优秀,小小菜鸟,也能有大大梦想,关注我,我们一起学习。
边栏推荐
- MySQL index and data storage structure foundation
- Appium automation test foundation - ADB shell command
- 【JVM】G1垃圾回收器簡述
- CRF (conditional random field) learning summary
- The URL copied by the browser and pasted into the document is a hyperlink
- Description of event object
- Notes on masking and padding in tensorflow keras
- How to build an all-in-one database cloud machine that meets the needs of information innovation?
- Flutter的特别之处在哪里
- Installation and use
猜你喜欢
文章内容无法复制复制不了
[JVM] brief introduction to CMS
Flume learning 4
Magnetic levitation 3D lamp
无人机项目跟踪记录八十三---pcb图完成
[new book recommendation] mongodb performance tuning
Principle and implementation of small program hand-held bullet screen (uni APP)
Applying applet container technology to IOT ecological construction
【JVM】CMS简述
About Jul
随机推荐
Theme Studio
Configuring MySQL for error reporting
MIT-6874-Deep Learning in the Life Sciences Week5
安装和使用
Some domestic image sources
IDC released the report on China's software defined storage and hyper convergence market in the fourth quarter of 2020, and smartx hyper convergence software ranked first in the financial industry
GNN动手实践(二):复现图注意力网络GAT
Description of event object
Flume learning 1
Application exploration and practice of super convergence in the production environment of insurance industry
【ARK UI】HarmonyOS ETS的启动页的实现
Golang magic code
JUL简介
正则表达式基础
Installation and use
Flume learning 4
How to build an all-in-one database cloud machine that meets the needs of information innovation?
JWT expiration processing - single token scheme
How does the diode work?
Flume learning III