1.结构体的输入与输出
没有现成的库函数供结构体输入使用,通过指针自定义函数输入:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct date{
int year;
int month;
int day;
};
struct date* inputdate(struct date *p){
scanf("%d",&(p->year));
scanf("%d",&(p->month));
scanf("%d",&(p->day));
return p;
}
void outputdate(struct date *p){
printf("%d年%d月%d日",p->year,p->month,p->day);
}
int main(){
struct date d;
//结构体的输入
inputdate(&d);
//结构体输出
outputdate(&d);
}
2.结构中的结构
struct Point{
int x;
int y;
};
struct Rectangle{
struct Point p;
struct Point q;
};
void PrintStruct(struct Rectangle r){
printf("<%d,%d>to<%d,%d>\n",r.p.x,r.p.y,r.q.x,r.q.y);
}
3.⾃定义数据类型 (typedef)
C语⾔提供了⼀个叫做 typedef 的功能来声明⼀个已有的数据类型的新名字
typedef struct ANode{
int x;
double y;
}Node;
4.联合
存储
• 所有的成员共享⼀个空间
• 同⼀时间只有⼀个成员是有效的
• union的⼤⼩是其最⼤的成员
• 初始化
• 对第⼀个成员做初始化
typedef union AU{
int a;
char ch[sizeof(int)];
};
从此结果可知,内存存储方式采用小端存储。
5.区分本地变量、全局变量、静态本地变量
本地变量:本地作用域,本地生存期;
全局变量:全局作用域,全局生存期;
静态本地变量:本地作用域,全局生存期;
6.关于include
#include是⼀个编译预处理指令,和宏⼀样,在编译 之前就处理了
• 它把那个⽂件的全部⽂本内容原封不动地插⼊到它所 在的地⽅
• 所以也不是⼀定要在.c⽂件的最前⾯#include
• #include有两种形式来指出要插⼊的⽂件
• “”要求编译器⾸先在当前目录(.c⽂件所在的目录) 寻找这个⽂件,如果没有,到编译器指定的目录去 找
• <>让编译器只在指定的目录去找
在main.c和max.c里都需要引入max.h
同样,如果需要使用某个.c文件中的全局变量时,需要在.h文件中先声明
7.不对外公开的函数
• 在函数前⾯加上static就使得它成为只能在所在的编 译单元中被使⽤的函数
• 在全局变量前⾯加上static就使得它成为只能在所在 的编译单元中被使⽤的全局变量
8.标准头文件结构