枚举 enum
定义:用户自定义数据类型,可以用于声明一组常数
格式:
enum 枚举名
{
value1=值,
value2=值,
value3=值
};
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum work
{
D1 = 1,
D2 = 2,
D3 = 3
};
int main()
{
int num;
scanf("%d",&num);
if (num==D1)
{
printf("工作第一天\n");
}
return 0;
}
未赋初值,第一个常数会默认为0,依次加一。
未赋值的部分在前一个的基础上加1
存储类型:
static extern register(硬件部分设计,C语言先不做学习)
static:修饰变量和函数
修饰变量:
1. 变量的存放位置在全局区(静态区)
如果静态变量有初值,存放.data区,没有初值存放在.bss区域
2. 生命周期为整个程序
3.限制作用域:
1)修饰局部变量,和普通局部变量作用域没有区别,但是生命周期被延长为整个程序
2)修饰全局变量,限制在本文件中使用
4.被static修饰的变量只初始化一次,不手动赋值,初始值为0
void fun(){
static int a;
a++;
printf("%d\n",a);
}
int main()
{
fun();// 1
fun();// 2
fun();// 3
}
修饰函数:
static修饰函数,限制在本文件使用
extern: 外部引用
通过extern可以引用其他文件中的全局变量或函数
//2.c
#include <stdio.h>
static int a=20;//int a=10;
static void fun(){
printf("24234234\n");
}
//hello.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int a;
int main()
{
extern void fun();
fun();
// printf("a=%d\n",a);
return 0;
}
在此,C语言的学习就结束了,下面依旧会跟进一些练习,然后进入下一门课程,LinuxC 高级