C语言
1.helloworld
1.1 pause
#include
int main(void)
{
printf("hello world!\n");
system("pause");
return 0;
}
1.2 cls清屏
#include
#include
int main(void)
{
printf("-----------hello world!\n");
Sleep(2000);
system("cls");
return 0;
}
1.3 加法运算
#include
int main(void)
{
int a = 3;
int b = 5;
int c;
c = a + b;
printf("hello world\n");
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;
}
1.4 hello
#include
#define PI 3.14
int main(void)
{
printf("helloworld\n");
printf("%d\n, PI");
return 0;
}
2 常量变量和数据类型
2.1 常量
#include
#define PI 3.1415
int main(void)
{
const int r = 3;
float s = PI * r * r;
float l = 2 * PI * r;
printf("圆的周长为:%.2f\n", l);
printf("圆的面积为:%.2f\n", s);
return 0;
}
2.2 变量
#include
int main(void)
{
int a;
a = 56;
return 0;
}
2.3 sizeof数据类型大小
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#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;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#define N 1024
int main(void)
{
int a = 3;
short b = 4;
long c = 5;
long long d = 6;
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;
unsigned short bun = 4;
unsigned long cun = 5;
unsigned long long dun = 6;
printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
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));
}
2.4 无符号整型
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
size_t var = 10;
printf("var = %u\n", var);
unsigned int a = 10u;
unsigned short b = 20u;
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;
}
2.5 字符类型
2.5.1 字符类型简介
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char ch = 'A';
printf("1 ch = %d\n", ch);
ch = 'm';
printf("2 ch = %d\n", ch);
ch = 'a';
printf("3 ch = %d\n", ch);
system("pause");
return EXIT_SUCCESS;
}
2.5.2 字符类型运算
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
2.6 实数型
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
2.7 进制和转换
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 0x2C;
printf("10进制显示 a = %d\n", a);
printf("8进制显示 a = %o\n", a);
printf("16进制显示 a = %x\n", a);
system("pause");
return EXIT_SUCCESS;
}
2.8 数据溢出
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char ch = 127+1;
printf("ch = %d\n", ch);
system("pause");
return EXIT_SUCCESS;
}
3. 运算符和分支循环语句
3.1 字符串输出
3.1.1 字符数组输出
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
3.1.2 字符数组格式化输出(%-15s)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[] = "hello world";
printf("str = |%-15s|\n", str);
system("pause");
return EXIT_SUCCESS;
}
3.1.2 字符输出 (putchar)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
putchar(97);
putchar('b');
putchar('c');
putchar('d');
putchar('abcZ');
system("pause");
return EXIT_SUCCESS;
}
3.2 格式化输入
3.2.1 获取单个输入
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a;
scanf("%d", &a);
printf("a = %d\n", a);
system("pause");
return EXIT_SUCCESS;
}
3.2.2 获取多个输入
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char ch1, ch2, ch3;
scanf("%c%c%c", &ch1, &ch2, &ch3);
printf("ch1 = %c\n", ch1);
printf("ch2 = %c\n", ch2);
printf("ch3 = %c\n", ch3);
system("pause");
return EXIT_SUCCESS;
}
3.2.3 连续相同输入
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a1, a2, a3;
scanf("%d %d %d", &a1, &a2, &a3);
printf("a1 = %d\n", a1);
printf("a2 = %d\n", a2);
printf("a3 = %d\n", a3);
system("pause");
return EXIT_SUCCESS;
}
3.2.4 字符数组输入
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char a[5];
scanf("%s", a);
printf("a = %s\n", a);
system("pause");
return EXIT_SUCCESS;
}
3.2.5 getchar获取字符输入
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char ch;
ch = getchar();
printf("ch = %c\n", ch);
putchar(ch);
putchar('\n');
system("pause");
return EXIT_SUCCESS;
}
3.3 算数运算符
3.3.1 数学运算
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 10;
int b = 20;
int c = a * b;
int d = 34 / 10;
printf("d = %d\n", d);
system("pause");
return EXIT_SUCCESS;
}
3.3.2 自增运算(a++与++a)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 10;
int b = 50;
printf("a = %d\n", a++);
printf("----a = %d", a);
printf("b = %d\n", ++b);
printf("----b = %d\n", b);
system("pause");
return EXIT_SUCCESS;
}
3.4 逻辑运算
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
3.5 三目运算
3.5.1 三目运算使用
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
//
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;
}
3.5.2 三目运算案例2
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
3.6 隐式转换和强制类型转换
3.6.1 隐式转换
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 321;
char ch = a;
printf("ch = %d\n", ch);
system("pause");
return EXIT_SUCCESS;
}
3.6.2 强制类型转换
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(int var)
{
float price = 3.6;
int weight = 4;
double sum = (int)(price * weight);
printf("价格:%lf\n", sum);
system("pause");
return EXIT_SUCCESS;
}
3.7 if 语句
3.7.1 if …else… 语句
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
3.7.2 else if语句
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int score;
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;
}
3.7.3 if else if else 语句
int main0703(void)
{
int pig1, pig2, pig3;
printf("请输入三只小猪的体重:");
scanf("%d %d %d", &pig1, &pig2, &pig3);
if (pig1 > pig2)
{
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;
}
3.8 switch
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int score;
scanf("%d", &score);
switch (score / 10)
{
case 10:
case 9:
printf("优秀\n");
break;
case 8:
case 7:
printf("良好\n");
case 6:
printf("及格\n");
default:
printf("不及格\n");
break;
}
system("pause");
return EXIT_SUCCESS;
}
3.9 while 循环
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int num = 1;
while (num <= 100)
{
if ((num % 7 == 0) || (num % 10 == 7) || (num / 10 == 7))
{
printf("敲桌子\n");
}
else
{
printf("%d\n", num);
}
num++;
}
system("pause");
return EXIT_SUCCESS;
}
3.10 do while
3.10.1 do…whie语句
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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
#include
#include
#include
#include
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;
}
4. for循环和数组排序
4.1 for 循环
4.1.1 基础的for循环
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
int main(void)
{
srand(time(NULL));
int n = 0;
int num = rand() % 100;
for (;;)
{
printf("请输入猜测的数字:");
scanf("%d", &n);
if (n < num)
{
printf("猜小了\n");
}
else if (n > num)
printf("猜大了\n");
else
{
printf("猜中!!!\n");
break;
}
}
printf("本尊是:%d\n", num);
system("pause");
return 0;
}
4.1.2 省略的表达式1
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int i = 1;
int sum = 0;
for (; i <= 100; i++)
{
sum = sum + i;
}
printf("sum = %d\n", sum);
system("pause");
return EXIT_SUCCESS;
}
4.1.3 省略表达式2
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int i = 1;
int sum = 0;
for (; i <= 100; )
{
sum = sum + i;
i++;
}
printf("sum = %d\n", sum);
system("pause");
return EXIT_SUCCESS;
}
4.1.4 省略表达式123
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int i = 0;
for (;;)
{
printf("i = %d\n", i);
i++;
}
system("pause");
return EXIT_SUCCESS;
}
4.1.5 多个表达式
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
4.2 猜数字游戏
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
int main(void)
{
srand(time(NULL));
int n = 0;
int num = rand() % 100;
for (;;)
{
printf("请输入猜测的数字:");
scanf("%d", &n);
if (n < num)
{
printf("猜小了\n");
}
else if (n > num)
printf("猜大了\n");
else
{
printf("猜中!!!\n");
break;
}
}
printf("本尊是:%d\n", num);
system("pause");
return 0;
}
4.3 模拟电子表
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
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;
}
4.4 9*9乘法表
4.4.1 正序9*9乘法表
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
4.4.2 倒序9*9乘法表
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
4.5 continue
4.5.1 for 循环中continue
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
4.5.2 while循环中continue
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int num = 5;
while (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;
}
4.6 goto语句
4.6.1 goto语句简介
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
4.6.2 循环语句中goto
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
4.7 数组
4.7.1 数组简介
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 5, b = 29, c = 10;
int arr[10] = { 1, 2 ,4, 6, 76, 8, 90 ,4, 3, 6 };
printf("&arr[0] = %p\n", &arr[0]);
printf("arr = %p\n", arr);
system("pause");
return EXIT_SUCCESS;
}
4.7.2 数组大小与个数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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 };
printf("数组大小:%u\n", sizeof(arr));
printf("数组元素的大小:%u\n", sizeof(arr[0]));
printf("数组元素个数:%d\n", sizeof(arr) / sizeof(arr[0]));
system("pause");
return EXIT_SUCCESS;
}
4.7.3 数组初始化
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int arr[10];
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;
}
4.8 数组逆序
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int arr[] = { 1, 6, 8, 0, 4, 3, 9, 2 };
int len = sizeof(arr) / sizeof(arr[0]);
int i = 0;
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;
}
4.9 冒泡排序
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
5. 二维数组、字符串、函数
5.1 二维数组
5.1.1 二维数组定义
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
5.1.2 二维数组练习
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
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]);
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;
}
5.1.3 多维数组
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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 }
}
};
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;
}
5.2 字符数组和字符串
5.2.1 字符数组打印
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[6] = { 'h', 'e', 'l', 'l', 'o', '\0' };
char str2[] = "world";
printf("%s\n", str2);
system("pause");
return EXIT_SUCCESS;
}
5.2.2 统计字符串中每个字符出现的次数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[11] = { 0 };
for (size_t i = 0; i < 10; i++)
{
scanf("%c", &str[i]);
}
int count[26] = { 0 };
for (size_t i = 0; i < 11; i++)
{
int index = str[i] - 'a';
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;
}
5.2.3 scanf获取字符串
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[100];
scanf("%[^\n]s", str);
printf("%s\n", str);
system("pause");
return EXIT_SUCCESS;
}
5.3 字符串操作函数
5.3.1 gets 函数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[10];
printf("请输入字符串:");
gets(str);
printf("%s\n", str);
system("pause");
return EXIT_SUCCESS;
}
5.3.2 fgets 函数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[10];
printf("获取的字符串为:%s", fgets(str, sizeof(str), stdin));
system("pause");
return EXIT_SUCCESS;
}
5.3.3 puts 函数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[] = "hello world\n";
int ret = puts(str);
printf("ret = %d\n", ret);
system("pause");
return EXIT_SUCCESS;
}
5.3.4 fputs函数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[] = "hello world\n";
int ret = fputs("hello world\n", stdout);
printf("ret = %d\n", ret);
system("pause");
return EXIT_SUCCESS;
}
5.3.5 strlen函数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
5.3.6 实现strlen函数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[] = "hello world";
int i = 0;
while (str[i] != '\0')
{
i++;
}
printf("%d\n", i);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[] = "hello\0world";
int i = 0;
while (str[i++]);
printf("%d\n", i - 1);
system("pause");
return EXIT_SUCCESS;
}
5.4 字符串拼接
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str1[] = "hello";
char str2[] = "world";
char str3[100];
int i = 0;
while (str1[i] != '\0')
{
str3[i] = str1[i];
i++;
}
int j = 0;
while (str2[j])
{
str3[i + j] = str2[j];
j++;
}
str3[i + j] = '\0';
printf("str3 = %s\n", str3);
system("pause");
return EXIT_SUCCESS;
}
5.5 函数
5.5.1 函数声明定义
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
void bubble_sort(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;
}
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;
}
5.5.2 exit函数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int func(int a, char ch);
int main(void)
{
int ret = func(10, 'a');
printf("ret = %d\n", ret);
system("pause");
exit(EXIT_SUCCESS);
}
int func(int a, char ch)
{
printf("a = %d\n", a);
printf("ch = %c\n", ch);
exit(10);
}
6. 指针
6.1 指针基础
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 10;
int* p = &a;
a = 350;
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;
}
6.2 野指针和空指针
6.2.1 野指针
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int *p;
*p = 2000;
printf("*p = %d\n", *p);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int m;
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
#include
#include
#include
#include
int main(void)
{
int *p = NULL;
if (p != NULL)
{
*p = 300;
printf("*p = %d\n", *p);
}
system("pause");
return EXIT_SUCCESS;
}
6.3 泛型指针(void **)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 345;
char ch = 'R';
void* p;
p = &ch;
printf("%c\n", *(char*)p);
system("pause");
return EXIT_SUCCESS;
}
6.4 const作用
6.4.1 修饰变量
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
const int a = 20;
int* p = &a;
*p = 650;
printf("%d\n", a);
system("pause");
return EXIT_SUCCESS;
}
6.4.2 const int *p
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 10;
int b = 30;
const int* p = &a;
p = &b;
printf("*p=%d", *p);
system("pause");
return EXIT_SUCCESS;
}
6.4.3 int const *p
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 10;
int b = 30;
int const* p = &a;
p = &b;
printf("*p=%d", *p);
system("pause");
return EXIT_SUCCESS;
}
6.4.4 int * const p
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 10;
int b = 30;
int* const p = &a;
*p = 300;
printf("a = %d\n", *p);
printf("a = %d\n", a);
system("pause");
return EXIT_SUCCESS;
}
6.4.5 const int * const p
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 10;
int b = 30;
const int* const p = &a;
printf("a = %d\n", *p);
printf("a = %d\n", a);
system("pause");
return EXIT_SUCCESS;
}
6.5 指针和数组
6.5.1 数组操作
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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 ", *(p + i));
}
printf("\n");
system("pause");
return EXIT_SUCCESS;
}
6.5.2 指针操作
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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++;
}
putchar('\n');
printf("last p = %p\n", p);
system("pause");
return EXIT_SUCCESS;
}
6.6 指针算法运算
6.6.1 指针±操作
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a[] = { 1, 2, 4, 5, 6, 7, 8, 9, 0 };
int* p = &a[5];
printf("p-2 = %p\n", p - 2);
printf("&a[3] = %p\n", &a[3]);
system("pause");
return EXIT_SUCCESS;
}
6.6.2 &数组名 +1操作
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
6.6.3 指针加减操作
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
6.6.4 strlen实现方式(指针)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int mystrlen(char arr[]);
int main(void)
{
char abc[] = "hello world";
int ret = mystrlen2(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;
}
6.7 指针的比较运算
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
6.8 指针数组
#include
#include
#include
#include
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 };
printf("*(arr[0]) = %d\n", *(*(arr + 0)));
printf("*(arr[0]) = %d\n", **arr);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a[] = { 10 };
int b[] = { 20 };
int c[] = { 30 };
int* arr[] = { a, b, c };
printf("*(arr[0]) = %d\n", *(*(arr + 0)));
printf("*(arr[0]) = %d\n", **arr);
system("pause");
return EXIT_SUCCESS;
}
6.9 多级指针
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int a = 10;
int* p = &a;
int** pp = &p;
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;
}
7. 指针和字符串
7.1 传值和传址
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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);
swap2(&m, &n);
printf("--after-- m = %d, n = %d\n", m, n);
system("pause");
return EXIT_SUCCESS;
}
int swap2(int* a, int* b)
{
int tmp = 0;
tmp = *a;
*a = *b;
*b = tmp;
return 0;
}
int swap(int a, int b)
{
int tmp = 0;
tmp = a;
a = b;
b = tmp;
return 0;
}
7.2 数组做函数参数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
7.3 指针做函数返回值
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int m = 100;
int* test_func2(int a, int b)
{
int p = 1234;
return &p;
}
int main(void)
{
int* ret = NULL;
ret = test_func2(10, 20);
printf("ret = %d\n", *ret);
system("pause");
return EXIT_SUCCESS;
}
7.4 指针和字符串
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str1[] = "hello";
char m[] = "hello";
char* str2 = "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;
}
7.5 字符串比较
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int mystrcmp(char *str1, char *str2)
{
int i = 0;
while (str1[i] == str2[i])
{
if (str1[i] == '\0')
{
return 0;
}
i++;
}
return str1[i] > str2[i] ? 1 : -1;
}
int mystrcmp2(char *str1, char *str2)
{
while (*str1 == *str2)
{
if (*str1 == '\0')
{
return 0;
}
str1++;
str2++;
}
return *str1 > *str2 ? 1 : -1;
}
int main(void)
{
char *str1 = "helloz";
char *str2 = "helloz";
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;
}
7.6 字符串拷贝
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
void mystrcpy(char* src, char* dst)
{
int i = 0;
while (src[i] != 0)
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
}
void mystrcpy2(char* src, char* dst)
{
while (*src != '\0')
{
*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;
}
7.7 字符串中查找字符
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
char* myStrch(char* str, char ch)
{
while (*str)
{
if (*str == ch)
{
return str;
}
str++;
}
return NULL;
}
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;
}
7.8 字符串去除空格
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
7.9 字符串中找子串
#include
#include
#include
int main(void)
{
char* ret = strstr("hellollollo", "llo");
printf("ret = %s\n", ret);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int str_times(char* str, char* substr)
{
int count = 0;
char* p = strstr(str, substr);
while (p != NULL)
{
count++;
p += strlen(substr);
p = strstr(p, substr);
}
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;
}
8. 字符串和内存
8.1 字符串操作
8.1.1 非空字符串元素个数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
8.1.2 字符串逆序
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
void str_inserse(char* str)
{
char* start = str;
char* end = str + strlen(str) - 1;
while (start < end)
{
char tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
}
int str_abcbb(char* str)
{
char* start = str;
char* end = str + strlen(str) - 1;
while (start < end)
{
if (*start != *end)
{
return 0;
}
start++;
end--;
}
return 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;
}
8.1.3 字符串拷贝
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
8.1.4 字符串拼接strcat和strncat
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
8.1.5 strcmp和strcmpz
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char* str1 = "helloworld";
char* str2 = "helloz";
printf("ret = %d\n", strcmp(str1, str2));
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char* str1 = "helloworld";
char* str2 = "helloz";
printf("ret = %d\n", strncmp(str1, str2, 8));
system("pause");
return EXIT_SUCCESS;
}
8.1.6 格式化度读入和写出sprintf,sscanf
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char buf[100] = { 0 };
sprintf(buf, "%d%c%d=%d\n", 10, '+', 34, 10 + 34);
puts(buf);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char buf[100] = { 0 };
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;
}
8.1.7 字符串分割strtok
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[] = "www.itcast.cn.com.net";
char* p = strtok(str, ".");
while (p != NULL)
{
p = strtok(NULL, ".");
printf("%s\n", p);
}
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
char str[] = "www.baidu.com$This is a strtok$test";
char* p = strtok(str, "$ .");
while (p != NULL)
{
p = strtok(NULL, ". $");
printf("p = %s\n", p);
}
system("pause");
return EXIT_SUCCESS;
}
8.1.8 atol_atof_atol
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
8.2 局部变量
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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);
}
printf("i 2 = %d\n", i);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int *p = (int *)malloc(sizeof(int) * 10);
if (p == NULL)
{
printf("malloc error\n");
return -1;
}
char *tmp = p;
for (size_t i = 0; i < 10; i++)
{
p[i] = i + 10;
}
for (size_t i = 0; i < 10; i++)
{
printf("%d ", *p);
p++;
}
free(tmp);
p = NULL;
system("pause");
return EXIT_SUCCESS;
}
8.3 申请堆空间
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int* p = (int*)malloc(sizeof(int) * 10);
if (p == NULL)
{
printf("malloc error\n");
return -1;
}
char* tmp = p;
for (size_t i = 0; i < 10; i++)
{
p[i] = i + 10;
}
for (size_t i = 0; i < 10; i++)
{
printf("%d ", *p);
p++;
}
free(tmp);
p = NULL;
system("pause");
return EXIT_SUCCESS;
}
8.4 二级指针malloc空间
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
int** p = malloc(sizeof(int*) * 3);
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));
}
printf("\n");
}
for (size_t i = 0; i < 3; i++)
{
free(p[i]);
p[i] = NULL;
}
free(p);
p = NULL;
system("pause");
return EXIT_SUCCESS;
}
9. 文件
9.1 联合体和枚举
9.1.1 联合体
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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 };
9.1.2 枚举
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
9.2 系统文件
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
fclose(stdout);
printf("hello file\n");
system("pause");
return EXIT_SUCCESS;
}
9.3 文件的打开和关闭
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{
FILE* fp = NULL;
fp = fopen("test2.txt", "w");
if (fp == NULL)
{
perror("fopen error");
getchar();
return -1;
}
fclose(fp);
printf("------------finish\n");
system("pause");
return EXIT_SUCCESS;
}
9.4 fput和fget
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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)
{
read_file();
system("pause");
return EXIT_SUCCESS;
}
9.5 feof
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
9.6 fgets和fputs
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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;
}
9.7 四则运算(文件版)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
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);
if (feof(fp))
{
break;
}
sscanf(buf, "%d%c%d=\n", &a, &ch, &b);
sprintf(result, "%d%c%d=%d\n", a, ch, b, calc(ch, a, b));
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
#include
#include
#include
#include
void write_file()
{
FILE* fp = fopen("abc.c", "w");
if (!fp)
{
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)
{
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);
}
void read_file2()
{
int a;
FILE* fp = fopen("abc.c", "r");
if (!fp)
{
perror("fopen error");
return -1;
}
while (1)
{
fscanf(fp, "%d\n", &a);
printf("%d\n", a);
if (feof(fp))
break;
}
fclose(fp);
}
void read_file3()
{
char buf[1024];
FILE* fp = fopen("abc.c", "r");
if (!fp)
{
perror("fopen error");
return -1;
}
while (1)
{
memset(buf, 0, 1024);
fgets(buf, 1024, fp);
if (feof(fp))
break;
printf("%d\n", buf[0]);
}
fclose(fp);
}
int main(void)
{
FILE* fp = fopen("test0101.txt", "r");
if (!fp)
{
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
#include
#include
#include
#include
void write_rand()
{
FILE *fp = fopen("test02.txt", "w");
if (!fp)
{
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)
{
perror("fopen error");
return -1;
}
while (1)
{
fscanf(fp, "%d\n", &arr[i]);
i++;
if (feof(fp))
break;
}
BubbleSort(arr, sizeof(arr)/sizeof(arr[0]));
fclose(fp);
fp = fopen("test02.txt", "w");
if (!fp)
{
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
#include
#include
#include
#include
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];
int i = 0;
while (1)
{
int ret = fread(&s1[i], 1, sizeof(stu_t), fp);
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
#include
#include
#include
#include
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
#include
#include
#include
#include
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)
{
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);
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
#include
#include
#include
#include
int main(void)
{
FILE *fp = fopen("test0501.txt", "w+");
int ret = fputs("11111", fp);
printf("ret 1 = %d\n", ret);
ret = fputs("22222", fp);
printf("ret 2 = %d\n", ret);
ret = fputs("33333", fp);
printf("ret 3 = %d\n", ret);
char buf[1024] = { 0 };
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
#include
#include
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
#include
#include
#include
#include
#include
#include
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
#include
#include
#include
#include
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;
}