10.常见关键字
break(跳出循环) char const continue default do case enum(枚举)
register(寄存器) int(定义的变量是有符号的,与signed int等价) unsigned(无符号数)
struck(结构体关键字) union(联合体)...
-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
auto:自动
局部变量都是自动变量,所以int a = 10 是由auto int a = 10省略得来
局部变量前都有auto
局部变量具有局部作用域,它是自动(auto)类型。它在程序运行期间不是一直存在,只有在函数执行期间存在,函数的一次调用结束后,变量所占内存即被收回
-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
typedef:
类型定义,可理解为类型重命名
#include <stdio.h>
int main()
{
typedef unsigned int u_int;//将unsigned int重命名为u_int
unsigned int num = 20;
u_int num2 = 20;//使用u_int就等于使用unsigned int
return 0;
}
上面的两个变量num与num2类型完全相同
-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
static:
static修饰变量和函数
static修饰局部变量
#include <stdio.h>
void test()
{
int a = 1;//这里a是一个局部变量,出函数范围时销毁
a++;
printf("a=%d\n",a);
}
int main()
{
int i = 0;
while(i<5)//while从0到4循环5次
{
test();
i++;//i++后0变成1,但仍小于5,又进入test函数并给a赋值:1
}
return 0;
}
运行结果:
a=2
a=2
a=2
a=2
a=2
-- -- -- -- -- -- -- -- -- --
如果我们把int a = 1换成static int a = 1其他不变呢
#include <stdio.h>
void test()
{
static int a = 1;
a++;
printf("a=%d\n",a);
}
int main()
{
int i = 0;
while(i<5)
{
test();
i++;
}
return 0;
}
运行结果①:
a=2
a=3
a=4
a=5
a=6
static修饰局部变量,局部变量的生命周期变长,得到a=2后a没有被销毁且会在下次循环进入函数时在a=2的基础上+1得到3,同样循环5次最后得到a=6
-- -- -- -- -- -- -- -- -- --
static修饰全局变量
新建一个全局变量并在其中创建一个全局变量:
//源文件1
int g_val = 2023;
//源文件2
#include <stdio.h>
int main()
{
//extern:声明外部符号的
extern int g_val;
printf("g_val=%d\n",g_val);
return 0;
}
运行结果:g_val=2023②
但如果我们给全局变量前面加一个static
//源文件1
static int g_val = 2023;
再次运行就会报错:“无法解析的外部符号 g_val”③
static修饰全局变量。改变了全局变量的作用域,让静态的全局变量只能在自己所在的源文件内部使用,出了源文件就无法使用了
-- -- -- -- -- -- -- -- -- --
static修饰函数
static修饰函数改变了函数的链接属性。想要引用外部的函数,用extern声明一下即可
若在函数前面加上static,函数的“外部链接属性”就会变成“内部链接属性”,这是,即便使用extern去引用外部的函数仍然会报错
-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#define定义常量和宏
#define定义标识符常量
#define MAX 100//编译器遇见MAX就会将其替换成100
#include <stdio.h>
int main()
{
int a = MAX;
printf("%d\n",a);
return 0;
}
运行结果:100
-- -- -- -- -- -- -- -- -- --
#define定义宏
#define MAX(X,Y) (X>Y?X:Y)
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int max = MAX(a,b);
printf("max=%d\n",max);
return 0;
}
运行结果:max=20
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
11.指针
#include <stdio.h>
int main()
{
int a = 0;
int *p = &a;//将变量a的地址取出来
//有一种变量是用来存放地址的————指针变量(p)
*p=2023;//*————解引用操作符
//对p进行解引用操作,通过地址找到它所指向的对象a
//*p=20是指通过对p解引用(*p)找到的a里面的值,并把它改成20
printf("a=%d\n",a);
return 0;
}
运行结果:a=2023④
-- -- -- -- -- -- -- -- -- --
#include <stdio.h>
int main()
{
char ch = 'w';
char*p = &ch;
*p='a';
printf("%c\n",ch);
return 0;
}
运行结果:a
指针变量的大小
在32位平台是四个字节,64位平台是八个字节
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
12.结构体
结构体(我们自己创造出来的类型)使C语言有能力描述复杂类型
struct:结构体关键字
#include <stdio.h>
struct Book//创建一个"书"类型
{
char name[20];//书的名字——C语言程序设计
short price;//书的价格——55
};//;用于结束该类型定义
int main()
{
//利用结构体类型,创建一个该类型的结构体变量
struct Book b1 = {"C语言程序设计",55};
printf("书名:%s\n",b1.name);//找到b1里面“name”该成员
printf("价格:%d\n",b1.price);
return 0;
}
运行结果:
书名:C语言程序设计
价格:55
#include <stdio.h>
struct Book
{
char name[20];
short price;
};
int main()
{
struct Book b1 = {"C语言程序设计",55};
struct Book*bp = &b1;//bp就是结构体指针
printf("书名:%s\n",bp->name);//结构体变量.成员 结构体指针->成员
printf("价格:%d\n",bp->price);//printf("%d\n",(*pb).price)结果一样,指针bp解引用了就是变量b1嘛
return 0;
}
运行结果:
书名:C语言程序设计
价格:55
附
①
![](https://i-blog.csdnimg.cn/blog_migrate/96930d629771e3e6d2872e3ccd47f157.jpeg)
②
![](https://i-blog.csdnimg.cn/blog_migrate/15fe253fb81bf928ae7200742a984933.png)
![](https://i-blog.csdnimg.cn/blog_migrate/359e09844487e91058692fc21103560a.png)
③
![](https://i-blog.csdnimg.cn/blog_migrate/d5f749f78d17e35e90309c51505101f9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b8c469869a61b6bd256d000fee1b70de.png)
④
可以理解为p=&a(p里面是a的地址),*p==a(对地址解引用就等于变量a),*p=20(对地址解引用再给他赋值就等于对a赋值)
还可以理解为*和&抵消了
![](https://i-blog.csdnimg.cn/blog_migrate/43f66a3f6701ed4c84f4e19959b4c509.png)
到此初识C语言部分就结束了,本章主要是简单介绍C语言的内容。旨在了解C语言并建设一个大致的框架,后面会针对各个部分做更加详细的介绍。希望能对正在学习的朋友能有所帮助,谢谢!