欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

C语言小知识点练习总结

发布时间:2025/3/21 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C语言小知识点练习总结 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

最近在准备C语言的上级考试,之前对C接触不多,在练习过程中把一些小知识点记录下来。

1.字符串的截取

利用strncpy函数,传入三个参数,分别为目标字符串,起始位置,长度。

例如将日期字符串转化为数字,如20120112

#include <stdio.h> #include <string.h> #include <stdlib.h>int main() {char date1[20],date2[20];scanf("%s",&date1);scanf("%s",&date2);char temp[4];int year1 = atoi(strncpy(temp,date1,4));int year2 = atoi(strncpy(temp,date2,4));printf("year1:%d\n",year1);printf("year2:%d\n",year2);char temp2[2];int month1 = atoi(strncpy(temp2,date1+4,2));int month2 = atoi(strncpy(temp2,date2+4,2));printf("month1:%d\n",month1);printf("month2:%d\n",month2);int day1 = atoi(strncpy(temp2,date1+6,2));int day2 = atoi(strncpy(temp2,date2+6,2));printf("day1:%d\n",day1);printf("day2:%d\n",day2);return 0;}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <stdio.h> #include <string.h> #include <stdlib.h> int main() {     char date1[20],date2[20];     scanf("%s",&date1);     scanf("%s",&date2);     char temp[4];     int year1 = atoi(strncpy(temp,date1,4));     int year2 = atoi(strncpy(temp,date2,4));     printf("year1:%d\n",year1);     printf("year2:%d\n",year2);     char temp2[2];     int month1 = atoi(strncpy(temp2,date1+4,2));     int month2 = atoi(strncpy(temp2,date2+4,2));     printf("month1:%d\n",month1);     printf("month2:%d\n",month2);     int day1 = atoi(strncpy(temp2,date1+6,2));     int day2 = atoi(strncpy(temp2,date2+6,2));     printf("day1:%d\n",day1);     printf("day2:%d\n",day2);     return 0; }

以上便实现了输入一个日期然后对其进行分割的操作。














2.二维数组的动态声明

利用malloc可以实现数组的动态声明

int **a;a = (int **)malloc(2*sizeof(int *));int i,j;for (i = 0; i < 2; i ++) {a[i] = (int *)malloc(3*sizeof(int));for (j = 0; j < 3; j++) {scanf("%d",&a[i][j]);}}
1 2 3 4 5 6 7 8 9 int **a;     a = (int **)malloc(2*sizeof(int *));     int i,j;     for (i = 0; i < 2; i ++) {         a[i] = (int *)malloc(3*sizeof(int));         for (j = 0; j < 3; j++) {             scanf("%d",&a[i][j]);         }     }

以上便实现了动态数组的分配,利用scanf为数组赋值







3.二维数组的声明和初始化

头文件

#include <memory.h>
1 #include <memory.h>

初始化和测试

int result[2][2];for (i = 0; i < 2; i ++) {for (j = 0; j < 2; j++) {printf("%d ",result[i][j]);}printf("\n");}memset(result,0,sizeof(int)*4);for (i = 0; i < 2; i ++) {for (j = 0; j < 2; j++) {printf("%d ",result[i][j]);}printf("\n");}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 int result[2][2];     for (i = 0; i < 2; i ++) {         for (j = 0; j < 2; j++) {             printf("%d ",result[i][j]);         }         printf("\n");     }     memset(result,0,sizeof(int)*4);     for (i = 0; i < 2; i ++) {         for (j = 0; j < 2; j++) {             printf("%d ",result[i][j]);         }         printf("\n");     }

结果

4196944 0 4195696 0 0 0 0 0
1 2 3 4 4196944 0 4195696 0 0 0 0 0

上述是数组的非动态声明









4.快速排序

假设要排序的数组是A[1]……A[N],首先任意选取一个数据(通常选用第一个数据)作为关键数据,然后将所有比它的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序。一趟快速排序的算法是:

1)设置两个变量I、J,排序开始的时候 I=0,J=N-1;

2)以第一个数组元素作为关键数据,赋值给X,即X=A[0];

3)从J开始向前搜索,即由后开始向前搜索,找到第一个小于X的值,两者交换;

4)从I开始向后搜索,即由前开始向后搜索,找到第一个大于X的值,两者交换;

5)重复第3、4步,直到I=J;

例如:待排序的数组A的值分别是:(初始关键数据X:=49)

A[0] A[1] A[2] A[3] A[4] A[5] A[6]
49 38 65 97 76 13 27

进行第一次交换后: 27 38 65 97 76 13 49
( 按照算法的第三步从后面开始找 )

进行第二次交换后: 27 38 49 97 76 13 65
( 按照算法的第四步从前面开始找>X的值,65>49,两者交换,此时I=3 )

进行第三次交换后: 27 38 13 97 76 49 65
( 按照算法的第五步将又一次执行算法的第三步从后开始找)

进行第四次交换后: 27 38 13 49 76 97 65
( 按照算法的第四步从前面开始找大于X的值,97>49,两者交换,此时J=4 )

此时再执行第三不的时候就发现I=J,从而结束一躺快速排序,那么经过一躺快速排序之后:
27 38 13 49 76 97 65
即所有大于49的数全部在49的后面,所有小于49的数全部在49的前面。

#include <stdio.h>void quiksort(int a[],int low,int high) {int i = low;int j = high; int temp = a[i]; if( low < high){ while(i < j) {while((a[j] >= temp) && (i < j)){ j--; }a[i] = a[j];while((a[i] <= temp) && (i < j)){i++; } a[j]= a[i];}a[i] = temp;quiksort(a,low,i-1);quiksort(a,j+1,high);}else{return;} }int main() {int arr[5] = {23,1,21,4,19};quiksort(arr,0,4);int i;for(i=0;i<5;i++){printf("%d ",arr[i]);}printf("\n");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <stdio.h> void quiksort(int a[],int low,int high) {     int i = low;     int j = high;       int temp = a[i];        if( low < high)     {                   while(i < j)         {             while((a[j] >= temp) && (i < j))             {                 j--;             }             a[i] = a[j];             while((a[i] <= temp) && (i < j))             {                 i++;             }               a[j]= a[i];         }         a[i] = temp;         quiksort(a,low,i-1);         quiksort(a,j+1,high);     }     else     {         return;     } } int main() {     int arr[5] = {23,1,21,4,19};     quiksort(arr,0,4);     int i;     for(i=0;i<5;i++)     {         printf("%d ",arr[i]);     }     printf("\n");     return 0; }

快速排序代码如上






















5.字符串拷贝

小例子如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello";char *a = "world";strcpy(str,a);printf("%s",str);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){     char str[20] = "hello";     char *a = "world";     strcpy(str,a);     printf("%s",str);     printf("\n");     system("pause");     return 0; }

运行结果:world,即把后者完全覆盖前者。

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char *str = new char[6];char *a = "world";strcpy(str,a);printf("%s", str);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){     char *str = new char[6];     char *a = "world";     strcpy(str,a);     printf("%s", str);     printf("\n");     system("pause");     return 0; }

运行结果一致

某一长度的字符串截取

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char *str = new char[6];char *a = "world";strncpy(str,a+1,5);printf("%s", str);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){     char *str = new char[6];     char *a = "world";     strncpy(str,a+1,5);     printf("%s", str);     printf("\n");     system("pause");     return 0; }

运行结果:orld











6.字符串的拼接

小例子如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello ";char *a = "world";char *x = strcat(str,a);printf("%s\n", x);printf("%s\n", x);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){     char str[20] = "hello ";     char *a = "world";     char *x = strcat(str,a);     printf("%s\n", x);     printf("%s\n", x);     printf("\n");     system("pause");     return 0; }

运行结果:

hello world

hello world

函数既返回结果,又将目标字符串赋值








7.字符串查找匹配

例子如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello ";char *x = strchr(str,'e');printf("%d\n", x - str);printf("%s\n",x);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){     char str[20] = "hello ";     char *x = strchr(str,'e');     printf("%d\n", x - str);     printf("%s\n",x);     printf("\n");     system("pause");     return 0; }

运行结果:

1

ello








8.字符串比较

例子如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello ";char str2[20] = "hello2";int x = strcmp(str, str2);printf("%d\n",x);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){     char str[20] = "hello ";     char str2[20] = "hello2";     int x = strcmp(str, str2);     printf("%d\n",x);     printf("\n");     system("pause");     return 0; }

运行结果-1

忽略大小写

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello";char str2[20] = "Hello";int x = stricmp(str, str2);printf("%d\n",x);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){     char str[20] = "hello";     char str2[20] = "Hello";     int x = stricmp(str, str2);     printf("%d\n",x);     printf("\n");     system("pause");     return 0; }

运行结果0








9.字符串分割

示例如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main() {char test1[] = "feng,ke,wei"; char x[3][30];char *p; p = strtok(test1, ",");int count = 0;while (p) { printf("%s\n", p); strcpy(x[count],p);count++;p = strtok(NULL, ","); } for (int i = 0; i<count; i ++) {printf("%s ", x[i]);}system("pause");return 0;}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <stdio.h> #include <string.h> #include <stdlib.h> int main() {       char test1[] = "feng,ke,wei";         char x[3][30];       char *p;         p = strtok(test1, ",");       int count = 0;       while (p) {           printf("%s\n", p);           strcpy(x[count],p);         count++;         p = strtok(NULL, ",");           }           for (int i = 0; i<count; i ++) {         printf("%s ", x[i]);       }       system("pause");       return 0; }

运行结果

feng
ke
wei
feng ke wei











10.格式化输出几位小数

例如

printf("%.5f",18.223);
1 printf("%.5f",18.223);

则是输出5位小数

又如

printf("%5.1f",1.2345);
1 printf("%5.1f",1.2345);

则是控制总位数为5,小数点后为1位,不够的在前面补空格

转载请注明:静觅 » C语言小知识点练习总结

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的C语言小知识点练习总结的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。