C语言入门课程学习笔记10
- 第48课 - 自定义数据类型(上)
- 实验-typedef
- 实验
- 小结
- 第49课 - 自定义数据类型(中)
- 实验
- 实验
- 小结
- 第50课 - 自定义数据类型(下)
- 实验
- 实验
- 小结
- 第51课 - 多文件程序设计
- 实验
- 实验
- 实验
- 小结
- 第52课 - 课程总结和展望
第48课 - 自定义数据类型(上)
实验-typedef
#include <stdio.h>
#include <string.h>typedef unsigned char byte;//void func()
{typedef byte uint8;uint8 var = 200;byte b = var; // 本质为相同类型变量之间的初始化printf("sizeof(uint8) = %d\n", sizeof(uint8));printf("var = %d\n", var);printf("b = %d\n", b);
}int main()
{//uint8 var = 1; // ERRORbyte b = 128;func();printf("sizeof(byte) = %d\n", sizeof(byte));printf("b = %d\n", b);return 0;
}
/*
sizeof(uint8) = 1
var = 200
b = 200
sizeof(byte) = 1
b = 128
*/
#include <stdio.h>
#include <string.h>typedef float(FArr5)[5]; // 定义数组类型名
typedef int(IFuncII)(int, int); // 定义函数类型名typedef FArr5* PFArr5;//新定义的类型PFArr5
typedef IFuncII* PIFuncII;float g_arr[5] = {0.1, 0.2, 0.3};int add(int a, int b)
{return a + b;
}int main()
{FArr5* pa = &g_arr; // float(*)[5]IFuncII* pf = add; // int(*)(int,int)PFArr5 npa = pa;//指针PIFuncII npf = pf;int i = 0;for(i=0; i<5; i++){printf("%f\n", (*pa)[i]);//元素值printf("%f\n", (*npa)[i]);}printf("%d\n", pf(2, 3));//5printf("%d\n", npf(2, 3));//5return 0;
}
实验
#include <stdio.h>
#include <string.h>struct Student
{char name[20];int id;short major;
};int main()
{struct Student s1 = {"Delphi", 908, 1};struct Student s2 = s1;printf("s1.name = %s\n", s1.name);printf("s1.id = %d\n", s1.id);printf("s1.major = %d\n", s1.major);strcpy(s2.name, "Tang");s2.id = 909;s2.major = 2;printf("s2.name = %s\n", s2.name);printf("s2.id = %d\n", s2.id);printf("s2.major = %d\n", s2.major);return 0;
}
/*
s1.name = Delphi
s1.id = 908
s1.major = 1
s2.name = Tang
s2.id = 909
s2.major = 2
*/
小结
第49课 - 自定义数据类型(中)
实验
#include <stdio.h>
#include <string.h>typedef struct Student Stu;//定义新名称Stustruct Student
{char name[20];int id;short major;
};int main()
{Stu s;//Stu* ps = &s;strcpy(ps->name, "Delphi");ps->id = 1;ps->major = 908;(*ps).major = 910; // ==> s.major = 910printf("s.name = %s\n", s.name);printf("s.id = %d\n", s.id);printf("s.major = %d\n", s.major);return 0;
}
/*
s.name = Delphi
s.id = 1
s.major = 910*/
#include <stdio.h>
#include <string.h>struct Test;
struct Test* g_pt; // 只要有了类型声明就可以创建对应的指针变量// 必须先给出类型的完整定义才能创建相应类型的变量
struct Test
{int a;int b;
};int main()
{struct Test t;t.a = 1;t.b = 2;g_pt = &t;printf("g_pt = %p\n", g_pt);printf("g_pt->a = %d\n", g_pt->a);//1printf("g_pt->b = %d\n", g_pt->b);//2return 0;
}
/*
g_pt = 000000000061FE18
g_pt->a = 1
g_pt->b = 2
*/
#include <stdio.h>
#include <string.h>int main()
{struct { int a, b; } v1;struct { int a, b; } v2;struct { int a, b; }*pv;v1.a = 1;v1.b = 2;// v2 = v1;//??error: incompatible types when assigning to type 'struct <anonymous>' from type 'struct <anonymous>'看上去一样,其实是不同的类型pv = &v2;//warning: assignment to 'struct <anonymous> *' from incompatible pointer type 'struct <anonymous> *' 不可以return 0;
}
实验
#include <stdio.h>
#include <string.h>struct BW
{unsigned char a : 4;unsigned char b : 2;unsigned char c : 2;
};int main()
{struct BW bw = {0};//每个比特位都初始化为0bw.a = 10;bw.b = 4; // 4 大于 b 能表示的最大值,因此赋值后 b 回转到 0bw.c = 3;printf("sizeof(struct BW) = %d\n", sizeof(struct BW));printf("bw.a = %d\n", bw.a);printf("bw.b = %d\n", bw.b);printf("bw.c = %d\n", bw.c);return 0;
}
/*
sizeof(struct BW) = 1
bw.a = 10
bw.b = 0
bw.c = 3
*/
#include <stdio.h>
#include <string.h>struct Bits1
{int a : 16;short b : 8;char c : 8;float f; // float f : 32; ==> 浮点型成员不能指点位宽度
};struct Bits2
{unsigned char a : 6;unsigned char b : 6;unsigned char c : 6;// unsigned char d : 9; ==> 指定的位宽度不能大于声明类型的位宽度
};struct Bits3
{unsigned char a : 4;unsigned char : 0; // 重启一个存储单元表示新的成员unsigned char b : 4;
};int main()
{printf("sizeof(Bits1) = %d\n", sizeof(struct Bits1));//2+1+1+4=8不对 ???? 4+2+1每4个字节对齐printf("sizeof(Bits2) = %d\n", sizeof(struct Bits2));//3printf("sizeof(Bits3) = %d\n", sizeof(struct Bits3));//2printf("sizeof(float) = %d\n", sizeof(float));return 0;
}
/*
sizeof(Bits1) = 12
sizeof(Bits2) = 3
sizeof(Bits3) = 2*/
小结
第50课 - 自定义数据类型(下)
实验
#include <stdio.h>
#include <string.h>union UTest//联合体
{int a;float f;
};struct STest
{int a;float f;
};int isLittleEndian()//低地址存在低字节数据
{union{int i;char a[4];} test = {0};test.i = 1;return (test.a[0] == 1);
}int main()
{union UTest ut = {987654321};struct STest st = {987654321, 0.1f};printf("union UTest size = %d\n", sizeof(union UTest));printf("&ut.a = %p\n", &ut.a);printf("&ut.f = %p\n", &ut.f);//地址一样printf("struct STest size = %d\n", sizeof(struct STest));printf("&st.a = %p\n", &st.a);printf("&st.f = %p\n", &st.f);//地址不一样printf("ut.a = %d\n", ut.a);//printf("ut.f = %f\n", ut.f);//ut.f = 987654321.0f;printf("ut.a = %d\n", ut.a);printf("ut.f = %f\n", ut.f);printf("System Endian: %d\n", isLittleEndian());return 0;
}
/*
union UTest size = 4
&ut.a = 000000000061FE1C
&ut.f = 000000000061FE1C
struct STest size = 8
&st.a = 000000000061FE14
&st.f = 000000000061FE18
ut.a = 987654321
ut.f = 0.001697
ut.a = 1315666339
ut.f = 987654336.000000
System Endian: 1
*/
实验
#include <stdio.h>
#include <string.h>enum Day { MON = 1, TUE, WED, THU, FRI, SAT, SUN };
enum Season { Spring, Summer = 3, Autumn, Winter = -1 };enum { CONSTANT = 12345 };int main()
{enum Day d = TUE;enum Season s = Winter;int i = SUN;int j = Autumn;printf("d = %d\n", d); // 2printf("s = %d\n", s); // -1printf("i = %d\n", i); // 7printf("j = %d\n", j); // 4d = 0;s = -2;printf("d = %d\n", d);printf("s = %d\n", s);printf("sizeof(enum Day) = %d\n", sizeof(enum Day));printf("sizeof(enum Season) = %d\n", sizeof(enum Season));printf("CONSTANT = %d\n", CONSTANT);// CONSTANT = 54321;return 0;
}
/*
d = 2
s = -1
i = 7
j = 4
d = 0
s = -2
sizeof(enum Day) = 4
sizeof(enum Season) = 4
CONSTANT = 12345*/
小结
第51课 - 多文件程序设计
实验
在这里插入代码片
实验
在这里插入代码片
实验
//opm.h
struct Test
{int a;int b;
};extern int g_var; // 声明全局变量时,不需要给出初始值
extern int f_var;int add(int a, int b);
int mul(int, int); // 进行函数声明的时候,可以省略参数名extern void test();//opm.c
#include <stdio.h>int g_var = 1;
static int f_var = 2; // 只能在当前文件中使用 ==> 静态全局变量static void func() // 静态函数只能在当前文件中被调用
{printf("void func() : f_var = %d\n", f_var);
}void test() // 当前文件中定义的所有函数都可以调用 func() 函数
{func();
}int add(int a, int b)
{return a + b;
}int mul(int a, int b)
{return a * b;
}
//main.c
#include <stdio.h>
#include <string.h>
#include "opm.h"int main()
{struct Test t = {1, 2};printf("a + b = %d\n", add(t.a, t.b));printf("a * b = %d\n", mul(t.a, t.b));printf("g_var = %d\n", g_var);// printf("f_var = %d\n", f_var); // 无法直接使用其他文件中定义的静态全局变量test();return 0;
}
小结
第52课 - 课程总结和展望