#includeintmain(void){int a =3;// 定义 整型变量 a, 赋初值 3int b =5;// 定义 整型变量 b, 赋初值 5int c;// 定义 整型变量 c, 无初值c = a + b;// a + b ,将结果 赋值 给 cprintf("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);return0;}
1.4 hello
#include#definePI3.14// 定义常量 PI宏定义// 我是一个单行注释君intmain(void){printf("helloworld\n");/*wo shi 一个多行注释 sir*/printf("%d\n, PI");return0;}
2 常量变量和数据类型
2.1 常量
#include#definePI3.1415// 常量intmain(void){// 圆的面积s = PI x 半径的平方// 圆的周长l = 2 * PI * r//int r = 3;// 变量constint r =3;// 只读变量float s = PI * r * r;float l =2* PI * r;//printf("圆的周长为:%f\n", l);//18.849001//printf("圆的面积为:%f\n", s);//28.273500printf("圆的周长为:%.2f\n", l);// 指定小数点后保留2位, 对第3位做,4舍五入printf("圆的面积为:%.2f\n", s);// 指定小数点后保留2位return0;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include#defineN1024// 定义常量intmain(void){int a =10;// 定义变量: 三要素short b =20;long c =30;longlong 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(longlong));system("pause");return EXIT_SUCCESS;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include#defineN1024// 定义常量intmain(void){int a =3;short b =4;long c =5;// 5L5llonglong d =6;// 5LL5llprintf("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(longlong));printf("--------------------------------------\n");unsignedint aun =3;// 3uunsignedshort bun =4;// 3uunsignedlong cun =5;// 3luunsignedlonglong dun =6;// 3lluprintf("sizeof(unsigned int) = %d\n",sizeof(unsignedint));// aunprintf("sizeof(unsigned short) = %d\n",sizeof(unsignedshort));printf("sizeof(unsigned long) = %d\n",sizeof(unsignedlong));printf("sizeof(unsigned long long) = %d\n",sizeof(unsignedlonglong));}
2.4 无符号整型
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(void){size_t var =10;printf("var = %u\n", var);unsignedint a =10u;// 简写成 unsigned int a = 10;unsignedshort b =20u;// 简写成 unsigned short b = 20;unsignedlong c =30Lu;unsignedlonglong 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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// A -- 65 a -- 97 intmain(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#includeintmain(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#includeintmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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 : 100);system("pause");return EXIT_SUCCESS;}
3.5.2 三目运算案例2
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// intmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// while 的基础用法intmain(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// 水仙花数:一个三位数。各个位上的数字的立方和等于本数字。 intmain(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#includeintmain(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");}elseif(n > num)printf("猜大了\n");else{printf("猜中!!!\n");break;// 跳出}}printf("本尊是:%d\n", num);system("pause");return0;}
4.1.2 省略的表达式1
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 基础for循环intmain(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;}
4.1.3 省略表达式2
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 基础for循环intmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 表达式有多个intmain(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#includeintmain(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");}elseif(n > num)printf("猜大了\n");else{printf("猜中!!!\n");break;// 跳出}}printf("本尊是:%d\n", num);system("pause");return0;}
4.3 模拟电子表
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include#includeintmain(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// 正序99乘法表intmain(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// 倒序 99 乘法表intmain(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#includeintmain(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#includeintmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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");return0;}
4.7 数组
4.7.1 数组简介
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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;}
4.7.2 数组大小与个数
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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;}
4.7.3 数组初始化
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 数组初始化intmain(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;}
4.8 数组逆序
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 数组元素逆序intmain(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;}
4.9 冒泡排序
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(void){char str[11]={0};// helloworld -->26个英文字母 a-za: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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include//fputsintmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include//fputsintmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 进阶版intmain(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;}
5.4 字符串拼接
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(void){char str1[]="hello";// [ h e l l o \0 ]char str2[]="world";char str3[100];int i =0;// 循环 str1while(str1[i]!='\0')// '\0' != '\0'{str3[i]= str1[i];// 循环着将str1中的每一个元素,交给str3i++;}// str3=[h e l l o]//printf("%d\n", i);--> 5int j =0;// 循环 str2while(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;}
5.5 函数
5.5.1 函数声明定义
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includevoidbubble_sort(int arr[]);// 函数声明 int arr[] 函数形参voidprint_arr(int arr[]);intmain(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(); 做退出}voidprint_arr(int arr[]){for(size_t i =0; i <10; i++){printf("%d ", arr[i]);}}voidbubble_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;}}}}intadd(int a,int b){return a + b;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 修饰变量intmain(void){constint 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// const int *p;intmain(void){int a =10;int b =30;constint* p =&a;//*p = 500;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#includeintmain(void){int a =10;int b =30;intconst* p =&a;//*p = 300;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#includeintmain(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;}
6.4.5 const int * const p
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(void){int a =10;int b =30;constint*const p =&a;//*p = 300;//p = &b;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// 操作数组元素的 4 种方法intmain(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;}
6.5.2 指针操作
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 使用指针++操作数组元素intmain(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;}
6.6 指针算法运算
6.6.1 指针±操作
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 指针在数组中 +- 整数intmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include// 指针加减指针intmain(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#includeintmystrlen(char arr[]);intmain(void){char abc[]="hello world";int ret =mystrlen2(abc);// 实际参数 abcprintf("ret = %d\n", ret);system("pause");return EXIT_SUCCESS;}// 借助数组 实现 intmystrlen(char str[]){int i =0;while(str[i]!='\0'){i++;}return i;}// 借助指针++ 实现 intmystrlen2(char str[]){char* p = str;while(*p !='\0'){p++;}return p - str;// 返回数组元素的个数。}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includevoidtest1(void);// 全局函数声明int m =4456;intmain(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;}
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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;}
8.3 申请堆空间
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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;}
8.4 二级指针malloc空间
#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#includeintmain(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;}