目录
一、递归函数
递归
回归
二、结构体
用户自定义的一种数据类型
定义:
struct 结构体名
{
数据类型 成员变量名1;
数据类型 成员变量名2;
数据类型 成员变量名3;
};
=================================
struct student
{
int id;
float score;
char name[33];
};
数据类型 == struct student
结构体变量
struct结构体名 结构体变量
1.定义的同时直接定义结构体变量
struct 结构体名
{
数据类型 成员变量名1;
数据类型 成员变量名2;
数据类型 成员变量名3;
}结构体变量名;
=============================
struct student
{
int id;
float score;
char name[33];
}stu;
2.先定义结构体数据类型,再定义结构体变量
#include<stdio.h>
struct student
{
int id;
float score;
char name[33];
};
struct student owo; //全局
int main(int argc, char const *argv[])
{
struct student qwq; //局部
return 0;
}
3.缺省结构体名
struct
{
int id;
float score;
char name[33];
}owo;
赋值
1.定义的同时直接用{ }赋值
struct student
{
int id;
float score;
char name[33];
};
struct student zhy={1,99.9,"fat sheep"};
2.先定义结构体,再单独赋值
#include<stdio.h>
#include<string.h>
struct student
{
int id;
float score;
char name[33];
};
struct student bbq;
int main(int argc, char const *argv[])
{
bbq.id=2;
bbq.score=99.9;
strcpy(bbq.name,"ice-cream cat"); //字符串赋值
3.点等法
struct student
{
int id;
float score;
char name[33];
};
struct student kkk={
.id=3,
.score=99.9,
.name="happy cat"
};
访问
结构体变量名.成员变量名
scanf("%f %s",&zhy.score,zhy.name);
printf("%f %s\n",zhy.score,zhy.name);
重命名
1.定义的同时直接重命名
typedef int size_t;
size_t b=6;
====================
typedef struct star
{
int id;
float height;
char name[33];
}ST;
struct star rb;
ST dg; //ST数据类型 dg结构体变量名
2.先定义结构体,再重命名
struct student
{
int id;
float score;
char name[33];
};
struct student qwe;
struct student asd;
typedef struct student ZH;
结构体数组
结构体类型相同的变量组成
1.定义
1.定义结构体同时,定义结构体数组
struct book
{
int id;
char name[33];
float price[33];
}b[5];
2.先定义结构体,再定义结构体数组
struct book
{
int id;
char name[33];
float price[33];
};
struct book b[5];
2.初始化
1.定义结构体同时直接赋值
struct book
{
int id;
char name[33];
float price[33];
}b[4];
struct book s[4] = {
{1,"西游记",21.1},
{2,"水浒传",23.5},
{3,"红楼梦",24},
{4,"三国演义",22.6}
};
2.先定义,再单独赋值
struct book b[4];
b[0].id = 1;
strcpy(b[0].name,"红楼梦");
b[0].price = 21.1;
3.结构体数组输入输出
struct star s[3];
printf("序号 歌名\n");
for(int i=0;i<3;i++)
scanf("%d %s",&s[i].id,s[i].name);
printf("序号 歌名\n");
for(int i=0;i<3;i++)
printf("%d\t%s\n",s[i].id,s[i].name);
结构体指针
struct 结构体名 *指针变量名
struct student
{
int id;
char name[33];
}st;
struct student s[3];
struct student *p=s;
struct student *q=&st;
赋值:结构体指针变量名->成员变量名
#include<string.h>
struct student
{
int id;
char name[33];
}s;
int main(int argc, char const *argv[])
{
struct student *p=&s;
p->id=1;
(*p).id=2;
strcpy(p->name,"qinghua");
printf("%d %s\n",s.id,s.name);
大小
printf("%ld\n",sizeof(p)); //8
结构体大小
在64位的操作系统中,默认的value值为8字节,找出结构体中数据类型最大的成员变量和value值对比,按小的数进行对齐
对齐时的地址偏移量是成员变量类型大小的整数倍
三、共用体
union共用体名
{
成员变量列表;
};
定义:
union共用体名 共用体变量名;
union star
{
int a;
char b;
}s;
int main(int argc, char const *argv[])
{
s.a=65;
s.b='B';
printf("%d %c\n",s.a,s.b);
特点
1.成员变量共用同一块地址空间
2.赋值以最后一次赋值为准
3.共用体的大小是成员变量中数据类型最大的
共用体验证大小端
union star
{
int a;
char b;
}s;
int main(int argc, char const *argv[])
{
s.a=0x12345678;
printf("%#x\n",s.b); //0x78
四、枚举
enum枚举名
{
val1,
val2,
...
};
#include <stdio.h>
enum num
{
value1,
value2=99,
value3
};
int main(int argc, char const *argv[])
{
printf("%d %d %d\n",value1,value2,value3);//0 99 100
return 0;
}
五、存储类型
auto修饰变量,一般省略时,会认为是auto类型
static:修饰变量和函数
修饰变量:
1. 变量的存放位置在全局区(静态区)
如果静态变量有初值,存放.data区,没有初值存放在.bss区域
2. 生命周期为整个程序
3. 限制作用域
3.1修饰局部变量,和普通局部变量作用域没有区别,但是生命周期被延长为整个程序
3.2修饰全局变量和函数,限制在本文件中使用
4. 被static修饰的变量只初始化一次,不手动赋值,初始值为0
修饰函数:
static修饰函数,限制在本文件使用
void fun()
{
static int a=5;
a++;
printf("%d\n",a); //6 7 8
}
int main(int argc, char const *argv[])
{
fun();
fun();
fun();
extern: 外部引用
通过extern可以引用其他文件中的全局变量或函数