C语言从入门到精通

文章目录

C语言

1.helloworld

1.1 pause

#include <stdio.h>		// #:关键标识符,表示引入头文件; include:引入头文件关键字
// stdio.h: 系统标准输入、输出库对应的头文件。 给printf函数服务。
//<>: 使用系统库函数。  "": 用户自定义库函数。
int main(void)			//int: 函数返回值是整型数据。  mian:函数名。程序唯一的入口。必须有,且只有一次。 void:函数调用无需传参。
{
    
    						// 函数体 起始位置

	printf("hello world!\n");	// 将 “hellow world”写到屏幕上。 \n: 回车换行
	system("pause");			// 调用 syatem 函数, 实现暂停功能。 

	return 0;			// 返回当前函数调用 --- 退出程序。  0 要跟 main函数的返回值一一对应。
}// 函数体 结束位置

image-20230428224532173

1.2 cls清屏

#include <stdio.h>
#include <Windows.h>

int main(void)			//int: 函数返回值是整型数据。  mian:函数名。程序唯一的入口。必须有,且只有一次。 void:函数调用无需传参。
{
    
    						// 函数体 起始位置
	printf("-----------hello world!\n");	// 将 “hellow world”写到屏幕上。 \n: 回车换行
	Sleep(2000);
	system("cls");			// 调用 syatem 函数, 实现清屏功能。 

	return 0;			// 返回当前函数调用 --- 退出程序。  0 要跟 main函数的返回值一一对应。
}

image-20230428224555490

image-20230428224559911

1.3 加法运算

#include <stdio.h>


int main(void)
{
    
    

	int a = 3;		// 定义 整型变量 a, 赋初值 3

	int b = 5;		// 定义 整型变量 b, 赋初值 5
	int c;			// 定义 整型变量 c, 无初值
	
	c = a + b;		// a + b ,将结果 赋值 给 c

	printf("hello world\n");

	// %d:格式匹配符,匹配整型数据

	printf("%d\n", c /*注释*/ );	


	printf("c = %d\n", c);

	printf("%d + %d = %d\n", a, b, c);

	printf("%d + %d = &d\n", a, b, a+b);

	return 0;
}

image-20230428224633287

1.4 hello

#include <stdio.h>

#define PI 3.14				// 定义常量 PI  宏定义

// 我是一个单行注释君

int main(void)
{
    
    
	printf("helloworld\n");
		
/*
wo shi 
一个多行注释 
sir
*/
	printf("%d\n, PI");
	
	return 0;
}

2 常量变量和数据类型

2.1 常量

#include <stdio.h>

#define PI 3.1415			// 常量

int main(void)
{
    
    
	// 圆的面积  s = PI x 半径的平方
	// 圆的周长  l = 2 * PI * r	
	//int r = 3;				// 变量
	const int r = 3;		// 只读变量

	float s = PI * r * r;
	float l = 2 * PI * r;

	//printf("圆的周长为:%f\n", l);  //18.849001
	//printf("圆的面积为:%f\n", s);  //28.273500

	printf("圆的周长为:%.2f\n", l);	// 指定小数点后保留2位, 对第3位做,4舍五入
	printf("圆的面积为:%.2f\n", s);	// 指定小数点后保留2位

	return 0;
}

image-20230428224936926

2.2 变量

#include <stdio.h>

int main(void)
{
    
    
	int a;   // 显示的做变量a的声明

	a = 56;			// 变量使用、变量赋值。

	return 0;
}

image-20230428225003504

2.3 sizeof数据类型大小

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define N 1024		// 定义常量

int main(void)
{
    
    
	int a = 10;		// 定义变量: 三要素

	short b = 20;

	long c = 30;
	long long d = 40;

	printf("================a===%d\n", sizeof(a));
	printf("================b===%d\n", sizeof(b));
	printf("================c===%d\n", sizeof(c));
	printf("================d===%d\n", sizeof(d));

	printf("int 大小为:%d\n", sizeof(int));
	printf("short 大小为:%d\n", sizeof(short));
	printf("long 大小为:%d\n", sizeof(long));
	printf("long long 大小为:%d\n", sizeof(long long));

	system("pause");

	return EXIT_SUCCESS;
}

image-20230428225159946

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define N 1024		// 定义常量
int main(void)
{
    
    
	int a = 3;
	short b = 4;
	long c = 5;			// 5L  5l
	long long d = 6;  // 5LL  5ll

	printf("sizeof(int) = %d\n", sizeof(int));
	printf("sizeof(short) = %d\n", sizeof(short));
	printf("sizeof(long) = %d\n", sizeof(long));
	printf("sizeof(long long) = %d\n", sizeof(long long));

	printf("--------------------------------------\n");

	unsigned int aun = 3;		// 3u
	unsigned short bun = 4;		// 3u
	unsigned long cun = 5;		// 3lu
	unsigned long long dun = 6;	// 3llu

	printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int)); // aun
	printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
	printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
	printf("sizeof(unsigned long long) = %d\n", sizeof(unsigned long long));
}

image-20230428225234486

2.4 无符号整型

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	size_t var = 10;
	printf("var = %u\n", var);

	unsigned int a = 10u;  // 简写成 unsigned int a = 10;
	unsigned short b = 20u;// 简写成 unsigned short b = 20;
	unsigned long c = 30Lu; 
	unsigned long long d = 40LLu;

	printf("unsigned int 型数据值:%u\n", a);
	printf("unsigned short 型数据值:%hu\n", b);
	printf("unsigned long 型数据值:%lu\n", c);
	printf("unsigned long long 型数据值:%llu\n", d);

	system("pause");

	return EXIT_SUCCESS;
}

image-20230428225312283

2.5 字符类型

2.5.1 字符类型简介

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char ch = 'A';  // 65

	//printf("1 ch = %c\n", ch);
	printf("1 ch = %d\n", ch);

	ch = 'm'; //

	//printf("2 ch = %c\n", ch);
	printf("2 ch = %d\n", ch);

	//ch = 97;
	ch = 'a';	// 97

	//printf("3 ch = %c\n", ch);
	printf("3 ch = %d\n", ch);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428225447304

2.5.2 字符类型运算

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// A -- 65 a -- 97 
int main(void)
{
    
    
	char ch = 'M';
	char var = '5';

	printf("ch = %c\n", ch + 32);
	printf("var = %c\n", var + 4);

	printf("'\\n\'的值为=%d\n", '\n');

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428225509912

2.6 实数型

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	float m = 3.145;
	double n = 4.566545;

	printf("m = %08.2f\n", m);
	printf("n = %08.3lf\n", n);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428225608697

2.7 进制和转换

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 0x2C;  // 等价 0x2c 

	printf("10进制显示 a = %d\n", a);
	printf("8进制显示 a = %o\n", a);
	printf("16进制显示 a = %x\n", a);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428225651509

2.8 数据溢出

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    

	char ch = 127+1;		// -128 -- 127

	printf("ch = %d\n", ch);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428225730661

3. 运算符和分支循环语句

3.1 字符串输出

3.1.1 字符数组输出

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char ch = 'a';

	printf("ch = %c\n", ch);

	char str[20] = "hello world";

	printf("str = %s\n", str);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428230254034

3.1.2 字符数组格式化输出(%-15s)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str[] = "hello world";

	printf("str = |%-15s|\n", str);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428230325442

3.1.2 字符输出 (putchar)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	putchar(97);  // 'a' == 97
	putchar('b');
	putchar('c');
	putchar('d');

	putchar('abcZ');

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428231115769

3.2 格式化输入

3.2.1 获取单个输入

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 获取用户输入 整数
int main(void)
{
    
    
	int a;

	scanf("%d", &a);		// &:表示取出变量a的地址。描述a的空间

	printf("a = %d\n", a);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428232733911

3.2.2 获取多个输入

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 获取用户输入 整数
int main(void)
{
    
    
	char ch1, ch2, ch3;	//	连续定义同类型多个变量。

	scanf("%c%c%c", &ch1, &ch2, &ch3);	// &:表示取出变量ch的地址。描述a的空间

	printf("ch1 = %c\n", ch1);
	printf("ch2 = %c\n", ch2);
	printf("ch3 = %c\n", ch3);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428234414143

3.2.3 连续相同输入

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 获取用户输入 整数
int main(void)
{
    
    
	int a1, a2, a3;	//	连续定义同类型多个变量。

	scanf("%d %d %d", &a1, &a2, &a3);	// &:表示取出变量ch的地址。描述a的空间

	printf("a1 = %d\n", a1);
	printf("a2 = %d\n", a2);
	printf("a3 = %d\n", a3);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428235349019

3.2.4 字符数组输入

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 获取用户输入 整数
int main(void)
{
    
    
	char a[5];			// 大小为5字节的数组

	scanf("%s", a);		// 接收用户键盘输入,写入数组a中

	printf("a = %s\n", a);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230428235429971

3.2.5 getchar获取字符输入

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 获取用户输入 整数
int main(void)
{
    
    
	char ch;

	ch = getchar();		// 接收用户输入,返回接收到的ASCII码

	printf("ch = %c\n", ch);

	putchar(ch);
	putchar('\n');

	system("pause");

	return EXIT_SUCCESS;
}

image-20230428235615376

3.3 算数运算符

3.3.1 数学运算

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 10;
	int b = 20;
	int c = a * b;
	int d = 34 / 10;  // 0.5
	//int m = 98 / 0;
	printf("d = %d\n", d);
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429094537763

3.3.2 自增运算(a++与++a)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(void)
{
    
    
	int a = 10;
	int b = 50;

	printf("a = %d\n", a++);  // 先取值给%d, 在自增

	printf("----a = %d", a);


	printf("b = %d\n", ++b);  // 先自增,再取值。 

	printf("----b = %d\n", b);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429094647742

3.4 逻辑运算

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 34;
	int b = 0;

	char str[10] = "hello";

	++str[0];

	printf("a = %d\n", !a);
	printf("b = %d\n", !b);

	printf("======%d\n", a && !b);

	printf("------%d\n", !a || b);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429095850742

3.5 三目运算

3.5.1 三目运算使用

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 
int main(void)
{
	int a = 40;
	int b = 4;

	int m = a < b ? 69 : a < b ? 3 : 5; 

	printf("m = %d\n", m);

	printf("%d\n", a > b ? 69 : 100);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429102326804

3.5.2 三目运算案例2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 
int main(void)
{
    
    
	int a = 10, b = 20, c = 30;
	int x = (a = 1, c = 5, b = 2);

	printf("x = %d\n", x);
	printf("a = %d\n", a);
	printf("b = %d\n", b);
	printf("c = %d\n", c);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429103340424

3.6 隐式转换和强制类型转换

3.6.1 隐式转换

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 隐式类型转换。
int main(void)
{
    
    
	int a = 321;

	char ch = a;

	printf("ch = %d\n", ch);// 字符类型转换为整数类型

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429104308349

3.6.2 强制类型转换

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 强制类型转换
int main(int var)
{
    
    
	//int *p = (int *)malloc(100);

	float price = 3.6;
	int weight = 4;

	//double sum = (int)price * weight;

	double sum = (int)(price * weight);

	printf("价格:%lf\n", sum);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429104359351

3.7 if 语句

3.7.1 if …else… 语句

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a;
	scanf("%d", &a);

	if (a > 0)
	{
    
    
		printf("a > 0\n");
	}
	else
	{
    
    
		printf("a <= 0\n");
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429105914555

3.7.2 else if语句

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int score;		// 100--90 优 90 -- 70 良好 70 -- 60 及格  < 60 差劲

	printf("请输入学生成绩:");
	scanf("%d", &score);

	if (score >= 90 && score <= 100)
	{
    
    
		printf("优秀\n");
	}
	else if (score < 90 && score >= 70)
	{
    
    
		printf("良好\n");
	}
	else if (score < 70 && score >= 60)
	{
    
    
		printf("及格\n");
	}
	else
	{
    
    
		printf("不及格\n");
	}



	system("pause");
	return EXIT_SUCCESS;
}

image-20230429110004781

3.7.3 if else if else 语句

int main0703(void)
{
    
    
	int pig1, pig2, pig3;

	// if (pig1 > pig2 && pig1 > pig3)
	// pig1 > pig2 ? pig1 : pig2;

	printf("请输入三只小猪的体重:");
	scanf("%d %d %d", &pig1, &pig2, &pig3);

	if (pig1 > pig2)		// 满足,说明pig1最重
	{
    
    
		if (pig1 > pig3)
		{
    
    
			printf("第一只小猪最重,体重为:%d\n", pig1);
		}
		else
		{
    
    
			printf("第3只小猪最重,体重为:%d\n", pig3);
		}
	}   
	else
	{
    
    
		if (pig2 > pig3)
		{
    
    
			printf("第2只小猪最重,体重为:%d\n", pig2);
		}
		else
		{
    
    
			printf("第3只小猪最重,体重为:%d\n", pig3);
		}
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429110624273

3.8 switch

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int score;
	scanf("%d", &score);

	switch (score / 10)
	{
    
    
	case 10:		//	 100 -- 90 优秀
	case 9:
		printf("优秀\n");
		break;
	case 8:			//   70 -- 90 良好
	case 7:
		printf("良好\n");
		//break;
	case 6:		   // 70 - 60 及格
		printf("及格\n");
		//break;
	default:
		printf("不及格\n");
		break;
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429110812193

3.9 while 循环

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 
int main(void)
{
    
    
	int num = 1;

	while (num <= 100)
	{
    
    
		if ((num % 7 == 0) || (num % 10 == 7) || (num / 10 == 7))		// 个位、10位、7的倍数
		{
    
    
			printf("敲桌子\n");
		}
		else
		{
    
    
			printf("%d\n", num);
		}
		num++;  // 递增
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429151130802

3.10 do while

3.10.1 do…whie语句

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// while 的基础用法
int main(void)
{
    
    
	int a = 1;
	do
	{
    
    
		a++;
		printf("a = %d\n", a);
	} while (a < 10);

	system("pause");
	return EXIT_SUCCESS;
}

3.10.2 水仙花数问题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 水仙花数:一个三位数。各个位上的数字的立方和等于本数字。 
int main(void)
{
    
    
	int a, b, c;
	int num = 100;

	do {
    
    
		a = num % 10;		// 个位
		b = num / 10 % 10;	// 十位
		c = num / 100;		// 百位

		if (a*a*a + b*b*b + c*c*c == num)
		{
    
    
			printf("%d\n", num);
		}
		num++;

	} while (num < 1000);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429151212274

4. for循环和数组排序

4.1 for 循环

4.1.1 基础的for循环

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>



int main(void)
{
    
    
	srand(time(NULL));	//种随机数种子。

	int n = 0;
	int num = rand() % 100;  // 生成随机数

	for (;;)  // while(1)
	{
    
    
		printf("请输入猜测的数字:");
		scanf("%d", &n);
		if (n < num)
		{
    
    						// for、while、if 如果执行语句只有一条。 { } 可以省略
			printf("猜小了\n");
		}
		else if (n > num)	
			printf("猜大了\n");	
		else
		{
    
    
			printf("猜中!!!\n");
			break;			// 跳出
		}
	}
	printf("本尊是:%d\n", num);

	system("pause");

	return 0;
}

image-20230429151839684

4.1.2 省略的表达式1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 基础for循环
int main(void)
{
    
    
	int i = 1;		// 循环因子
	int sum = 0;

	for (; i <= 100; i++)
	{
    
    
		sum = sum + i;  //sum += i;
	}

	printf("sum = %d\n", sum);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429151938606

4.1.3 省略表达式2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 基础for循环
int main(void)
{
    
    
	int i = 1;		// 循环因子
	int sum = 0;

	for (; i <= 100; )
	{
    
    
		sum = sum + i;  //sum += i;
		i++;
	}

	printf("sum = %d\n", sum);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429152031949

4.1.4 省略表达式123

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 省略表达式123,无限循环
int main(void)
{
    
    
	int i = 0;		// 循环因子

	//for (;1;)		// 死循环。while(k=1)
	for (;;)
	{
    
    
		printf("i = %d\n", i);
		i++;
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429152115130

4.1.5 多个表达式

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 表达式有多个
int main(void)
{
    
    
	int i = 0;
	int a = 0;

	for (i = 1, a = 3; a < 20; i++)
	{
    
    
		printf("i = %d\n", i);
		printf("a = %d\n", a);
		a += 5;
	}
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429152216527

4.2 猜数字游戏

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    
    
	srand(time(NULL));	//种随机数种子。

	int n = 0;
	int num = rand() % 100;  // 生成随机数

	for (;;)  // while(1)
	{
    
    
		printf("请输入猜测的数字:");
		scanf("%d", &n);
		if (n < num)
		{
    
    						// for、while、if 如果执行语句只有一条。 { } 可以省略
			printf("猜小了\n");
		}
		else if (n > num)	
			printf("猜大了\n");	
		else
		{
    
    
			printf("猜中!!!\n");
			break;			// 跳出
		}
	}
	printf("本尊是:%d\n", num);

	system("pause");

	return 0;
}

image-20230429152400418

4.3 模拟电子表

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

int main(void)
{
    
    
	int i, j, k;

	// 小时
	for (i = 0; i < 24; i++)
	{
    
    
		// 分钟
		for (j = 0; j < 60; j++)
		{
    
    
			// 秒
			for (k = 0; k < 60; k++)
			{
    
    
				printf("%02d:%02d:%02d\n", i, j, k);
				Sleep(960);
				system("cls");  // 清屏
			}
		}
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429152600758

4.4 9*9乘法表

4.4.1 正序9*9乘法表

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 正序99乘法表
int main(void)
{
    
    
	for (size_t i = 1; i <= 9; i++)
	{
    
    
		for (size_t j = 1; j <= i; j++)
		{
    
    
			printf("%dx%d=%d\t", j, i, j * i);
		}
		printf("\n");
	}
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429152720977

4.4.2 倒序9*9乘法表

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


// 倒序 99 乘法表
int main(void)
{
    
    
	int i, j;

	for (i = 9; i >= 1; i--)		// 行
	{
    
    
		for (j = 1; j <= i; j++)		// 列
		{
    
    
			printf("%dx%d=%d\t", j, i, j * i);
		}
		putchar('\n');
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429152754836

4.5 continue

4.5.1 for 循环中continue

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	for (size_t i = 0; i < 5; i++)
	{
    
    
		if (i == 3)
		{
    
    
			continue;
		}
		printf("i = %d\n", i);
		printf("============1=========\n");
		printf("============2=========\n");
		printf("=============3========\n");
		printf("============4=========\n");
		printf("=============5========\n");

	}
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429160516753

4.5.2 while循环中continue

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int num = 5;

	//while (num--)  // 当num自减为 0 时循环终止。  等价于 while (num-- != 0)

	while (num-- != 0) // 当num自减为 0 时循环终止。
	{
    
    
		printf("num = %d\n", num);
		if (num == 3)
		{
    
    
			continue;
		}
		printf("============1=========\n");
		printf("============2=========\n");
		printf("=============3========\n");
		printf("============4=========\n");
		printf("=============5========\n");
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429160604553

4.6 goto语句

4.6.1 goto语句简介

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	printf("============1==========\n");
	printf("============2==========\n");
	goto LABLE;

	printf("============3==========\n");
	printf("============4==========\n");
	printf("============5==========\n");
	printf("============6==========\n");
	printf("============7==========\n");

LABLE:
	printf("============8==========\n");
	printf("============9==========\n");
	printf("============10==========\n");

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429160650249

4.6.2 循环语句中goto

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int i = 0;

	for (i = 0; i < 10; i++)
	{
    
    
		if (i == 5)
			goto ABX234;

		printf("i = %d\n", i);
	}

	for (int j = 0; j < 20; j++)
	{
    
    
	ABX234:
		printf("j = %d\n", j);
	}

	system("pause");
	return 0;
}

image-20230429160758657

4.7 数组

4.7.1 数组简介

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 5, b = 29, c = 10;

	int arr[10] = {
    
     1, 2 ,4, 6, 76, 8, 90 ,4, 3, 6 };  //int a = 109;

	printf("&arr[0] = %p\n", &arr[0]);  // 取数组首元素的地址

	printf("arr = %p\n", arr);		// 数组名

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429161005455

4.7.2 数组大小与个数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 5, b = 29, c = 10;

	int arr[12] = {
    
     1, 2 ,4, 6, 76, 8, 90 ,4, 3, 6 , 6, 8 };  //int a = 109;

	printf("数组大小:%u\n", sizeof(arr));

	printf("数组元素的大小:%u\n", sizeof(arr[0]));

	printf("数组元素个数:%d\n", sizeof(arr) / sizeof(arr[0]));

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429161052082

4.7.3 数组初始化

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 数组初始化
int main(void)
{
    
    
	int arr[10];  //int a = 109;
	arr[0] = 5;
	arr[1] = 6;
	arr[2] = 7;

	int n = sizeof(arr) / sizeof(arr[0]);

	for (size_t i = 0; i < n; i++)
	{
    
    
		printf("%d\n", arr[i]);
	}

	system("pause");
	return EXIT_SUCCESS;
}


image-20230429161114564

4.8 数组逆序

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 数组元素逆序
int main(void)
{
    
    
	int arr[] = {
    
     1, 6, 8, 0, 4, 3, 9, 2 };  // {2, 9, 3, 4, 0, 8, 6, 1}
	int len = sizeof(arr) / sizeof(arr[0]); //数组元素个数

	int i = 0;				// i表示数组的首元素下标
	int j = len - 1;		// 表示数组的最后一个元素下标
	int temp = 0;		// 临时变量 

	// 交换 数组元素,做逆序
	while (i < j)
	{
    
    
		temp = arr[i];		// 三杯水法变量交换
		arr[i] = arr[j];
		arr[j] = temp;
		i++;
		j--;
	}
	// 打印交互后的 数组
	for (size_t n = 0; n < len; n++)
	{
    
    
		printf("%d ", arr[n]);
	}
	printf("\n");

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429161218262

4.9 冒泡排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int arr[] = {
    
     12, 32, 14, 62, 27, 8, 89 };

	int n = sizeof(arr) / sizeof(arr[0]);	// 数组元素个数

	int temp = 0;		// 临时变量

	for (size_t i = 0; i < n; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	printf("\n");

	// 完成乱序数组的冒泡排序。
	for (size_t i = 0; i < n - 1; i++)		// 外层控制行
	{
    
    
		for (size_t j = 0; j < n - 1 - i; j++)	// 内层控制列
		{
    
    
			if (arr[j] > arr[j + 1])		// 满足条件 三杯水交换
			{
    
    
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}

	// 打印排序后的数组,确定正确性。
	for (size_t i = 0; i < n; i++)
	{
    
    
		printf("%d ",arr[i]);
	}
	printf("\n");

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429161328552

5. 二维数组、字符串、函数

5.1 二维数组

5.1.1 二维数组定义

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int arr[3][4] = {
    
     {
    
    2, 7, 8, 5},
						{
    
    75, 8, 9, 8},
						{
    
    26, 37, 99, 9} };
	for (size_t i = 0; i < 3; i++)		//行
	{
    
    
		for (size_t j = 0; j < 4; j++)  //列
		{
    
    
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}

	printf("数组的大小为:%u\n", sizeof(arr));
	printf("数组行的大小:%u\n", sizeof(arr[0]));
	printf("数组一个元素的大小:%u\n", sizeof(arr[0][0]));

	printf("行数=总大小/一行大小:%d\n", sizeof(arr) / sizeof(arr[0]));
	printf("列数=行大小/一个元素大小:%d\n", sizeof(arr[0]) / sizeof(arr[0][0]));

	printf("arr= %p\n", arr);
	printf("&arr[0] = %p\n", &arr[0][0]);
	printf("arr[0] = %p\n", arr[0]);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429161625727

5.1.2 二维数组练习

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	/*
	56 78 92
	45 67 93
	29 83 88
	93 56 89
	72 83 81
	*/
	int scores[5][3] = {
    
     1, 2, 4, 5, 6, 7, 8, 9 };

	int row = sizeof(scores) / sizeof(scores[0]);
	int col = sizeof(scores[0]) / sizeof(scores[0][0]);

	// 获取 5 名学生、3门功课成绩
	for (size_t i = 0; i < row; i++)
	{
    
    
		for (size_t j = 0; j < col; j++)
		{
    
    
			scanf("%c", &scores[i][j]);
		}
	}
	// 求一个学生的总成绩
	for (size_t i = 0; i < row; i++) // 每个学生
	{
    
    
		int sum = 0;
		for (size_t j = 0; j < col; j++)// 每个学生的成绩
		{
    
    
			sum += scores[i][j];
		}
		printf("第%d个学生的总成绩为:%d\n", i + 1, sum);
	}
	//求一门功课的总成绩
	for (size_t i = 0; i < col; i++)  // 第几门功课
	{
    
    
		int sum = 0;
		for (size_t j = 0; j < row; j++)  // 每门功课的第几个学生
		{
    
    
			sum += scores[j][i];
		}
		printf("第%d门功课的总成绩为:%d\n", i + 1, sum);
	}


	system("pause");
	return EXIT_SUCCESS;
}

image-20230429161837988

5.1.3 多维数组

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a[3][4][2] =
	{
    
    
		{
    
    
			{
    
    1, 2},
			{
    
    2, 3},
			{
    
    4, 5},
			{
    
    5, 6}
		},
		{
    
    
			{
    
    45, 67},
			{
    
    78, 90},
			{
    
    12, 6},
			{
    
    45, 9}
		},
		{
    
    
			{
    
     45, 67 },
			{
    
     78, 90 },
			{
    
     12, 6 },
			{
    
     45, 9 }
		}
	};

	//int arr[2][3][5] = {1, 2, 4, 5, 6, 7, 8 , 9, 0, 0, 7, 9, 8};
	for (size_t i = 0; i < 3; i++)
	{
    
    
		for (size_t j = 0; j < 4; j++)
		{
    
    
			for (size_t k = 0; k < 2; k++)
			{
    
    
				printf("%d ", a[i][j][k]);
			}
			printf("\n");
		}
		printf("\n\n");
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429161930939

5.2 字符数组和字符串

5.2.1 字符数组打印

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str[6] = {
    
     'h', 'e', 'l', 'l', 'o', '\0' };

	char str2[] = "world";  //  == {'w', 'o', 'r', 'l', 'd', '\0'}

	printf("%s\n", str2);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429162035940

5.2.2 统计字符串中每个字符出现的次数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str[11] = {
    
     0 };		// helloworld -->  26个英文字母 a-z  a:97 d:100

	// scanf("%s", str);
	for (size_t i = 0; i < 10; i++)
	{
    
    
		scanf("%c", &str[i]);
	}

	int count[26] = {
    
     0 };  // 代表26个英文字母出现的次数。 

	for (size_t i = 0; i < 11; i++)
	{
    
    
		int index = str[i] - 'a';	// 用户输入的字符在 count数组中的下标值。
		count[index]++;
	}

	for (size_t i = 0; i < 26; i++)
	{
    
    
		if (count[i] != 0)
		{
    
    
			printf("%c字符在字符串中出现 %d 次\n", i + 'a', count[i]);
		}
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429162139682

5.2.3 scanf获取字符串

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str[100];

	//scanf("%s", str);
	scanf("%[^\n]s", str);

	printf("%s\n", str);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429164426179

5.3 字符串操作函数

5.3.1 gets 函数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//gets
int main(void)
{
    
    
	char str[10];
	printf("请输入字符串:");
	gets(str);
	printf("%s\n", str);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429180602574

5.3.2 fgets 函数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str[10];

	printf("获取的字符串为:%s", fgets(str, sizeof(str), stdin));

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429180741162

5.3.3 puts 函数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//puts
int main(void)
{
    
    
	char str[] = "hello world\n";

	int ret = puts(str);	// puts("hello world");

	printf("ret = %d\n", ret);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429180852681

5.3.4 fputs函数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//fputs
int main(void)
{
    
    
	char str[] = "hello world\n";

	//int ret = fputs(str, stdout);	// 

	int ret = fputs("hello world\n", stdout);

	printf("ret = %d\n", ret);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429181023656

5.3.5 strlen函数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//fputs
// strlen() 函数 : 获取字符串的 有效长度: 不包含\0
int main(void)
{
    
    
	char str[] = "hello\0world";

	printf("sizeof(str) = %u\n", sizeof(str));

	printf("strlen(str) = %u\n", strlen(str));

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429181225681

5.3.6 实现strlen函数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//fputs
int main(void)
{
    
    
	char str[] = "hello world";

	int i = 0;
	while (str[i] != '\0')
	{
    
    
		i++;
	}
	printf("%d\n", i);

	// 等价于 printf("%d\n", strlen(str));

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429182149876

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 进阶版
int main(void)
{
    
    
	char str[] = "hello\0world";

	int i = 0;
	while (str[i++]);
	printf("%d\n", i - 1);
	// 等价于 printf("%d\n", strlen(str));
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429182327307

5.4 字符串拼接

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str1[] = "hello";  // [ h e l l o \0 ]
	char str2[] = "world";

	char str3[100];

	int i = 0;		// 循环 str1
	while (str1[i] != '\0')   // '\0' != '\0'
	{
    
    
		str3[i] = str1[i];  // 循环着将str1中的每一个元素,交给str3
		i++;
	}					// str3=[h e l l o]
	//printf("%d\n", i);  --> 5

	int j = 0;		// 循环 str2
	while (str2[j]) // 等价于 while(str2[j] !='\0') 等价于 while(str2[j] != 0)
	{
    
    
		str3[i + j] = str2[j];
		j++;
	}					// str3=[h e l l o w o r l d]

	// 手动添加 \0 字符串结束标记
	str3[i + j] = '\0';

	printf("str3 = %s\n", str3);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429182546697

5.5 函数

5.5.1 函数声明定义

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void bubble_sort(int arr[]);  // 函数声明 int arr[] 函数形参
void print_arr(int arr[]);

int main(void)
{
    
    
	printf("add = %d\n", add(2, 6));

	int arr[] = {
    
     54, 5, 16, 34 , 6, 9, 34, 1, 7, 93 };

	bubble_sort(arr);

	print_arr(arr);

	system("pause");

	return EXIT_SUCCESS;   // 底层 调用 _exit(); 做退出
}

void print_arr(int arr[])
{
    
    
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
}

void bubble_sort(int arr[])
{
    
    
	int i, j, temp;

	for (i = 0; i < 10 - 1; i++)
	{
    
    
		for (j = 0; j < 10 - 1 - i; j++)
		{
    
    
			if (arr[j] < arr[j + 1])
			{
    
    
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}


int add(int a, int b)
{
    
    
	return a + b;
}

image-20230429184608675

5.5.2 exit函数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int func(int a, char ch);

int main(void)
{
    
    
	int ret = func(10, 'a');

	printf("ret = %d\n", ret);

	system("pause");
	//return EXIT_SUCCESS;
	exit(EXIT_SUCCESS);
}

int func(int a, char ch)
{
    
    
	printf("a = %d\n", a);

	printf("ch = %c\n", ch);

	//return 10;
	exit(10);
}

image-20230429184721613

6. 指针

6.1 指针基础

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 10;

	int* p = &a;

	//*p = 2000;
	a = 350;

	//printf("a = %d\n", a);
	printf("*p = %d\n", *p);

	printf("sizeof(int *) = %u\n", sizeof(int*));
	printf("sizeof(short *) = %u\n", sizeof(short*));
	printf("sizeof(char *) = %u\n", sizeof(char*));
	printf("sizeof(long *) = %u\n", sizeof(long*));
	printf("sizeof(double *) = %u\n", sizeof(double*));

	printf("sizeof(void *) = %u\n", sizeof(void*));



	system("pause");
	return EXIT_SUCCESS;
}

image-20230429185006616

6.2 野指针和空指针

6.2.1 野指针

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 野指针1
int main(void)
{
    
    
	int *p;
	*p = 2000;
	printf("*p = %d\n", *p);
	system("pause");
	return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
// 野指针2
int main(void)
{
    
    
	int m;
	//int *p = 1000;   // 0-255 确定留给操作系统
	int *p = 0x0bfcde0000;

	p = &m;

	*p = 2000;

	printf("*p = %d\n", *p);

	system("pause");
	return EXIT_SUCCESS;
}

6.2.2 空指针

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 空指针
int main(void)
{
    
    
	int *p = NULL;   // NULL == 0

	// .....
	// lllll

	// .....

	if (p != NULL)
	{
    
    
		*p = 300;
		printf("*p = %d\n", *p);
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429185321866

6.3 泛型指针(void **)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 345;

	char ch = 'R';

	void* p;  // 万能指针、泛型指针
	//p = &a;
	p = &ch;

	printf("%c\n", *(char*)p);


	system("pause");
	return EXIT_SUCCESS;
}

image-20230429185417944

6.4 const作用

6.4.1 修饰变量

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>



// 修饰变量
int main(void)
{
    
    
	const int a = 20;

	int* p = &a;

	*p = 650;

	printf("%d\n", a);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429185613757

6.4.2 const int *p

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>



// const int *p;
int main(void)
{
    
    
	int a = 10;
	int b = 30;
	const int* p = &a;

	//*p = 500;
	p = &b;
	printf("*p=%d", *p);
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429185716146

6.4.3 int const *p

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 10;
	int b = 30;
	int const* p = &a;

	//*p = 300;
	p = &b;
	printf("*p=%d", *p);
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429185808956

6.4.4 int * const p

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 10;
	int b = 30;
	int* const p = &a;

	*p = 300;
	//p = &b;

	printf("a = %d\n", *p);
	printf("a = %d\n", a);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429185906932

6.4.5 const int * const p

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(void)
{
    
    
	int a = 10;
	int b = 30;
	const int* const p = &a;

	//*p = 300;
	//p = &b;

	printf("a = %d\n", *p);
	printf("a = %d\n", a);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429185945068

6.5 指针和数组

6.5.1 数组操作

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


// 操作数组元素的 4 种方法
int main(void)
{
    
    
	int a[] = {
    
     1, 2, 4, 5, 6, 7, 8, 9, 0 };

	int n = sizeof(a) / sizeof(a[0]);

	int* p = a;

	printf("sizeof(a) = %u\n", sizeof(a));
	printf("sizeof(p) = %u\n", sizeof(p));

	for (size_t i = 0; i < n; i++)
	{
    
    
		//printf("%d "), a[i];
		//printf("%d ", *(a+i));  // a[i] == *(a+i)
		//printf("%d ", p[i]);
		printf("%d ", *(p + i));  // p[i] = *(p+i)
	}
	printf("\n");
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429190119092

6.5.2 指针操作

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


// 使用指针++操作数组元素
int main(void)
{
    
    
	int arr[] = {
    
     1, 2, 4, 5, 6, 7, 8, 9, 0 };
	int* p = arr;
	int n = sizeof(arr) / sizeof(arr[0]);

	printf("first p = %p\n", p);

	for (size_t i = 0; i < n; i++)
	{
    
    
		printf("%d ", *p);
		p++;  // p = p+1;   一次加过一个int大小。 一个元素。
	}
	putchar('\n');

	printf("last p = %p\n", p);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429190151202

6.6 指针算法运算

6.6.1 指针±操作

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


// 指针在数组中 +- 整数
int main(void)
{
    
    
	int a[] = {
    
     1, 2, 4, 5, 6, 7, 8, 9, 0 };
	//int *p = a; // a == &a[0];

	int* p = &a[5];

	printf("p-2 = %p\n", p - 2);

	printf("&a[3] = %p\n", &a[3]);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429190257476

6.6.2 &数组名 +1操作

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


// &数组名 +1
int main(void)
{
    
    
	short a[10] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

	printf("a = %p\n", a);
	printf("&a[0] = %p\n", &a[0]);

	printf("a+1 = %p\n", a + 1);

	printf("&a   = %p\n", &a);
	printf("&a+1 = %p\n", &a + 1);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429190335022

6.6.3 指针加减操作

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>



// 指针加减指针
int main(void)
{
    
    
	int a[10] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	int* p = a;

	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%d ", *p);
		p++;
	}
	printf("p - a = %d\n", p - a);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429190413455

6.6.4 strlen实现方式(指针)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int mystrlen(char arr[]);

int main(void)
{
    
    
	char abc[] = "hello world";

	int ret = mystrlen2(abc);  // 实际参数 abc

	printf("ret = %d\n", ret);

	system("pause");
	return EXIT_SUCCESS;
}

// 借助数组 实现 
int mystrlen(char str[])
{
    
    
	int i = 0;
	while (str[i] != '\0')
	{
    
    
		i++;
	}
	return i;
}

// 借助指针++ 实现 
int mystrlen2(char str[])
{
    
    
	char* p = str;
	while (*p != '\0')
	{
    
    
		p++;
	}
	return p - str;  // 返回数组元素的个数。
}


image-20230429190738117

6.7 指针的比较运算

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a[] = {
    
     1, 2, 4, 5, 6, 7, 8, 9, 0 };

	int *p = &a[0];

	if (p > a)
		printf("成立\n");

	else if (p < a)
		printf("不成立\n");

	else
		printf("== \n");



	system("pause");
	return EXIT_SUCCESS;
}

image-20230429191821573

6.8 指针数组

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 指针数组1
int main(void)
{
    
    
	int a = 10;
	int b = 20;
	int c = 30;

	int* p1 = &a;
	int* p2 = &b;
	int* p3 = &c;

	int* arr[] = {
    
     p1, p2, p3 };  // 整型指针数组arr, 存的都是整型地址。

	printf("*(arr[0]) = %d\n", *(*(arr + 0)));  //arr[0] ==  *(arr+0)

	printf("*(arr[0]) = %d\n", **arr);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429192007206

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


// 指针数组2
int main(void)
{
    
    
	int a[] = {
    
     10 };
	int b[] = {
    
     20 };
	int c[] = {
    
     30 };

	int* arr[] = {
    
     a, b, c };  // 整型指针数组arr, 存的都是整型地址。

	printf("*(arr[0]) = %d\n", *(*(arr + 0)));  //arr[0] ==  *(arr+0)

	printf("*(arr[0]) = %d\n", **arr);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429192118085

6.9 多级指针

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int a = 10;
	int* p = &a;
	int** pp = &p;
	// int **pp = &(&a); 不允许!!
	int*** ppp = &pp;

	printf("***ppp = %d\n", ***ppp);
	printf("**pp = %d\n", **pp);
	printf("*p = %d\n", *p);
	printf("a = %d\n", a);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429193350987

7. 指针和字符串

7.1 传值和传址

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int swap(int, int);  // 函数声明
int swap2(int*, int*);

int main(void)
{
    
    
	int m = 23;
	int n = 57;

	printf("--before-- m = %d, n = %d\n", m, n);
	// 函数调用
	//swap(m, n);  // m/n 实参

	swap2(&m, &n);

	printf("--after-- m = %d, n = %d\n", m, n);

	system("pause");
	return EXIT_SUCCESS;
}

int swap2(int* a, int* b)	// 形参a、b, 需传地址值
{
    
    
	int tmp = 0;
	tmp = *a;
	*a = *b;
	*b = tmp;
	return 0;
}

// 函数定义
int swap(int a, int b)	// a/b 形参
{
    
    
	int tmp = 0;

	tmp = a;
	a = b;
	b = tmp;

	return 0;
}

image-20230429194840054

7.2 数组做函数参数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//void BubbleSort(int arr[])  // void BubbleSort(int *arr)

void BubbleSort(int* arr, int n)
{
    
    
	for (int i = 0; i < n - 1; i++)
	{
    
    
		for (int j = 0; j < n - 1 - i; j++)
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}


int main(void)
{
    
    
	int arr[] = {
    
     5, 89, 3, 22, 40, 31, 9, 22, 67, 28, 45, 78 };

	printf("main: sizeof(arr) = %d\n", sizeof(arr));

	int n = sizeof(arr) / sizeof(arr[0]);

	BubbleSort(arr, n);

	for (size_t i = 0; i < n; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	printf("\n");

	system("pause");
	return EXIT_SUCCESS;
}


image-20230429194920350

7.3 指针做函数返回值

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int m = 100;   // 全局变量 对应空间消失 ==> 程序结束。

int* test_func2(int a, int b)
{
    
    
	int p = 1234;  // 局部变量
	//return &m;

	return &p;
}

int main(void)
{
    
    
	int* ret = NULL;  // NULL == 0

	ret = test_func2(10, 20);

	printf("ret = %d\n", *ret);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429195002578

7.4 指针和字符串

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str1[] = "hello";		// {'h',, 'e', 'l', 'l', 'o', '\0'}
	char m[] = "hello";

	char* str2 = "hello";		// "hello" 是一个字符串常量, 不能修改。
	char* n = "hello";

	str1[0] = 'R';
	str2[0] = 'R';

	printf("str1 = %p\n", str1);
	printf("m = %p\n", m);

	printf("str2 = %p\n", str2);
	printf("n = %p\n", n);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429195636846

7.5 字符串比较

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// str1 和 str2  == -->0  str1 > str2 --> 1, str1<str2 --> -1
int mystrcmp(char *str1, char *str2)
{
    
    
	int i = 0;

	while (str1[i] == str2[i])   // *(str1+i) == *(str2+i)
	{
    
    
		if (str1[i] == '\0')
		{
    
    
			return 0;			// 2字符串一样。
		}
		i++;
	}
	return str1[i] > str2[i] ? 1 : -1;
}

int mystrcmp2(char *str1, char *str2)
{
    
    
	while (*str1 == *str2)   // *(str1+i) == *(str2+i)
	{
    
    
		if (*str1 == '\0')
		{
    
    
			return 0;			// 2字符串一样。
		}
		str1++;
		str2++;
	}
	return *str1 > *str2 ? 1 : -1;
}

int main(void)
{
    
    
	char *str1 = "helloz";
	char *str2 = "helloz";

	//int ret = mystrcmp(str1, str2);
	int ret = mystrcmp2(str1, str2);

	if (ret == 0)
		printf("相同\n");
	else if (ret == 1)
		printf("str1 > str2\n");
	else if (ret == -1)
		printf("str1 < str2\n");
	else
		printf("异常\n");

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429195751740

7.6 字符串拷贝

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// src: 源  dst: 目标
//数组版本
void mystrcpy(char* src, char* dst)
{
    
    
	int i = 0;
	while (src[i] != 0)  // src[i] == *(src+i)
	{
    
    
		dst[i] = src[i];
		i++;
	}
	dst[i] = '\0';
}
//指针版
void mystrcpy2(char* src, char* dst)
{
    
    
	while (*src != '\0')  // src[i] == *(src+i)
	{
    
    
		*dst = *src;
		src++;
		dst++;
	}
	*dst = '\0';
}

int main(void)
{
    
    
	char* src = "helloworldfuoie11ll";

	char dst[100];

	mystrcpy2(src, dst);

	printf("dst = %s\n", dst);

	system("pause");
	return EXIT_SUCCESS;
}


image-20230429195903419

7.7 字符串中查找字符

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// hellowrld --- 'm'
char* myStrch(char* str, char ch)
{
    
    
	while (*str)
	{
    
    
		if (*str == ch)
		{
    
    
			return str;
		}
		str++;
	}
	return NULL;
}

// hellowrld --- 'o'
char* myStrch2(char* str, char ch)
{
    
    
	int i = 0;
	while (str[i])
	{
    
    
		if (str[i] == ch)
		{
    
    
			return &str[i];
		}
		i++;
	}
	return NULL;
}

int main(void)
{
    
    
	char str[] = "hello world";
	char ch = ' ';

	char* ret = NULL;

	ret = myStrch2(str, ch);

	printf("ret = %s\n", ret);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429200002939

7.8 字符串去除空格

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


void str_no_space(char* src, char* dst)
{
    
    
	int i = 0;  
	int j = 0;	 
	while (src[i] != 0)
	{
    
    
		if (src[i] != ' ')
		{
    
    
			dst[j] = src[i];
			j++;
		}
		i++;
	}
	dst[j] = '\0';
}
void str_no_space2(char* src, char* dst)
{
    
    
	while (*src != 0)
	{
    
    
		if (*src != ' ')
		{
    
    
			*dst = *src;
			dst++;
		}
		src++;
	}
	*dst = '\0';
}

int main(void)
{
    
    
	char str[] = "ni chou sha chou ni za di";
	char dst[100] = {
    
     0 };

	str_no_space2(str, dst);

	printf("dst = %s\n", dst);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429200356090

7.9 字符串中找子串

#include <stdlib.h>
#include <math.h>
#include <time.h>

//strstr函数测试
int main(void)
{
    
    
	char* ret = strstr("hellollollo", "llo");

	printf("ret = %s\n", ret);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429200541946

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


int str_times(char* str, char* substr)
{
    
    
	int count = 0;
	char* p = strstr(str, substr);  // "llollollo"

	while (p != NULL)
	{
    
    
		count++;
		p += strlen(substr);	// p = p+strlen(substr) --> "llollo"
		p = strstr(p, substr);	// 返回: "llo"
	}
	return count;
}
// 统计字符串中,子串出现的次数。
int main(void)
{
    
    
	char str[] = "helloabclloxyzllo";
	char substr[] = "llo";

	int ret = str_times(str, substr);

	printf("出现%d次\n", ret);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429200746096

8. 字符串和内存

8.1 字符串操作

8.1.1 非空字符串元素个数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int no_space_str(char* str)
{
    
    
	int count = 0;

	char* p = str;

	while (*p)
	{
    
    
		if (*p != ' ')
		{
    
    
			count++;
		}
		p++;
	}
	return count;
}

int main(void)
{
    
    
	char str[] = "ni chou sha";

	int ret = no_space_str(str);

	printf("%d\n", ret);

	system("pause");
	return EXIT_SUCCESS;
}


image-20230429202216711

8.1.2 字符串逆序

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


// o e l l h
// 
// 字符串逆序
void str_inserse(char* str)
{
    
    
	//int i, j;  // str[i] *(str+i)
	char* start = str;					// 记录首元素地址
	char* end = str + strlen(str) - 1;	// 记录最后一个元素地址。

	while (start < end)			// 首元素地址是否 < 最后一个元素地址
	{
    
    
		char tmp = *start;		// 三杯水 char 元素交换
		*start = *end;
		*end = tmp;
		start++;			// 首元素对应指针后移
		end--;				// 尾元素对应指针前移
	}
}
// 判断回文字符串   abcddpba
int str_abcbb(char* str)
{
    
    
	char* start = str;		// 记录首元素地址
	char* end = str + strlen(str) - 1;// 记录最后一个元素地址。

	while (start < end)		// 首元素地址是否 < 最后一个元素地址
	{
    
    
		if (*start != *end)	// 判断字符是否一致。
		{
    
    
			return 0; // 0 表示非 回文
		}
		start++;
		end--;
	}
	return 1;		// 1 表示 回文
}

int main(void)
{
    
    
	char str[] = "this is a test";

	str_inserse(str);

	printf("str=%s\n ---------------------\n", str);

	char s2[] = "abcmncba";

	int ret = str_abcbb(s2);

	if (ret == 0)
		printf("不是回文\n");
	else if (ret == 1)
		printf("是回文\n");

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429204912005

8.1.3 字符串拷贝

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// strcpy
int main(void)
{
    
    
	char src[] = "abc efg  zhansan wangwu ";
	char dest[10] = {
    
     0 };
	char* p = strcpy(dest, src); 
	printf("p= %s\n", p);
	printf("dest = %s\n", dest);
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429205027689

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// strcpy
// strncpy
int main(void)
{
    
    
	char src[] = "hello world";
	char dest[100] = {
    
     0 };

	char* p = strncpy(dest, src, 100); 
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%c\n", p[i]);
	}

	printf("p= %s\n", p);
	printf("dest = %s\n", dest);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429205330710

8.1.4 字符串拼接strcat和strncat

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char src[] = "world";
	char dest[] = "hello";

	char* p = strcat(dest, src);

	printf("p = %s\n", p);
	printf("dest = %s\n", dest);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429205842298

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char src[] = "world";
	char dest[6] = "hello";

	char* p = strncat(dest, src, 3);

	printf("p = %s\n", p);
	printf("dest = %s\n", dest);

	printf("%d\n", strlen(dest));

	system("pause");
	return EXIT_SUCCESS;
}


image-20230429210030113

8.1.5 strcmp和strcmpz

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// strncpy
int main(void)
{
    
    
	char* str1 = "helloworld";
	char* str2 = "helloz";

	printf("ret = %d\n", strcmp(str1, str2));

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429210639819

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// strncpy
int main(void)
{
    
    
	char* str1 = "helloworld";
	char* str2 = "helloz";

	printf("ret = %d\n", strncmp(str1, str2, 8));

	system("pause");
	return EXIT_SUCCESS;
}


image-20230429210711859

8.1.6 格式化度读入和写出sprintf,sscanf

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
// sprintf
int main(void)
{
    
    
	char buf[100] = {
    
     0 }; //buffer  string str  source src

	sprintf(buf, "%d%c%d=%d\n", 10, '+', 34, 10 + 34);

	puts(buf);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429210833339

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// sscanf
int main(void)
{
    
    
	char buf[100] = {
    
     0 }; //buffer  string str  source src

	int a, b, c;

	char str[] = "13+56=89";

	sscanf(str, "%d+%d=%d", &a, &b, &c);

	printf("a = %d\n", a);
	printf("b = %d\n", b);
	printf("c = %d\n", c);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429210915578

8.1.7 字符串分割strtok

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str[] = "www.itcast.cn.com.net";  // www baidu com

	char* p = strtok(str, ".");  // 第一次拆分,参1 传 待拆分的原串。

	while (p != NULL)
	{
    
    
		p = strtok(NULL, ".");  // 第1+ 次拆分是,参1传 NULL.

		printf("%s\n", p);
	}
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429211032274

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char str[] = "www.baidu.com$This is a strtok$test";

	char* p = strtok(str, "$ .");
	//strtok函数的第一个参数不为NULL,函数将找到str中第一个被分割的字符串,同时strtok还会记住该分割符的位置
	// strtok函数的第一个参数为NULL,函数将在之前strtok记住的分隔符的位置开始,查找下一个标记
	while (p != NULL)
	{
    
    
		p = strtok(NULL, ". $");
		printf("p = %s\n", p);
	}

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429211111692

8.1.8 atol_atof_atol

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

static int a = 1034673;

void test1(void)
{
    
    
	static int b = 0;

	printf("b = %d\n", b++);
}
int main(void)
{
    
    
	char str[] = "abc345";
	int num = atoi(str);
	printf("num = %d\n", num);
	char str1[] = "     -10";
	int num1 = atoi(str1);
	printf("num1 = %d\n", num1);
	char str2[] = "0.123f";
	double num2 = atof(str2);
	printf("num2 = %.2lf\n", num2);
	char str3[] = "123L";
	long num3 = atol(str3);
	printf("num3 = %ld\n", num3);
	system("pause");
	return EXIT_SUCCESS;
}

image-20230429212050512

8.2 局部变量

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void test1(void);  // 全局函数声明

int m = 4456;

int main(void)
{
    
    
	int i = 10903;

	for (size_t j = 0; j < 10; j++)
	{
    
    
		printf("j = %d\n", j);
		//test1();
	}
	printf("i 2 = %d\n", i);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429212208378

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(void)
{
    
    
	//int arr[1000000] = {10, 20, 40};
	int *p = (int *)malloc(sizeof(int) * 10);
	//char *str = (char *)malloc(sizeof(char)*10);
	if (p == NULL)
	{
    
    
		printf("malloc error\n");
		return -1;
	}
	char *tmp = p;  // 记录malloc返回的地址值。用于free

	// 写数据到 malloc 空间。
	for (size_t i = 0; i < 10; i++)
	{
    
    
		p[i] = i + 10;
	}
	// 读出malloc空间中的数据
	//for (size_t i = 0; i < 10; i++)
	//{
    
    
	//	printf("%d ", *(p+i));
	//}
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%d ", *p);
		p++;
	}

	// 释放申请的内存。
	free(tmp);
	p = NULL;

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429212325685

8.3 申请堆空间

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	//int arr[1000000] = {10, 20, 40};
	int* p = (int*)malloc(sizeof(int) * 10);
	//char *str = (char *)malloc(sizeof(char)*10);
	if (p == NULL)
	{
    
    
		printf("malloc error\n");
		return -1;
	}
	char* tmp = p;  // 记录malloc返回的地址值。用于free

	// 写数据到 malloc 空间。
	for (size_t i = 0; i < 10; i++)
	{
    
    
		p[i] = i + 10;
	}
	// 读出malloc空间中的数据
	//for (size_t i = 0; i < 10; i++)
	//{
    
    
	//	printf("%d ", *(p+i));
	//}
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%d ", *p);
		p++;
	}

	// 释放申请的内存。
	free(tmp);
	p = NULL;

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429212438329

8.4 二级指针malloc空间

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	int** p = malloc(sizeof(int*) * 3);	// int **p ==> int *p[10]; ==> [ int *, int *, int * ]

	for (size_t i = 0; i < 3; i++)
	{
    
    
		p[i] = malloc(sizeof(int) * 5);
	}

	// 使用空间 -- 写
	for (size_t i = 0; i < 3; i++)
	{
    
    
		for (size_t j = 0; j < 5; j++)
		{
    
    
			p[i][j] = i + j;
		}
	}

	// 使用空间 -- 读
	for (size_t i = 0; i < 3; i++)
	{
    
    
		for (size_t j = 0; j < 5; j++)
		{
    
    
			printf("%d ", *(*(p + i) + j));  // p[i][j] == *(p+i)[j] == *(*(p+i)+j)
		}
		printf("\n");
	}

	// 释放空间时,应先释放内层空间。
	for (size_t i = 0; i < 3; i++)
	{
    
    
		free(p[i]); //*(p+i)
		p[i] = NULL;
	}
	// 释放外层空间
	free(p);
	p = NULL;

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429212624159

9. 文件

9.1 联合体和枚举

9.1.1 联合体

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
typedef union test {
    
    
	char ch;
	short sh;
	int a;
}test_t;
int main(void)
{
    
    
	test_t obj;

	obj.a = 0x87654321;

	printf("&obj    = %p\n", &obj);
	printf("&obj.ch = %p\n", &obj.ch);
	printf("&obj.sh = %p\n", &obj.sh);
	printf("&obj.a  = %p\n", &obj.a);

	printf("sizeof(test_t) = %u\n", sizeof(test_t));

	printf("a  = 0x%x\n", obj.a);
	printf("sh = 0x%x\n", obj.sh);
	printf("ch = 0x%x\n", obj.ch);

	obj.ch = 0xFF;

	printf("a  = 0x%x\n", obj.a);
	printf("sh = 0x%x\n", obj.sh);
	printf("ch = 0x%x\n", obj.ch);

	system("pause");
	return EXIT_SUCCESS;
}

enum  color {
    
     red, green = -5, blue, black, pink = 18, yellow };

image-20230429212825987

9.1.2 枚举

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
typedef union test {
    
    
	char ch;
	short sh;
	int a;
}test_t;
enum  color {
    
     red, green = -5, blue, black, pink = 18, yellow };
int main(void)
{
    
    
	int flg = 2;

	// ......

	if (flg == blue)
	{
    
    
		printf("blue is 2\n");
	}
	else
	{
    
    
		printf("blue is not 2, blue = %d\n", blue);
	}
	printf("yellow = %d\n", yellow);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429212917992

9.2 系统文件

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	fclose(stdout);
	printf("hello file\n");
	system("pause");
	return EXIT_SUCCESS;
}

9.3 文件的打开和关闭

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	FILE* fp = NULL;

	fp = fopen("test2.txt", "w");
	if (fp == NULL)
	{
    
    
		perror("fopen error");  //printf("fopen error\n");  :xxxxxxx
		getchar();
		return -1;
	}

	fclose(fp);
	printf("------------finish\n");

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429213415291

9.4 fput和fget

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char *filename = "test04.txt";

	FILE *fp = fopen(filename, "w");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return -1;
	}

	int ret = fputc('A', fp);

	printf("ret = %d\n", ret);

	fclose(fp);
	printf("---------------finish\n");

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429213645226

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	char* buf = "abcdefghijklmnopqrstuvwxyz";

	char* filename = "test04.txt";
	int ret = 0;

	FILE* fp = fopen(filename, "w");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return -1;
	}
	int n = strlen(buf);
	for (size_t i = 0; i < n; i++)
	{
    
    
		ret = fputc(buf[i], fp);
		if (ret == -1)
		{
    
    
			perror("fputc eror");
			return -1;
		}
	}
	fclose(fp);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429213711109

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void write_file()
{
    
    
	FILE* fp = fopen("05test.txt", "w");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return;
	}

	fputc('a', fp);
	fputc('b', fp);
	fputc('c', fp);
	fputc('d', fp);

	fclose(fp);
}

void read_file()
{
    
    
	char ch = 0;

	FILE* fp = fopen("05test.txt", "r");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return;
	}

	while (1)
	{
    
    
		ch = fgetc(fp);// 读取一个字符
		if (ch == EOF)
		{
    
    
			break;
		}
		printf("%d\n", ch);
	}

	fclose(fp);
}

int main(void)
{
    
    
	//write_file();
	read_file();

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429214034539

9.5 feof

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void read_file06()
{
    
    
	char ch = 0;

	FILE* fp = fopen("06test.txt", "r");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return;
	}

	while (1)
	{
    
    
		ch = fgetc(fp);

		if (feof(fp))
		{
    
    
			break;
		}
		printf("%d\n", ch);
	}

	fclose(fp);
}

void test_feof()
{
    
    
	FILE* fp = fopen("06test.txt", "r");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return;
	}
	while (1)
	{
    
    
		printf("没有到达文件结尾\n");
		fgetc(fp);				// 一次读一个字符,读到字符直接丢弃。
		if (feof(fp))
		{
    
    
			break;
		}
	}
	fclose(fp);
}
void write_file06()
{
    
    
	FILE* fp = fopen("06test.txt", "w");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return;
	}
	fputc('a', fp);
	fputc('b', fp);
	fputc(-1, fp);
	fputc('c', fp);
	fputc('d', fp);
	fputc('\n', fp);

	fclose(fp);
}

int main(void)
{
    
    
	write_file06();
	read_file06();
	test_feof();

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429214142893

9.6 fgets和fputs

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	FILE* fp = fopen("test07.txt", "w");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return -1;
	}
	char buf[4096] = {
    
     0 };

	while (1)
	{
    
    
		fgets(buf, 4096, stdin);
		if (strcmp(buf, ":wq\n") == 0)
		{
    
    
			break;
		}
		fputs(buf, fp);
	}

	fclose(fp);

	system("pause");
	return EXIT_SUCCESS;
}

image-20230429214321536

9.7 四则运算(文件版)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void write_file08()
{
    
    
	FILE* fp = fopen("test08.txt", "w");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return;
	}

	fputs("10/2=\n", fp);
	fputs("10*3=\n", fp);
	fputs("4-2=\n", fp);
	fputs("10+2=\n", fp);

	fclose(fp);
}

int calc(char ch, int a, int b)
{
    
    
	switch (ch)
	{
    
    
	case '/':
		return a / b;

	case '+':
		return a + b;

	case '-':
		return a - b;

	case '*':
		return a * b;
	default:
		break;
	}
}

void read_file08()
{
    
    
	char buf[4096] = {
    
     0 };
	char result[4096] = {
    
     0 };

	char sum_res[4096] = {
    
     0 };

	int a, b, ret;
	char ch;

	FILE* fp = fopen("test08.txt", "r");
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return;
	}

	while (1)
	{
    
    
		fgets(buf, 4096, fp);  //buf = "10/2=\n\0";
		if (feof(fp))
		{
    
    
			break;
		}
		sscanf(buf, "%d%c%d=\n", &a, &ch, &b);	// a:10, ch:'/' b: 2

		sprintf(result, "%d%c%d=%d\n", a, ch, b, calc(ch, a, b));  // 10 / 2 = 5;

		strcat(sum_res, result);
	}
	fclose(fp);  // 将 只有表达式没有结果的文件关闭。

	fp = fopen("test08.txt", "w");	// 清空 只有表达式没有结果的文件
	if (fp == NULL)
	{
    
    
		perror("fopen error");
		return;
	}
	fputs(sum_res, fp);	// 将 既有表达式又有结果的字符串写到文件中。
	fclose(fp);
}
int main(void)
{
    
    
	write_file08();
	getchar();
	read_file08();
	system("pause");
	return EXIT_SUCCESS;
}

9.8 sprintf和sscanf

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void write_file()
{
    
    
	FILE* fp = fopen("abc.c", "w");
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}

	fprintf(fp, "%d\n", 10);
	fprintf(fp, "%d\n", 8);
	fprintf(fp, "%d\n", 6);

	fclose(fp);
}

void read_file()
{
    
    
	int a;

	FILE* fp = fopen("abc.c", "r");
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}

	fscanf(fp, "%d\n", &a);
	printf("%d\n", a);

	fscanf(fp, "%d\n", &a);
	printf("%d\n", a);

	fscanf(fp, "%d\n", &a);
	printf("%d\n", a);

	a = 0;
	fscanf(fp, "%d\n", &a);
	printf("%d\n", a);

	fclose(fp);
}

// fscanf 循环读文件
void read_file2()
{
    
    
	int a;

	FILE* fp = fopen("abc.c", "r");
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}
	while (1)
	{
    
    
		fscanf(fp, "%d\n", &a);  // 读
		printf("%d\n", a);
		if (feof(fp))		// 真-- 文件结尾
			break;
	}

	fclose(fp);
}

// fgets 循环读文件
void read_file3()
{
    
    
	char buf[1024];
	FILE* fp = fopen("abc.c", "r");
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}
	while (1)
	{
    
    
		memset(buf, 0, 1024);
		fgets(buf, 1024, fp);// 读 \n
		if (feof(fp))		// 真-- 文件结尾
			break;
		printf("%d\n", buf[0]);
	}

	fclose(fp);
}

int main(void)
{
    
    
	FILE* fp = fopen("test0101.txt", "r");
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}
	int a;
	char ch;
	char str[10];

	int ret = fscanf(fp, "%d %c %s", &a, &ch, str);
	printf("ret = %d\n", ret);

	fclose(fp);

	system("pause");
	return EXIT_SUCCESS;
}

9.9 文件版随机排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void write_rand()
{
    
    
	FILE *fp = fopen("test02.txt", "w");
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}

	srand(time(NULL)); // 随机数种子

	for (size_t i = 0; i < 10; i++)
	{
    
    
		fprintf(fp, "%d\n", rand() % 100);  // 将生成的随机数写入文件
	}
	
	fclose(fp);
}


void BubbleSort(int * src, int len)
{
    
    
	for (int i = 0; i < len - 1; i++)
	{
    
    
		for (int j = 0; j < len - 1 - i; j++)
		{
    
    
			if (src[j] > src[j + 1])
			{
    
    
				int temp = src[j];
				src[j] = src[j + 1];
				src[j + 1] = temp;
			}
		}
	}
}

void read_rand02()
{
    
    
	int arr[10], i = 0;

	FILE *fp = fopen("test02.txt", "r");
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}

	while (1)
	{
    
    
		fscanf(fp, "%d\n", &arr[i]);// 从文件中读取一个随机数,存入数组arr
		i++;
		if (feof(fp))				// 先存储,后判断,防止最后一个元素丢失
			break;
	}
	BubbleSort(arr, sizeof(arr)/sizeof(arr[0]));  // 对读取到的乱序数组排序

	fclose(fp);							// 关闭文件
	fp = fopen("test02.txt", "w");		// 重新w方式打开文件, 清空原未排序文件。
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}
	for (size_t i = 0; i < 10; i++)
		fprintf(fp, "%d\n", arr[i]);	// 写排好序的数组到文件

	fclose(fp);
}

int main(void)
{
    
    
	write_rand();

	getchar();

	read_rand02();

	system("pause");
	return EXIT_SUCCESS;
}

9.10 结构化读取fwrite和fread

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

typedef struct student {
    
    
	int age;
	char name[10];
	int num;
} stu_t;

void write_struct()
{
    
    
	stu_t stu[4] = {
    
    
		18, "afei", 10,
		20, "andy", 20,
		30, "lily", 30,
		16, "james", 40
	};

	FILE *fp = fopen("test03.txt", "w");
	if (!fp)
	{
    
    
		perror("fopen error");
		return -1;
	}

	int ret = fwrite(&stu[0], 1, sizeof(stu_t) * 4, fp);
	if (ret == 0)
	{
    
    
		perror("fwrite error");
		return -1;
	}

	printf("ret = %d\n", ret);

	fclose(fp);
}

// 一次读一个元素。
void read_struct()
{
    
    
	FILE *fp = fopen("test03.txt", "r");
	if (!fp)
	{
    
    
		perror("fopen error");
		return -1;
	}
	stu_t s1;

	int ret = fread(&s1, 1, sizeof(stu_t), fp);
	printf("ret = %d\n", ret);

	printf("age = %d, name=%s, num = %d\n", s1.age, s1.name, s1.num);

	fclose(fp);
}

// 读所有元素
void read_struct2()
{
    
    
	FILE *fp = fopen("test03.txt", "r");
	if (!fp)
	{
    
    
		perror("fopen error");
		return -1;
	}
	stu_t s1[10];  // stu_t *s1 = malloc(sizeof(stu_t) * 1024);
	int i = 0;
	while (1)
	{
    
    
		int ret = fread(&s1[i], 1, sizeof(stu_t), fp);
		//if (ret == 0)		// 替代feof()函数来判断读到文件结尾。
		if (feof(fp))
		{
    
    
			break;
		}
		i++;
		printf("age = %d, name=%s, num = %d\n", s1[i].age, s1[i].name, s1[i].num);
	}
	fclose(fp);
}


int main(void)
{
    
    
	write_struct();
	read_struct2();

	system("pause");
	return EXIT_SUCCESS;
}

9.11 大文件拷贝

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void myfile_cp()
{
    
    
	FILE *rfp = fopen("demo.avi", "rb");
	FILE *wfp = fopen("mycopy.avi", "wb");

	char buf[4096] = {
    
    0};  

	int ret = 0;

	while (1)
	{
    
    
		memset(buf, 0, sizeof(buf));
		ret = fread(buf, 1, sizeof(buf), rfp);
		if (ret == 0)
		{
    
    
			break;
		}
		printf("ret = %d\n", ret);
		fwrite(buf, 1, ret, wfp);
	}

	fclose(wfp);
	fclose(rfp);
}

int main(void)
{
    
    
	myfile_cp();

	printf("---------------------finish\n");

	system("pause");
	return EXIT_SUCCESS;
}


9.12 随机读取文件

9.12.1 fseek

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

typedef struct student {
    
    
	int age;
	char name[10];
	int num;
} stu_t;

int main(void)
{
    
    
	stu_t stu[4] = {
    
    
		18, "afei", 10,
		20, "andy", 20,
		30, "lily", 30,
		16, "james", 40
	};
	stu_t s1;

	FILE* fp = fopen("test05.txt", "wb+");
	if (!fp)  // fp == NULL
	{
    
    
		perror("fopen error");
		return -1;
	}
	int ret = fwrite(&stu[0], 1, sizeof(stu), fp);  // 以二进制形式写入,
	printf("ret = %d\n", ret);

	fseek(fp, sizeof(stu_t) * 2, SEEK_SET);		// 从文件起始位置,向后偏移一个stu结构体

	ret = fread(&s1, 1, sizeof(s1), fp);
	printf("ret = %d\n", ret);

	printf("1 age= %d, name=%s, num=%d\n", s1.age, s1.name, s1.num);

	int len = ftell(fp); // 获取文件当前读写指针位置,到文件起始位置的偏移量。

	printf("len = %d\n", len);

	rewind(fp);	// 将文件读写指针回卷到起始。

	ret = fread(&s1, 1, sizeof(s1), fp);

	printf("2 age= %d, name=%s, num=%d\n", s1.age, s1.name, s1.num);

	// 获取文件大小。
	fseek(fp, 0, SEEK_END);	// 将文件读写指针放到文件结尾。
	len = ftell(fp);
	printf("文件大小为:%d\n", len);

	fclose(fp);

	system("pause");
	return EXIT_SUCCESS;
}

9.12.2 改变指针位置

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    
    
	FILE *fp = fopen("test0501.txt", "w+");  // "r+"

	int ret = fputs("11111", fp);
	printf("ret 1 = %d\n", ret);		// 0 表示成功。

	ret = fputs("22222", fp);
	printf("ret 2 = %d\n", ret);

	ret = fputs("33333", fp);
	printf("ret 3 = %d\n", ret);

	char buf[1024] = {
    
     0 };

	//fseek(fp, 5 * 2, SEEK_SET);  // 改变读写指针位置。
	rewind(fp); // 起始位置。
	char *ptr = fgets(buf, 1024, fp);
	if (ptr == NULL)
		printf("ptr == NULL \n");

	printf("fgets ptr = %s\n", ptr);
	printf("buf = %s\n", buf);

	fclose(fp);
	system("pause");
	return EXIT_SUCCESS;
}

9.12.3 读取指针位置

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    
	FILE *fp = fopen("test1.txt", "r+");

	char buf[6] = {
    
     0 };
	char *ptr = fgets(buf, 6, fp);

	printf("buf=%s, ptr=%s\n", ptr, buf);

	fseek(fp, 0, SEEK_CUR);
	int ret = fputs("AAAAA", fp);
	printf("ret = %d\n", ret);

	fclose(fp);

	system("pause");
	return 0;
}

9.13 获取文件属性

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#include <sys/types.h>
#include <sys/stat.h>

/*
FILE *fp = fopen("test05.txt", "r");

fseek(fp, 0, SEEK_END);

int len = ftell(fp);

printf("文件大小:%d\n", len);

fclose(fp);
*/

int main(void)
{
    
    
	struct stat buf;

	int ret = stat("test05.txt", &buf);  // 传出参数:在函数调用结束时,充当函数返回值。

	printf("文件大小:%d\n", buf.st_size); // 不打开文件,获取文件大小。

	system("pause");
	return EXIT_SUCCESS;
}

9.14 缓存区刷新

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main0701(void)
{
    
    
	FILE *fp = fopen("test07.txt", "w+");
	if (!fp)
	{
    
    
		perror("fopen error");
		return -1;
	}
	char m = 0;

	while (1) 
	{
    
    
		scanf("%c", &m);
		if (m == ':')
		{
    
    
			break;
		}
		fputc(m, fp);
		fflush(fp);  // 手动刷新文件缓冲到物理磁盘。
	}
	// 当文件关闭时,会自动刷新缓冲区。
	fclose(fp);

	system("pause");
	return EXIT_SUCCESS;
}

猜你喜欢

转载自blog.csdn.net/weixin_42917352/article/details/130448459