第一章 基础技能篇
第1课 C语言简介
在线课件:https://shimo.im/docs/gTt8JX9k9WXdkCYT
学习过程常见问题:https://blog.csdn.net/tencupofkaiwater/article/details/105301159
任何问题可以加扣群 :876662784
扫描观看视频课程:
第2课 开发环境搭建
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("Hello World xxxx!!\n");
return 0;
}
第3课 Hello World
/*
* @file hello.c
* 接受输入,然后和hello world一起输出
* @author WangTeacher
*/
// 预处理器指令
// 引入头文件
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
/*
* @brief:主函数
* @argc: 命令行参数个数
* @argv: 参数值列表
*/
int main(int argc, char* argv[]) { /* 语句块开始 */
// 变量
char str[100] = {0};
// 语句
// gets(str);
scanf("%[^\n]%*c", str);
// 调用函数
printf("Hello, World!\n");
printf("%s\n", str);
// 返回值
return 0;
} /* 语句块结束 */
// 编译
// $ gcc hello.c
// 运行
// $ a.out
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char c = 0;
6 char word[100] = {0};
7 char line[200] = {0};
8
9 scanf("%c\n%s\n%[^\n]%*c", &c, word, line);
10 // scanf("%[^\n]%*c", word);
11 // scanf("%[^\n]%*c", line);
12
13 printf("%c\n%s\n%s\n", c, word, line);
14 // printf("%s\n", word);
15 // printf("%s\n", line);
16
17 return 0;
18 }
第4课 程序的基本组成
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int test() {
5 char if;
6 char 12_b=0;
7 char a='\0';
8
9 scanf("%c%c%c", &if,12_b,a);
10 printf("%c\n%s\n%s\n",if,12_b,a);
11
12 return 0;
13 }
第5课 数据类型及整数
1 #include <stdio.h>
2
3 int main() {
4 unsigned int i = 0;
5 unsigned int j = 0;
6 unsigned int k = 0;
7
8 for (i = 1; i < 5; i++) {
9 for (j = 1; j < 5; j++) {
10 for (k = 1; k < 5; k++) {
11 if (i != j && i != k && j != k) {
12 printf("%d%d%d\n", i, j, k);
13 }
14 }
15 }
16 }
17 }
第6课 浮点类型和void类型
1 #include <stdio.h>
2 #include <math.h>
3
4 int main(int argc, char* argv[]) {
5 const double pi = 3.1415926;
6 double r = 0.0;
7 double h = 0.0;
8 double area = 0.0;
9
10 scanf("%lf%lf", &r, &h);
11 area = pi * r * r * 2 + 2 * pi * r * h;
12
13 printf("area = %10.3lf\n", area);
14
15 return 0;
16 }
1 #include <stdio.h>
2 #include <float.h>
3
4 int main(int argc, char* argv[]) {
5 printf("float 存储最大字节数 : %lu \n", sizeof(float));
6 printf("float 最小值: %E\n", FLT_MIN );
7 printf("float 最大值: %E\n", FLT_MAX );
8 printf("float 精度值: %u\n", FLT_DIG );
9
10 printf("double 存储最大字节数 : %lu \n", sizeof(double));
11 printf("double 最小值: %E\n", DBL_MIN );
12 printf("double 最大值: %E\n", DBL_MAX );
13 printf("double 精度值: %u\n", DBL_DIG );
14
15 printf("long double 存储最大字节数 : %lu \n", sizeof(long double));
16 printf("long double 最小值: %LE\n", LDBL_MIN );
17 printf("long double 最大值: %LE\n", LDBL_MAX );
18 printf("long double 精度值: %u\n", LDBL_DIG );
19
20 return 0;
21 }
第7课 char类型
1 #include <stdio.h>
2
3 int main() {
4 char x = '\0';
5 int diff = 'a' - 'A';
6
7 printf("please input a char: ");
8 scanf("%c", &x);
9
10 if (x >= 'A' && x <= 'Z') {
11 printf("%c\n", x + diff);
12 } else if (x >= 'a' && x <= 'z') {
13 printf("%c\n", x - diff);
14 } else {
15 printf("%c\n", x);
16 }
17
18 return 0;
19 }
1 #include <stdio.h>
2
3 int main() {
4 unsigned int score = 0;
5 char grade = '\0';
6
7 printf("please input your score: ");
8 scanf("%d", &score);
9
10 if (score >= 90) {
11 grade = 'A';
12 } else if (score >= 60) {
13 grade = 'B';
14 } else {
15 grade = 'C';
16 }
17
18 printf("%c\n", grade);
19
20 return 0;
21 }
1 #include <stdio.h>
2
3 int main() {
4 char x = 'a';
5
6 while (x <= 'z') {
7 printf("%d ", x++);
8 }
9
10 printf("\n=================\n");
11
12 x = 'A';
13 while (x <= 'Z') {
14 printf("%d ", x++);
15 }
16
17 printf("\n=================\n");
18
19 x = '0';
20 while (x <= '9') {
21 printf("%d ", x++);
22 }
23
24 return 0;
25 }
第8课 bool类型
#include <stdio.h>
void fun1(const int x) {
int true = 1;
int false = 0;
int ret = 0;
if (x == 0) {
ret = false;
} else {
ret = true;
}
if (ret) {
printf("fun1: true\n");
} else {
printf("fun1: false\n");
}
}
void fun2(const int x) {
typedef enum {false, true} bool;
bool ret = false;
if (x == 0) {
ret = false;
} else {
ret = true;
}
if (ret) {
printf("fun2: true\n");
} else {
printf("fun2: false\n");
}
}
void fun3(const int x) {
#define false 0
#define true 1
int ret = false;
if (x == 0) {
ret = false;
} else {
ret = true;
}
if (ret) {
printf("fun3: true\n");
} else {
printf("fun3: false\n");
}
}
int main() {
int x = 0;
printf("please input a int: ");
scanf("%d", &x);
fun1(x);
fun2(x);
fun3(x);
return 0;
}
第9课 枚举类型
#include <stdio.h>
#include <string.h>
void first_kind(char* in_color) {
enum colour {
white,
black,
blue,
red,
other
};
enum colour my_color = white;
if (strcmp(in_color, "white") == 0) {
my_color = white;
} else if (strcmp(in_color, "black") == 0) {
my_color = black;
} else if (strcmp(in_color, "blue") == 0) {
my_color = blue;
} else if (strcmp(in_color, "red") == 0) {
my_color = red;
} else {
my_color = other;
}
printf("first >> %s: %d\n", in_color, my_color);
}
void second_kind(char* in_color) {
enum colour {
white = 1,
black,
blue,
red,
other
} my_color;
if (strcmp(in_color, "white") == 0) {
my_color = white;
} else if (strcmp(in_color, "black") == 0) {
my_color = black;
} else if (strcmp(in_color, "blue") == 0) {
my_color = blue;
} else if (strcmp(in_color, "red") == 0) {
my_color = red;
} else {
my_color = other;
}
printf("second >> %s: %d\n", in_color, my_color);
}
void third_kind(char* in_color) {
enum {
white = 2,
black = 4,
blue = 8,
red = 16,
other = 32
} my_color;
if (strcmp(in_color, "white") == 0) {
my_color = white;
} else if (strcmp(in_color, "black") == 0) {
my_color = black;
} else if (strcmp(in_color, "blue") == 0) {
my_color = blue;
} else if (strcmp(in_color, "red") == 0) {
my_color = red;
} else {
my_color = other;
}
printf("third >> %s: %d\n", in_color, my_color);
}
void fourth_kind(char* in_color) {
typedef enum {
white,
black,
blue,
red,
other
} colour;
colour my_color = white;
if (strcmp(in_color, "white") == 0) {
my_color = white;
} else if (strcmp(in_color, "black") == 0) {
my_color = black;
} else if (strcmp(in_color, "blue") == 0) {
my_color = blue;
} else if (strcmp(in_color, "red") == 0) {
my_color = red;
} else {
my_color = other;
}
printf("fourth >> %s: %d\n", in_color, my_color);
}
int main() {
char in_color[16] = {0};
printf("please input your like color: ");
scanf("%s", in_color);
first_kind(in_color);
second_kind(in_color);
third_kind(in_color);
fourth_kind(in_color);
return 0;
}
第10课 typedef
#include <stdio.h>
#include <string.h>
typedef char Name[50];
typedef char Type;
typedef char int8;
typedef short int16;
typedef int int32;
typedef long long int64;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef int32 Count;
typedef uint32 Bid;
typedef struct {
Name name;
Type type;
Count count;
Bid book_id;
} Book;
int main() {
Book book;
memset(&book, 0, sizeof(book));
printf("please input book's name: ");
scanf("%s", book.name);
printf("please input book's type: ");
scanf("\n%c", &book.type);
printf("please input book's count: ");
scanf("%d", &book.count);
printf("please input book's id: ");
scanf("%u", &book.book_id);
printf("name: %s, type: %c, count: %d, id: %u\n", book.name, book.type, book.count, book.book_id);
return 0;
}
第11课 数组
#include <stdio.h>
#define MAX_SIZE 100
#define STAT_MAX_SIZE 127
typedef unsigned int uint32;
int main() {
char tmp = 0;
char char_set[MAX_SIZE] = {0};
uint32 i = 0;
uint32 stat[STAT_MAX_SIZE] = {0};
printf("please input chars in line: ");
while (scanf("%c", &tmp) && tmp != '\n') {
char_set[i++] = tmp;
}
for (i = 0; i < MAX_SIZE && char_set[i] != 0; i++) {
printf("%c", char_set[i]);
stat[char_set[i]]++;
}
for (i = 0; i < STAT_MAX_SIZE; i++) {
if (stat[i] == 0) {
continue;
}
printf("%c: %u\n", i, stat[i]);
}
return 0;
}
第12课 多维数组
#include <stdio.h>
int main() {
unsigned int score[2][3] = {0};
double avg[3] = {0};
int j = 0;
int i = 0;
for (j = 0; j < 2; j++) {
for (i = 0; i < 3; i++) {
scanf("%u", &score[j][i]);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
avg[i] += score[j][i];
}
}
for (i = 0; i < 3; i++) {
printf("%f ", avg[i]/2);
}
printf("\n");
return 0;
}
第13课 转义符
\t.\n");
printf("\\': %c.\n", ch);
printf("\\\?: %c.\n", xch);
printf("\\a: \a.\n");
return 0;
}
第14课 字符串基础
#include <stdio.h>
#include <string.h>
int main() {
char s1[128] = {'\0'};
char s2[128] = {'\0'};
char ch = 0;
char *p1 = NULL;
char *p2 = NULL;
scanf("%s", s1);
scanf("%s", s2);
printf("start >> s1: %s, s2: %s\n", s1, s2);
// strcpy(s1, s2);
// printf("end >> s1: %s, s2: %s\n", s1, s2);
// printf("s1[3]: %c, s1[4]: %c\n", s1[3], s1[4]);
//strcat(s1, s2);
//printf("end >> s1: %s, s2: %s\n", s1, s2);
//printf("s1.length: %lu, s2.length: %lu\n", strlen(s1), strlen(s2));
/*
if (strcmp(s1, s2) == 0) {
printf("s1 == s2\n");
} else if (strcmp(s1, s2) > 0) {
printf("s1 > s2\n");
} else {
printf("s1 < s2\n");
}
*/
/*
ch = getchar();
ch = getchar();
p1 = strchr(s1, ch);
p2 = strchr(s2, ch);
if (p1) {
printf("p1 >> %p: %c\n", p1, *p1);
} else {
printf("p1 >> address: %p\n", p1);
}
if (p2) {
printf("p2 >> %p: %c\n", p2, *p2);
} else {
printf("p2 >> address: %p\n", p2);
}
*/
p1 = strstr(s1, s2);
printf("p1: %s\n", p1);
return 0;
}
第15课 逻辑判断语句
#include <stdio.h>
void switch_select(const int kind) {
switch (kind) {
case 1:
case 2:
if (kind == 1) {
printf("《人工智能》\n");
}
else if (kind == 2) {
printf("《老人与海》\n");
}
printf("《时间简史》\n");
break;
case 3:
printf("《明朝的那些事儿》\n");
break;
default:
printf("什么都没有找到\n");
break;
}
}
void if_select(const int kind) {
if (kind == 1) {
printf("a.《人工智能》\n");
} else if (kind == 2) {
printf("b.《老人与海》\n");
} else if (kind == 3) {
printf("c.《明朝的那些事儿》\n");
} else {
printf("什么都没有找到\n");
}
}
int main() {
int kind = 0;
char seq = 0;
printf("Book Query System\n");
printf("1. 科技,2. 文学,3. 历史\n");
printf("Please select number(1~3):");
scanf("%d", &kind);
//switch_select(kind);
if_select(kind);
printf("Please input the book seq:");
scanf("\n%c", &seq);
switch (seq) {
case 'a':
printf("a.《人工智能》\n");
break;
case 'b':
printf("b.《老人与海》\n");
break;
case 'c':
printf("c.《明朝的那些事儿》\n");
break;
default:
printf("Don't found this book.\n");
break;
}
return 0;
}
第16课 循环语句
#include <stdio.h>
#define MAX_SIZE 128
int main() {
int inputs[MAX_SIZE] = {0};
int n = 0;
int i = 0;
int tmp = 0;
int max = 0;
printf("Please input N: ");
scanf("%d", &n);
if (n > MAX_SIZE) {
printf("N must < %d\n", MAX_SIZE);
return -1;
}
printf("Please input N int: ");
while (i < n && scanf("%d", &tmp)) {
inputs[i] = tmp;
i++;
}
// for
//for (i = 0; i < n; i++) {
// if (inputs[i] > max) {
// max = inputs[i];
// }
//}
//i = 0;
//do {
// if (inputs[i] > max) {
// max = inputs[i];
// }
// i++;
//} while (i < n);
i = 0;
start:
if (inputs[i] > max) {
max = inputs[i];
}
i++;
if (i >= n) {
goto end;
}
goto start;
end:
printf("max: %d\n", max);
return 0;
}
第17课 VS调试程序
第18课 gdb调试程序
第19课 函数
#include <stdio.h>
int min(const int m, const int n);
int max(const int m, const int n) {
if (m > n) {
return m;
}
return n;
}
int sum(const int m, const int n) {
return m + n;
}
int diff(const int m, const int n) {
return m - n;
}
int product(const int m, const int n) {
return m * n;
}
int main() {
int m = 0;
int n = 0;
scanf("%d", &m);
scanf("%d", &n);
printf("min: %d\n", min(m, n));
printf("max: %d\n", max(m, n));
printf("sum: %d\n", sum(m, n));
printf("diff: %d\n", diff(m, n));
printf("product: %d\n", product(m, n));
return 0;
}
int min(const int m, const int n) {
if (m > n) {
return n;
}
return m;
}
第20课 变量作用域
int sum(int a, int b);
int main() {
// int a = 0;
int b = 0;
printf("sum = %d\n", sum(a, b));
return 0;
}
int test() {
return sum(1, 2);
}
int sum(int a, int b) {
return a + b;
}
第21课 变量初始化
#include <stdio.h>
char a;
int b;
float c;
double d;
char* p;
int main() {
printf("a = %c, b = %d, c = %f, d = %lf, p = %p\n", a, b, c, d, p);
return 0;
}
第22课 算术运算符
#include <stdio.h>
int main() {
int a = 0;
int b = 0;
scanf("%d%d", &a, &b);
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a / b = %lf\n", (double)a / b);
printf("a %% b = %d\n", a % b);
printf("a++ = %d, b++ = %d\n", a++, b++);
printf("a = %d\n", a);
printf("a-- = %d, b-- = %d\n", a--, b--);
a = 1;
b = 2;
printf("++a = %d, ++b = %d\n", ++a, ++b);
printf("--a = %d, --b = %d\n", --a, --b);
return 0;
}
第23课 关系运算符
#include <stdio.h>
void kind_one(const int a, const int b);
void kind_two(const int a, const int b);
int main() {
int a = 0;
int b = 0;
printf("Please input a and b: ");
scanf("%d%d", &a, &b);
kind_one(a, b);
printf("--------------------\n");
kind_two(a, b);
return 0;
}
void kind_one(const int a, const int b) {
if (a == b) {
printf("a == b ? true\n");
} else {
printf("a == b ? false\n");
}
if (a != b) {
printf("a != b ? true\n");
} else {
printf("a != b ? false\n");
}
if (a > b) {
printf("a > b ? true\n");
} else {
printf("a > b ? false\n");
}
if (a < b) {
printf("a < b ? true\n");
} else {
printf("a < b ? false\n");
}
if (a >= b) {
printf("a >= b ? true\n");
} else {
printf("a >= b ? false\n");
}
if (a <= b) {
printf("a <= b ? true\n");
} else {
printf("a <= b ? false\n");
}
}
void kind_two(const int a, const int b) {
printf("a == b ? %d\n", a == b);
printf("a != b ? %d\n", a != b);
printf("a > b ? %d\n", a > b);
printf("a < b ? %d\n", a < b);
printf("a >= b ? %d\n", a >= b);
printf("a <= b ? %d\n", a <= b);
}
第24课 逻辑运算符
#include <stdio.h>
void normal_var(const int a, const int b) {
// &&
if (a && b) {
printf("a && b ? true\n");
} else {
printf("a && b ? false\n");
}
if (a || b) {
printf("a || b ? true\n");
} else {
printf("a || b ? false\n");
}
if (!(a && b)) {
printf("!(a && b) ? true\n");
} else {
printf("!(a && b) ? false\n");
}
}
void expr() {
int score[100][2] = {0};
int number = 0;
int i = 0;
int boy_pass_cnt = 0;
int girl_pass_cnt = 0;
int all_fail_cnt = 0;
printf("Please the number: ");
scanf("%d", &number);
// boy == 0, girl == 1
for (i = 0; i < number; i++) {
scanf("%d%d", &score[i][0], &score[i][1]);
}
for (i = 0; i < number; i++) {
if (score[i][0] == 0 && score[i][1] >= 60) {
boy_pass_cnt++;
} else if (score[i][0] == 1 && score[i][1] >= 60) {
girl_pass_cnt++;
} else {
all_fail_cnt++;
}
}
printf("boy_pass_cnt = %d\n", boy_pass_cnt);
printf("girl_pass_cnt = %d\n", girl_pass_cnt);
printf("all_fail_cnt= %d\n", all_fail_cnt);
}
int main() {
int a = 0;
int b = 1;
normal_var(a, b);
expr();
return 0;
}
第25课 位运算符
void swap(int a, int b) {
a=a^b;
b=b^a;
a=a^b;
}
int abs( int x ) {
int y=x>>31 ;
return(x^y)-y;//也可写作 (x+y)^y
}
int CountNumberOfOne(int number)
{
int counter = 0;
while (number)
{
counter++;
number &= number - 1 ;
}
return counter;
}
第26课 赋值算符
#include <stdio.h>
void print_ret(const char* str, const int c) {
printf("%s = %d\n", str, c);
}
int main() {
int a = 0;
int b = 0;
int t = 0;
printf("Please input a and b: ");
scanf("%d%d", &a, &b);
t = a;
print_ret("a + b", a + b);
print_ret("a += b", a += b);
a = t;
print_ret("a -= b", a -= b);
a = t;
print_ret("a *= b", a *= b);
a = t;
print_ret("a /= b", a /= b);
a = t;
print_ret("a %= b", a %= b);
a = t;
print_ret("a <<= b", a <<= b);
a = t;
print_ret("a >>= b", a >>= b);
a = t;
print_ret("a &= b", a &= b);
a = t;
print_ret("a |= b", a |= b);
a = t;
print_ret("a ^= b", a ^= b);
return 0;
}
第27课 其他运算符
#include <stdio.h>
struct Book {
int a;
int b;
} book;
void show_sizeof() {
int a = 0;
int b[10] = {};
char c[10] = {};
printf("sizeof(a) = %lu\n", sizeof(a));
printf("sizeof(b) = %lu\n", sizeof(b));
printf("sizeof(c) = %lu\n", sizeof(c));
printf("sizeof(book) = %lu\n", sizeof(book));
}
void show_point() {
int a = 2;
int *p = &a;
printf("p = %p\n", p);
printf("*p = %d\n", *p);
}
void show_three() {
int a = 0;
int b = 1;
int c = 0;
/**
if (a != 0) {
c = a;
} else {
c = b;
}
*/
c = a != 0 ? a + b : a - b;
printf("c = %d\n", c);
}
int main() {
// show_sizeof();
// show_point();
show_three();
return 0;
}
第28课 结构体
#include <stdio.h>
struct Student {
char name[32];
char sex;
unsigned age;
char class[32];
};
int main() {
struct Student stud;
printf("name: ");
scanf("%s", stud.name);
printf("sex: ");
scanf("\n%c", &stud.sex);
printf("age: ");
scanf("%d", &stud.age);
printf("class: ");
scanf("%s", stud.class);
printf("name: %s\n", stud.name);
printf("sex: %c\n", stud.sex);
printf("age: %u\n", stud.age);
printf("class: %s\n", stud.class);
return 0;
}
第29课 结构体嵌套
#include <stdio.h>
#include <string.h>
typedef struct {
unsigned chinese;
unsigned math;
unsigned english;
} Score;
typedef struct {
char name[32];
char sex;
unsigned age;
char class[32];
Score score;
} Student;
void print_info(const Student stud) {
printf("name = %s, sex = %c, age = %u, class = %s\n",
stud.name, stud.sex, stud.age, stud.class);
printf("score >> Chinese = %u, math = %u, English = %u\n",
stud.score.chinese, stud.score.math, stud.score.english);
}
int main() {
// 1: {}
Student stud1 = {};
// 2
Student stud2 = {"stud2", 'n', 12, "二班", {99,88,77}};
// 3
Student stud3;
// 4
Student stud4 = {
.score.chinese = 99,
.score.math = 100,
.score.english = 60,
.class = "三班",
.age = 13,
.sex = 'v',
.name = "stud4"
};
// 5
Student stud5 = {
class : "三班",
age : 13,
sex : 'v',
name : "stud4"
};
Student stud[10] = {};
unsigned n = 0;
int i = 0;
printf("Please input number: ");
scanf("%u", &n);
printf("Please input info: ");
for (i = 0; i < n; i++) {
scanf("%s %c%u%s%u%u%u", stud[i].name, &stud[i].sex,
&stud[i].age, stud[i].class, &stud[i].score.chinese,
&stud[i].score.math, &stud[i].score.english);
}
for (i = 0; i < n; i++) {
print_info(stud[i]);
}
printf("--------------------\n");
for (i = 0; i < n; i++) {
memset(&stud[i].score, 0, sizeof(Score));
// stud[i].score.chinese = 0;
// stud[i].score.math = 0;
// stud[i].score.english = 0;
}
for (i = 0; i < n; i++) {
print_info(stud[i]);
}
return 0;
}
第30课 结构体数组
gcc 能通过编译
#include <stdio.h>
#include <string.h>
typedef struct {
char name[32];
char sex;
unsigned age;
char class[32];
} Student;
Student search(const Student stud[], const char* name);
void print_info(const Student stud[]);
int main() {
// kind 1:
Student stud1[10] = {}; // VS 只能使用{0}
// kind 2:
Student stud2[10] = {
{"zhangsan", 'n', 12, "二班"},
{.name = "lisi", .sex = 'n', .age = 13, .class= "三班"},
{.sex = 'n', .age = 13, .class = "三班", .name = "lisi"},
{name : "lisi", sex : 'n', age : 13, class: "三班"}, // VS不支持
{sex : 'n', age : 13, class: "三班", name : "lisi"}, // VS不支持
{"", '\0', 0, ""}
};
Student xstud = {}; // VS 只能使用{0}
print_info(stud1);
print_info(stud2);
xstud = search(stud2, "zhangsan");
printf("found : name=%s, sex=%c, age=%d, class=%s\n",
xstud.name, xstud.sex, xstud.age, xstud.class);
memset(&xstud, 0, sizeof(Student));
xstud = search(stud2, "xlisi");
printf("found : name=%s, sex=%c, age=%d, class=%s\n",
xstud.name, xstud.sex, xstud.age, xstud.class);
return 0;
}
Student search(const Student stud[], const char* name) {
Student xstud = {}; // VS 只能使用{0}
int i = 0;
for (i = 0; i < 10; i++) {
if (strcmp(stud[i].name, name) == 0) {
xstud = stud[i];
break;
}
}
return xstud;
}
void print_info(const Student stud[]) {
int i = 0;
printf("----------------\n");
for (i = 0; i < 10; i++) {
if (strlen(stud[i].name) == 0) {
break;
}
printf("%d. name=%s, sex=%c, age=%d, class=%s\n",
i, stud[i].name, stud[i].sex, stud[i].age, stud[i].class);
}
}
VS请使用下面的代码:
#include <stdio.h>
#include <string.h>
typedef struct {
char name[32];
char sex;
unsigned age;
char class[32];
} Student;
Student search(const Student stud[], const char* name);
void print_info(const Student stud[]);
Student stud1[10] = { 0 };
int main() {
// kind 1:
// kind 2:
Student stud2[10] = {
{"zhangsan", 'n', 12, "二班"},
{.name = "lisi", .sex = 'n', .age = 13, .class = "三班"},
{.sex = 'n', .age = 13, .class = "三班", .name = "lisi"},
{"", '\0', 0, ""}
};
Student xstud = {0};
print_info(stud1);
print_info(stud2);
xstud = search(stud2, "zhangsan");
printf("found : name=%s, sex=%c, age=%d, class=%s\n",
xstud.name, xstud.sex, xstud.age, xstud.class);
memset(&xstud, 0, sizeof(Student));
xstud = search(stud2, "xlisi");
printf("found : name=%s, sex=%c, age=%d, class=%s\n",
xstud.name, xstud.sex, xstud.age, xstud.class);
return 0;
}
Student search(const Student stud[], const char* name) {
Student xstud = {0};
int i = 0;
for (i = 0; i < 10; i++) {
if (strcmp(stud[i].name, name) == 0) {
xstud = stud[i];
break;
}
}
return xstud;
}
void print_info(const Student stud[]) {
int i = 0;
printf("----------------\n");
for (i = 0; i < 10; i++) {
/*
if (strlen(stud[i].name) == 0) {
break;
}
*/
printf("%d. name=%s, sex=%c, age=%d, class=%s\n",
i, stud[i].name, stud[i].sex, stud[i].age, stud[i].class);
}
}
第31课 结构体位字段
#include <stdio.h>
// 定义简单的结构
typedef struct {
char mon;
char tue;
char wed;
char thur;
char fri;
} StudAbsent1;
// 定义位域结构
typedef struct {
unsigned char mon : 1;
unsigned char tue : 1;
unsigned char wed : 1;
unsigned char thur : 1;
unsigned char fri : 1;
} StudAbsent2;
// 存储大小
typedef struct {
unsigned int age : 3;
} Student;
void normal_struct() {
StudAbsent1 absent1 = {0};
absent1.mon = 1;
absent1.thur = 1;
printf("size = %lu\n", sizeof(StudAbsent1));
printf("absent1.mon = %d, absent1.tue = %d, absent1.thur = %d\n",
absent1.mon, absent1.tue, absent1.thur);
}
void bit_struct() {
StudAbsent2 absent2 = {0};
absent2.mon = 1;
absent2.thur = 1;
printf("size = %lu\n", sizeof(StudAbsent2));
printf("absent1.mon = %d, absent1.tue = %d, absent1.thur = %d\n",
absent2.mon, absent2.tue, absent2.thur);
}
void bit_age() {
Student stud = {0};
// 111
stud.age = 7;
printf("stud.age = %d\n", stud.age);
// 1000
stud.age = 8;
printf("stud.age = %d\n", stud.age);
}
void combination() {
// 组合使用
typedef struct {
char name[32];
unsigned age : 3;
unsigned sex : 1;
char class[32];
} Student;
Student stud1 = {.name = "zhangsan", .age = 6, .sex = 0, .class = "二班"};
Student stud2 = {.name = "lisi", .age = 5, .sex = 1, .class = "二班"};
printf("stud1.name = %s, stud1.age = %u, stud1.sex = %u, stud1.class = %s\n",
stud1.name, stud1.age, stud1.sex, stud1.class);
printf("stud2.name = %s, stud2.age = %u, stud2.sex = %u, stud2.class = %s\n",
stud2.name, stud2.age, stud2.sex, stud2.class);
}
int main() {
normal_struct();
printf("----------------------------\n");
bit_struct();
printf("----------------------------\n");
bit_age();
printf("----------------------------\n");
combination();
return 0;
}
第32课 共用体Union
#include <stdio.h>
#include <string.h>
union Data1 {
int a;
char b;
double c;
} t1;
union Data1 t2;
typedef union _Data2 {
int a;
char b;
double c;
} Data2;
typedef union {
int a;
char b;
double c;
} Data3;
// 匿名共用体
union {
int a;
char b;
double c;
} x4;
union Score {
float sc;
char grade[16]; // A+ A B+ B
};
// 共用体和结构体
typedef struct {
char name[32];
// union Score score;
union {
float sc;
char grade[16]; // A+ A B+ B
} score;
unsigned age;
} Student;
int main() {
union Data1 x1 = {0};
Data2 x2 = {0};
Data3 x3 = {0};
Student stud = {0};
printf("x1: a=%d, b=%c, c=%lf\n", x1.a, x1.b, x1.c);
printf("x2: a=%d, b=%c, c=%lf\n", x2.a, x2.b, x2.c);
printf("x3: a=%d, b=%c, c=%lf\n", x3.a, x3.b, x3.c);
x1.a = 100;
x1.b = 'A';
x1.c = 1.2345;
printf("set value >> x1: a=%d, b=%c, c=%lf\n", x1.a, x1.b, x1.c);
strcpy(stud.name, "zhangsan");
strcpy(stud.score.grade, "A+");
stud.score.sc = 99.99;
stud.age = 12;
printf("stud >> name=%s, score.sc=%f, score.grade=%s, age=%u\n",
stud.name, stud.score.sc, stud.score.grade, stud.age);
return 0;
}
第33课 多文件调用
- 知识点1:多文件怎么编译?
- 知识点2:C语言中模块的概念。
- 知识点3:不显示声明函数,可以调用成功吗?
- 知识点4:函数中对传入的数组参数使用sizeof求长度,可以吗?
- 知识点5:使用头文件声明对外接口。
033_a.c文件
#include <stdio.h>
// extern int add(const int a, const int b);
// extern int sum(const int array[]);
int main() {
int array[] = {1, 2, 3};
printf("add(10, 20) = %d\n", add(10, 20));
printf("len1 = %ld\n", sizeof(array)/sizeof(int));
printf("sum(1, 2, 3) = %d\n", sum(array));
return 0;
}
033_b.c
#include <stdio.h>
int add(const int a, const int b) {
return a + b;
}
int sum(const int array[]) {
int sum = 0;
int len = sizeof(array) / sizeof(int);
int i = 0;
printf("len2 = %d\n", len);
for (i = 0; i < len; ++i) {
sum += array[i];
}
return sum;
}
第34课 指针初探
- 什么是指针?
- 指针占用的字节大小是多少?
- 怎样获取变量的地址?
- 基本类型指针定义和使用。
- 字符串指针的定义和使用。
- 结构体指针的定义和使用。
- 怎样输入指针的地址?