
C语言练习
洋洋v风大不大∃
学好技术
努力赚钱
别急
慢慢来!
展开
-
指针相减(数组)
两个同类型指针相减的结构不是以字节为单位的,而是以指针指向的数据类型大小为单位的。 在数组内,两个指针的相减,得出的是中间相隔的元素的个数。 #include <stdio.h> #include <stdlib.h> int main() { int a = 1; int b = 2; int c = 3; int d = 4; int *p1 = &a; int *p2 = &b; int *p3..原创 2022-02-14 23:11:15 · 2573 阅读 · 1 评论 -
按值传递与引用传递
#include <stdio.h> #include <stdlib.h> void change(int); void change(int num) //形参 { num++; //此时的num=10 } int main() { int num = 9; //局部变量 change(num); //调用函数 printf("%d\n",num); //输出的值是main函数里面的9,不是change函数里的值 return 0; .原创 2022-02-03 17:52:19 · 392 阅读 · 0 评论 -
局部变量与全局变量
/** * *MyCounter.c *用来存放计算函数调用次数的函数原型及实现 */ //引用了外部变量 extern int whileCount; void counter(int); //用来计算当前counter函数执行了多少次 void counter(int i) { //静态存储 static int subTotal = 0; //静态变量 subTotal++; printf("counter函数被调用了%d次\n",subTot.原创 2022-02-03 17:23:35 · 702 阅读 · 0 评论 -
静态存储与自动存储
/** * *MyCounter.c *用来存放计算函数调用次数的函数原型及实现 */ int counter(); //用来计算本函数被调用了多少次 int counter() { int count = 0; count++; return count; } #include <stdio.h> #include <stdlib.h> int main() { int count = 0; counter(.原创 2022-02-03 16:04:41 · 612 阅读 · 0 评论 -
C小练习-函数 全局变量
#include <stdio.h> #include <stdlib.h> int count = 0; //全局变量 - 有默认值 void changeNum() { count++; } int main() { changeNum(); changeNum(); printf("count = %d\n",count); return 0; }原创 2022-01-23 11:53:29 · 307 阅读 · 0 评论 -
C小练习-函数 局部变量
#include <stdio.h> #include <stdlib.h> void changeNum() { //局部变量 int num1 = 2, num2 = 4;//修改局部变量值没用 num1 = 55; num2 = 289; } int main() { int num1 = 5,num2 = 8; changeNum(); //调用函数 printf("num1 = %d\tnum2 .原创 2022-01-23 11:52:28 · 200 阅读 · 0 评论 -
C小练习-函数的作用域
#include <stdio.h> #include <stdlib.h> int main() { int num1 = 9; { //代码块 int num2 = 90; printf("%d\n",num1 + num2); } return 0; }原创 2022-01-23 11:05:43 · 413 阅读 · 0 评论 -
C小练习-形参和实参
#include <stdio.h> #include <stdlib.h> //自行实现pow函数 //求第一个参数的n次幂 //double num1,double num2 --形式参数 double pow(double, int); //函数原型 int main() { //实际参数 - 调用 printf("%d的%d次幂等于:%.2lf",9,2,pow(9,2)); //在调用函数的时候,传递的参数是实参; //在定义函数.原创 2022-01-20 13:50:40 · 502 阅读 · 0 评论 -
C小练习-自定义函数2
#include <stdio.h> #include <stdlib.h> //函数原型 int calcSum(); //求1-100之间的偶数和 int main() { //如果函数有返回值,调用时,记得使用对应类型变量来接收 int sum = calcSum(); printf("1-100之间的偶数和为:%d\n",sum); return 0; } int calcSum() { //1-100之间的偶数和 i.原创 2022-01-20 13:12:35 · 357 阅读 · 0 评论 -
C小练习-自定义函数1
#include <stdio.h> #include <stdlib.h> #include <math.h> //函数原型 void calcCircle(); /** *计算圆的面积(函数实现) */ void calcCircle() { double radius, s; printf("请输入圆的半径:"); scanf("%lf",&radius); //圆面积 =3.14乘以半径的平方 s = 3.14.原创 2022-01-18 13:36:27 · 531 阅读 · 0 评论 -
C小练习-内置函数malloc
#include <stdio.h> #include <stdlib.h> int main() { //动态分配内存 int * nums; int i; nums = (int *)malloc(20);//为前面的指针动态分配了20个字节的空间 //等价于 :int nums[5] //为指针动态分配空间后,指针就变成了数组 for(i = 0; i < 5; i++) { print.原创 2022-01-17 11:59:15 · 423 阅读 · 0 评论 -
C小练习-内置函数二
#include <stdio.h> #include <stdlib.h> int main() { printf("%.2lf\n",sqrt(9)); //求平方根 printf("%.2lf\n",pow(9, 2)); //求第一个数字的第二个数字次幂 printf("%d\n",abs(-98)); //求绝对值 return 0; } #include <stdio.h> #inclu.原创 2022-01-16 21:18:32 · 163 阅读 · 0 评论 -
C小练习-内置函数-ceil和floor
#include <stdio.h> #include <stdlib.h> int main() { //常用内置函数, -0表示假,非0表示真 //ceil - [进一法-1以上才进] floor - [去尾法] printf("%.2lf\n",ceil(98.01)); printf("%.2lf\n",floor(98.9)); return 0; } #include <stdio.h> #in..原创 2022-01-16 20:59:53 · 140 阅读 · 0 评论 -
C小练习-内置函数
#include <stdio.h> #include <stdlib.h> int main() { //常用内置函数, -0表示假,非0表示真 printf("%d\n",isupper('a')); //判断是否为大写 printf("%d\n",islower('a')); //判断是否小写 printf("%d\n",isalpha('a')); //返回字符是否为字母 printf("%d\n",isdigit('9')); //返.原创 2022-01-16 20:46:52 · 82 阅读 · 0 评论 -
C小练习-二维数组与指针
#include <stdio.h> #include <stdlib.h> int main() { //二维数组的理解1是由n个一维数组所组成的 int i, j; double score[5][3] = { {55, 32, 37}, {54, 35, 29}, {57, 39, 36}, {68, 46, 68}, {37, 89, 68}, }; for(i = 0; i < 5; .原创 2022-01-15 14:25:01 · 253 阅读 · 0 评论 -
C小练习-二维数组
#include <stdio.h> #include <stdlib.h> int main() { //数组名就是数组的首地址(数组首元素地址) //二维数组的理解:1、由n个一维数组所组成 int i, j; double score[5][3] = { {55, 66, 77}, {52, 62, 72}, {15, 26, 37}, {54, 65, 76}, {55, 66, 77}, }.原创 2022-01-15 14:10:10 · 113 阅读 · 0 评论 -
C小练习-二维数组
#include <stdio.h> #include <stdlib.h> int main() { //二维数组的理解1是由n个一维数组所组成的 int i, j; double score[5][3] = { {55, 32, 37}, {54, 35, 29}, {57, 39, 36}, {68, 46, 68}, {37, 89, 68}, }; //遍历第一行数据:score[0] .原创 2022-01-15 13:59:32 · 75 阅读 · 0 评论 -
C小练习-数组逆序
#include <stdio.h> #include <stdlib.h> #define N 5 int main() { int array [] = {25, 30, 35, 40, 45}; //实现数组的逆序 - 原理就是数组的首尾元素进行交换 int temp; //临时变量 int i; for (i = 0; i < N / 2; i++) { //第i个值和第N-i-1值相交换 .原创 2022-01-07 16:36:26 · 502 阅读 · 0 评论 -
C小练习-数组和指针1
#include <stdio.h> #include <stdlib.h> int main() { int i; double score[5] = {89, 67, 82, 98, 62}; double * ptr_score = score; //使用指针访问数组元素 printf("数组的首地址:%p\t数组首元素的地址:%p\n",score,&score[0]); for (i = 0; i < 5; i++) .原创 2022-01-07 15:09:49 · 492 阅读 · 0 评论 -
C小练习-指针1
#include <stdio.h> #include <stdlib.h> int main() { int num1 = 1024; int num2 = 2048; int * ptr1; int * ptr2; ptr1 = &num1; ptr2 = &num2; printf("num1的值是%d\tnum1的地址是:%p\n", num1, ptr1); printf("num2的值是%.原创 2022-01-04 13:22:18 · 448 阅读 · 0 评论 -
C小练习数组2
#include <stdio.h> #include <stdlib.h> int main() { int i,j; //使用二维数组表示学生的成绩表 //四个学生有三门课成绩 - 4行 3列 double scores[4][3] = { {98, 67, 89}, {89, 67, 88}, {99, 77, 55}, {78, 99, 67}, }; printf("语文\t英语\t数学\.原创 2022-01-03 16:47:50 · 234 阅读 · 0 评论 -
C小练习-数组1
#include <stdio.h> #include <stdlib.h> #define N 5 int main() { //动态录入 double score[N]; int i ; //循环变量 //使用循环操作数组时,要注意:一个循环尽量只做一件事 for (i = 0; i < N; i++ ) { printf("请输入第%d位同学的成绩:", i + 1); scanf("%.原创 2022-01-01 15:10:43 · 345 阅读 · 0 评论 -
C小练习10
数组排序练习 降序排列 - 从大到小 冒泡排序的基础原理:遍历和交换 1 25 16 11 90 23 2 25 16 11 90 23 3 25 16 90 11 23 4 25 16 90 23 11 需要多轮(数组长度-1)轮 每轮比较次数比上一轮-1次【(数组长度- 1)-当前的轮数】 #include <stdio....原创 2021-12-13 20:55:05 · 488 阅读 · 0 评论 -
C小练习九(数组)
数组是一个变量,由数据类型相同的一组元素组成。【内存中的一串连续的空间】 练习: 有一个数组:4,8,2,1,23,344,12,13 循环输出数列的值 求数列中所有数值的和及平均值 猜数(从数组中任意输出一个数,并判断数组中是否有此数) #include <stdio.h> #include <stdlib.h> int main() { //1.定义数组并初始化 int nums[]={4,8,2,1,23,...原创 2021-12-13 17:46:03 · 861 阅读 · 0 评论 -
C小练习八
********** ********* ******** ******* ****** ***** **** *** ** * #include <stdio.h> #include <stdlib.h> int main() { int i,j; for (i = 0; i <= 9; i++) { for (j = 0;j <= 9-i;j++) { printf("*"); .原创 2021-12-10 14:29:47 · 4103 阅读 · 0 评论 -
C小练习七(星号)
打印星号图形有两个关键---双循环【矩形】 1、行数 - 一共有几行 2、列数 - 每行有几列 外层循环 - 控制行(行数、换行) 内层循环 - 控制列(列数、列的符号) 每一行中,行数和列数之间的关系(0开始) i j 0 0 1 2 2 4 3 ...原创 2021-12-10 14:23:39 · 377 阅读 · 0 评论 -
C小练习六(continue)
问题:求1-100之间的偶数和? continue:跳出本次循环,继续下次循环 #include <stdio.h> #include <stdlib.h> int main() { int i; int sum = 0; for (i =0; i <= 100;i ++) { if (i % 2 != 0) //奇数的情况 continue; //跳过奇数的情况 sum += i; }原创 2021-12-08 16:44:44 · 557 阅读 · 0 评论 -
C小练习五
问题:猜价格? #include <stdio.h> #include <stdlib.h> int main() { //循环次数 //每猜一次,猜测总次数+1 int price = 7890; //要用户猜的商品价格,也可以使用随机数生成 int guessPrice;//用户猜测的商品价格 int count = 0; //用来记录猜的次数 srand((unsigned)time(NULL)); //生成随机原创 2021-12-08 14:57:27 · 574 阅读 · 0 评论 -
C小练习四(break)
break小练习 题目:输入玩家的年龄,如果年龄为负停止输入,提示输入错误。 #include <stdio.h> #include <stdlib.h> int main() { //1.定义玩家年龄变量 //2循环输入年龄-循环几次(死循环) //3.如果输入的年龄为负 - 输入完毕后,就需要立即判断变量的值 //for或while int age; //年龄 for (;;) { printf("请输入玩家年龄:");原创 2021-12-08 14:23:23 · 253 阅读 · 0 评论 -
C小练习三
猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃一个,以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再次时,只剩下一个桃子了。求第一天共摘了多少? 设第9天还剩y个,第10天还剩x(x=1) y/2-1=1(x) ---y=4 y/2-1=4(x)-----y=10=(X+1)*2[这里的X就是上得出的y] #include <stdio.h> #include <stdlib.h>...原创 2021-12-07 21:03:54 · 81 阅读 · 0 评论 -
C小练习一
#include <stdio.h> #include <stdlib.h> int main() { while (1) { int choice; //用户的选择 //打印游戏菜单 printf("性格测试:\n"); printf("如果您是一位君王,对于身旁的伴侣您希望?\n"); printf("1、只要有一位真爱的妻子\n"); printf("2、可以两位以上的爱人\n"); .原创 2021-12-07 15:46:44 · 358 阅读 · 0 评论 -
C小练习二
#include <stdio.h> #include <stdlib.h> int main() { int a = 1, b = 10; do { b -= a; a++; } while (b-- < 0); printf("%d\n",b); }原创 2021-12-07 15:52:29 · 115 阅读 · 0 评论