![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c语言程序设计
行躬
纸上得来终觉浅,绝知此事要躬行。
展开
-
[Error] ‘for‘ loop initial declarations are only allowed in C99 or C11 mode解决
原创 2021-11-30 15:35:45 · 982 阅读 · 0 评论 -
二分查找的程序实现中,如果left和right的更新不是取mid+1和mid-1而是都取mid
原因:会导致程序进入死循环验证:以查找数组中不存在的元素进行验证原创 2021-10-26 16:52:51 · 602 阅读 · 1 评论 -
结构体指针的探究
#include <stdio.h>#include <malloc.h>struct Node{ int date; struct Node * Next;};typedef struct Node * Test; int main(void){ /* 探究: a是一个结构型指针变量,并且为其分配了内存,将其中的整型变量赋值为12,重新定义另一个同类型的 结构型指针变量b,并未给其分配内存,将a赋值给b,通过b->date访问b中的整型数值,看是否可以原创 2021-10-26 14:36:48 · 49 阅读 · 0 评论 -
c语言--关于全局与局部数组的内存分配,以及是否分配值探究
int array[8][8];int mian(void) { int arr[8][8];}总结:以int型数组为例:没有为数组分配内存,数组中的元素的值是垃圾值有为数组分配内存,数组中元素默认赋值为0原创 2021-10-23 16:53:10 · 348 阅读 · 0 评论 -
typedef用于创建自定义的指针类型
参考:c primer plus总结:将代码中的char改为别的类型(例如:自定义的结构类型也可)原创 2021-10-12 11:25:43 · 177 阅读 · 0 评论 -
关于typedef用于结构
选自c primes plus原创 2021-10-12 10:36:35 · 52 阅读 · 0 评论 -
devc++编译窗口无法显示(报错的信息看不到)
解决:1.视图—》将浮动报告窗口打上勾2.打钩之后出现如下—报告窗口,然后将该窗口上下拉动放大,就会看到编译信息3.然后将–报告窗口移动到最下面,点击x这个按钮即可原创 2021-10-10 14:15:02 · 36069 阅读 · 6 评论 -
scanf函数详解
1.论证:当scanf函数扫描完其格式串,或者碰到某些输入无法与格式控制说明匹配的情况时,该函数终止,也就是说只保存了匹配成功的,后面中断的并未保存验证思路:写如下scanf()格式:scanf("%d %d %d %d",&a,&b,&a1,&a2);输入如下数据:1 2 'a' 'b'输出格式:printf("%d %d %c %c",a,b,a1,a2);因为输入到scanf中的后两个元素并不匹配,如果输出只输出前两个数据,即得证代码实现:#原创 2021-10-04 21:18:10 · 899 阅读 · 0 评论 -
用c语言实现一个简单的学生管理系统
#include <stdio.h>#include <malloc.h>#include <stdbool.h>//引入bool类型struct Student { int age; float score; char name[100];};//弄一个排序,按成绩从大到小存储void bubbleSort(struct Student *array, int length) { bool flag = false;//优化 for(i原创 2021-10-04 17:33:00 · 254 阅读 · 0 评论 -
冒泡排序及其优化(c语言)
#include <stdio.h>#include <stdbool.h>void bubbleSort(int *array, int length) { bool flag = false;//优化 for(int i = 0; i < length - 1; i++) {//最多只需遍历length-1次就可以将其排序好 for(int j = 0; j < length - 1 - i; j++) {原创 2021-10-04 15:43:06 · 162 阅读 · 0 评论 -
取出结构体中每一个变量的成员并对其进行赋值(standard edition)
#include <stdio.h>#include <string.h>//定义结构体的第1种方式,推荐使用该方式 struct Student{ int age; char name[50]; float height;};//相当于定义了一个数据类型 int main(void) { struct Student student2; student2.age = 21; strcpy(student2.name, "Tome原创 2021-10-04 11:35:38 · 218 阅读 · 0 评论 -
结构体的赋值和初始化(以及字符串数组如何赋值)
#include <stdio.h>#include <string.h>//定义结构体的第1种方式,推荐使用该方式 struct Student{ int age; char name[50]; float height;};//相当于定义了一个数据类型 int main(void) { struct Student student1 = {20,"xurong",174.5};//定义一个变量时,并直接对其赋值 struct Student stu原创 2021-10-04 10:44:16 · 1241 阅读 · 0 评论 -
定义结构体的3种方式
#include <stdio.h>//定义结构体的第1种方式,推荐使用该方式 struct Student{ int age; char name[100]; float height;};//相当于定义了一个数据类型 //定义结构体的第2种方式,该方式定义的结构体只能使用1次 struct Student2{ int age; char name[100]; float height;} studnet2; //定义结构体的第3种方式,数据类型名字都没有,不推原创 2021-10-04 10:16:30 · 860 阅读 · 0 评论 -
动态内存可以夸函数使用
#include <stdio.h>void f(int ** q) {//1.q是一个指针变量,因为本机是64位操作系统,所以该变量占8个字节 *q = (int *)malloc(sizeof(int)); //等价于 p = (int *)malloc(sizeof(int)); **q = 5; }int main(void) { int * p; f(&p); printf("%d\n",*p); return 0;} 输出结果:5----原创 2021-10-04 09:12:11 · 77 阅读 · 0 评论 -
静态变量不能夸函数使用
#include <stdio.h>void f(int ** q) {//1.q是一个指针变量,因为本机是64位操作系统,所以该变量占8个字节 int i = 5; //2.q是 传入的是指针变量p的地址,所以*q = p; 可用于存放一个int类型的地址 *q = &i;//p = &i; }int main(void) { int *p; f(&p); printf("%d\n",*p);//3.本句语法没有错误,但是逻辑上有错误(如下分析原创 2021-10-04 08:58:09 · 123 阅读 · 0 评论 -
动态内存分配地址举例-动态一维数组举例
#include <stdio.h>#include <malloc.h>int main(void) { int a[5]; /*1. 如上,构建了一个静态数组,如果int占4个字节,本数组总共占20个字节 */ int len; int *pArr; printf("请输入你要存放的元素个数:"); scanf("%d",&len); pArr = (int *)malloc(4 * len);//malloc函数返回值的类型是(void*),原创 2021-09-19 11:28:30 · 239 阅读 · 0 评论 -
动态分配的内存变量所对应的修改值(malloc应用)
#include <stdio.h>#include <malloc.h>void f(int * q) {// *p = 200;//error:p变量并没有在该函数中定义// q = 200;//error:字节将地址更改,没有达到要求 *q = 200;//ok// free(q);//error:将导致值刚刚被更改,然后q指向的4个字节的内存就被释放了,4个字节所对应的值自然不复存在,只留下一个垃圾值12190032 } int main(void)原创 2021-09-12 20:20:32 · 116 阅读 · 0 评论 -
malloc函数简单介绍-动态内存分配
#include <stdio.h>#include <malloc.h>//引入动态分配内存的函数 (malloc:memory(内存) allocate(分配)的缩写) int main(void) { int i = 5;//分配了4个字节,属于静态分配的内存,函数不终止,内存就不会释放 int * p = (int *)malloc(4);//动态分配的内存,随时可以释放 (第5行) /* 1.要使用malloc函数,必须添加malloc.h头文件 2.原创 2021-09-12 19:45:56 · 340 阅读 · 0 评论 -
一个变量的地址只可用第一个字节地址所表示,虽然r只指向第一个字节的地址,但指针变量类型就可以确定其本身所对应变量占几个字节
#include <stdio.h>int main(void) { char ch = 'A'; int i = 99; double x = 66; char * p = &ch; int * q = &i; double * r = &x; printf("%d %d %d\n",sizeof(p),sizeof(q),sizeof(r));//8 8 8,sizeof(变量或数据类型)返回其在内存中所占字节数 /* 1. char占1个字节原创 2021-09-12 17:08:17 · 309 阅读 · 0 评论 -
指针相减--仅限于同一块内存空间
#include <stdio.h>int main(void) { int b[6] = {3,-1,-4,0,5}; printf("b[5]与b[3]相差%d个单元\n",&b[5] - &*(b+3)); return 0;} 输出结果:b[5]与b[3]相差2个单元--------------------------------Process exited after 0.01435 seconds with return value 0请按任原创 2021-09-12 15:53:30 · 71 阅读 · 0 评论 -
将数组用一个函数输出
#include <stdio.h>//造一个f()函数将数组内容输出void f(int * pArr,int len) {//知道数组的首地址以及数组的长度即数组元素的个数,即可知道一个数组全貌 for (int i = 0; i < len; i++) {// printf("%d ",*(pArr+i));//*(pArr+i)等价于pArr[i],等价于b[i] printf("%d ",pArr[i]); }} int main(void) { int原创 2021-09-12 15:37:20 · 1738 阅读 · 1 评论 -
指针与一维数组的探索-通过被调函数修改主调函数中数组的值
#include <stdio.h>//造一个f()函数将数组内容输出void f(int * pArr,int len) {//知道数组的首地址以及数组的长度即数组元素的个数,即可知道一个数组全貌 pArr[3] = 666;} int main(void) { int b[6] = {3,-1,-4,0,5}; printf("被调函数调用之前b[3]:%d\n",b[3]); f(b,6); printf("被调函数调用之后b[3]:%d\n",b[3]); ret原创 2021-09-12 15:21:25 · 765 阅读 · 0 评论 -
指针与一维数组探索
#include <stdio.h>int main(void) { int a[5];//a是数组名 int b[5];/*1.a = b;//error:一维数组名是个指针常量,它存放的是一维数组第一个元素的地址,两个都是指针常量,无法完成赋值操作 */ printf("%#d\n",&a[0]);//以16进制输出地址值 printf("%#d\n",&a);/*2.64875526487552------------------------原创 2021-09-12 14:49:50 · 68 阅读 · 0 评论 -
经典问题对指针理解-利用指针写一个函数实现两个数的互换
#include <stdio.h>void swap(int *,int *);int main (void) { int a = 2; int b = 3; swap(&a,&b); printf("a = %d,b = %d\n",a,b); return 0; } /*利用指针写一个函数实现两个数的互换 */ void swap(int *p,int *q) {//传入的是a,b的地址,所以 *p就是a,*q就是b int temp原创 2021-09-05 16:12:36 · 161 阅读 · 0 评论 -
c指针-错误2(出错):int类型与int * 类型不匹配
#include <stdio.h> int main (void) { /* 错误2(出错):int类型与int * 类型不匹配 */ int i= 5; int *p; int *q; p = &i; *q = p; printf("%d\n",*q); return 0; }报错信息:[Warning] assignment makes integer from pointer without a cast赋值使整数来自指原创 2021-09-05 14:51:08 · 781 阅读 · 0 评论 -
c指针-错误1(给出警告):下面代码并没有给p指定地址值,而是直接在垃圾地址上对*p进行赋值
#include <stdio.h> int main (void) { /* 错误1(给出警告):下面代码并没有给p指定地址值,而是直接在垃圾地址上对*p进行赋值 */ int * p; int i = 5; *p = i; printf("%d\n",*p); return 0; }原创 2021-09-05 14:46:45 · 80 阅读 · 0 评论 -
c语言指针基本理解
#include <stdio.h> int main (void) { int * p1; //1.p是变量的名字,int * 表示p1变量存放的是int类型变量的地址(int * 为一个类型:)// int * 为一个类型:p是变量名,p变量的数据类型是 int *类型 double * p2;//2.同理:int * 表示p1变量存放的是double类型变量的地址 int i = 3;//3.表示存放的是int类型变量的值 p1 = &i;//合法/原创 2021-09-05 11:23:20 · 124 阅读 · 0 评论 -
%g及其边界值测试
#include <stdio.h>int main() { printf("测试float精确位数\n"); printf("%f\n",1.44e-4); printf("%f\n",1.44e-5); printf("%f\n",1.44e-6); printf("%f\n",1.44e-7);//只精确到小数点后6位 printf("-----------------\n%e\n",1.44e-7); //%g用来输出实数,它根据数值的大小,自动选f格式或e格式(原创 2021-08-16 17:45:50 · 161 阅读 · 0 评论 -
需求4-1:编写函数strrindex(s,t),它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1;
#include <stdio.h>int strrindex(char s[],char t[]);/*需求4-1:编写函数strrindex(s,t),它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1;思路(仿p59):*/int main(){ printf("%d\n",strrindex("abcdef","cde")); return 0; } int strrindex(char s[],char t[]) { int i, j, k;原创 2021-08-14 10:27:45 · 1934 阅读 · 0 评论 -
atoi函数:将字符串s转换为相应的整型数
#include <stdio.h>#include <ctype.h>int atoi1(char s[]);int atoi2(char s[]);/*atoi函数:将字符串s转换为相应的整型数*/int main(){ printf("%d\n",atoi1("123")); printf("%d\n",atoi2(" -123")); return 0;} /*版本一:*/ int atoi1(char s[]) { int i,n; n = 0原创 2021-08-09 09:47:47 · 828 阅读 · 0 评论 -
16进制格式化输出输出
#include <stdio.h>#include <ctype.h>#include <limits.h> int main(void){ for (int ndx=0; ndx<=UCHAR_MAX; ndx++) if (isspace(ndx)) printf("0x%2x\n", ndx);}输出:0x 90x a0x b0x c0x d0x20#include <stdio.h>#i原创 2021-08-09 09:22:27 · 1528 阅读 · 0 评论 -
位运算(未完成)
#include <stdio.h>int main(){ /* 1.按位与运算符:& 1 & 0 = 0; 0 & 1 = 0; 0 & 0 = 0; 1 & 1 = 1; 0001 = 1;0010 = 2; 0011 = 3;0100 = 4; 0101 = 5;0110 = 6; 0111 = 7;1000 = 8; 1001 = 9;1010 = 10; 1011 = 11;1100 = 12; 1101 = 13;原创 2021-08-02 23:57:02 · 54 阅读 · 0 评论 -
逗号运算符的运用-倒置s中的元素
#include <stdio.h>#include <string.h>void reverse(char s[]); /*逗号运算符的运用-倒置s中的元素*/int main(){ char s[] = "1,3,5,13,15,2,4";// char s[] = {1,3,5,13,15,2,4};//只能用for循环遍历将其输出 reverse(s);// for (int i = 0; i < strlen(s); i++) {// print原创 2021-08-02 23:10:21 · 47 阅读 · 0 评论 -
编写一个函数escape(s,t), 将字符串t复制到字符串s中,并在复制过程中将换行符,制表符等不可见字符分别转换为\n,\t等相应的可见转义字符序列。
#include <stdio.h>void escape(char s[],char t[]);void unescape(char s[],char t[]); /*编写一个函数escape(s,t), 将字符串t复制到字符串s中,并在复制过程中将换行符,制表符等不可见字符分别转换为\n,\t等相应的可见转义字符序列。要求使用switch语句。再编写一个具有相反功能的函数,在复制过程中将转义字符序列转换为实际字符。 思考:字符串是一个字符数组,为此,在复制过程中需要得到单个原创 2021-08-02 20:49:03 · 1213 阅读 · 0 评论 -
需求2-7: 编写一个函数invert(x,p,n),该函数返回对x执行下列操作后的结果值: 将x中从第p位开始的n个(二进制)位求反(即1变成0,0变成1),x的其余各位保持不变。
#include <stdio.h>unsigned invert1(unsigned x,int p,int n);unsigned invert2(unsigned,int,int);/*需求2-7:编写一个函数invert(x,p,n),该函数返回对x执行下列操作后的结果值:将x中从第p位开始的n个(二进制)位求反(即1变成0,0变成1),x的其余各位保持不变。 思路:先将x从p开始的n位截取出来(p开始的n位屏蔽操作),然后进行取反操作, 再将其结果p开始的n位截取出来(原创 2021-07-31 08:10:04 · 447 阅读 · 0 评论 -
位运算规格
位运算公式:1.左移:expression << 需要向左移动的位数(低级错误:将表达式写在右边)2.右移:expression >> 需要向右移动的位数原创 2021-07-31 08:00:21 · 56 阅读 · 0 评论 -
c-二分查找运用
#include <stdio.h>int binsearch(int x, int v[], int n);int main(void){ int v[8] = {1,3,5,7,9,15,18,23}; printf("%d\n",binsearch(15, v,sizeof(v) / sizeof(v[0]))); return 0;} /*法1:二分查找运用 */ int binsearch(int x, int v[], int n) { int low,hig原创 2021-07-30 11:01:00 · 55 阅读 · 0 评论 -
编写一个函数,该函数返回对x执行下列操作后的结果值: 将x中从第p位开始的n个(二进制)位设置为y中最右边n位的值,x其余各位保持不变。
#include <stdio.h>unsigned setbits(unsigned x,int p, int n,unsigned y);/*需求2-6:编写一个函数setbits(unsigned x,unsigned p,unsigned n,unsigned y),该函数返回对x执行下列操作后的结果值:将x中从第p位开始的n个(二进制)位设置为y中最右边n位的值,x其余各位保持不变。思路: 首先获取y最右边的值,可利用按位与运算符:~(~0<<n)设置移到右边n原创 2021-07-30 07:09:29 · 287 阅读 · 0 评论 -
2.10-赋值运算符与表达式--2-11-条件表达式--2-12-运算符优先级与求值顺序
2.10-赋值运算符与表达式:1.1-1:公式:expr1 = (expr1)op(expr2) expr1 op= expr2(op:+,-,*,/,%,<<,>>,&,^,|)注意:expre2两边的圆括号是必不可少的(x *= y + 1;-->x = x * (y + 1) ) 1-2-eg:i = i + 2;与i += 2;区别:后一种形式值计算一次。总结:赋值表达式的类型是它的左操作数的类型,其值是赋值完成后的值。 2-1.原创 2021-07-28 14:35:59 · 117 阅读 · 0 评论 -
编写函数any(s1,s2),将字符串S2中的任意一个字符在字符串s1中第一次出现的位置作为结果返回。s1中不包含s2中的字符,则返回-1。 (标准库函数strpbrk具有同样的功能,只是返回指针)
#include <stdio.h>int any(char s1[],char s2[]);/*需求2-5:编写函数any(s1,s2),将字符串S2中的任意一个字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2中的字符,则返回-1。 (标准库函数strpbrk具有同样的功能,但他返回的是指向该位置的指针。) 思路:同样利用两个for语句对字符串进行遍历,在第2个for语句的外层返回下标 .*/int main(){ printf("%d\n",any("原创 2021-07-27 22:34:48 · 296 阅读 · 0 评论