蓝桥杯备赛 第一周 第一讲

第一讲 初始C语言

前言部分

打字手指分区如下

安装编程软件

1.软件和文件都存D盘

2.安装路径不能有中文

创建工程

写一个C代码

#include <stdio.h>

int main()
{
printf("hello micu");
return 0;
}

#include <stdio.h>

int main()
{
printf("hello micu");
printf("hello may");
return 0;
}

换行的操做:每一行不能拆分,用\n换行

#include <stdio.h>

int main()
{
printf("hello micu\n");
printf("hello may\n");
return 0;
}

#include <stdio.h>

int main()
{
printf("hello micu\\n");
printf("hello may\\n");
return 0;
}

变量和赋值

整数 例 int a 小数 例 float b

变量先声明 再使用

变量的运算

加减乘除取余

±*/%

用C语言来解决常见问题

通过长宽高计算长方形的体积

#include <stdio.h>
int lenth=2;
int width=3;
int high=4;
int volum;
int main()
{
volum = lenth * width * high;
printf("体积=%d",volum);
return 0;
}

#include <stdio.h>
int lenth=2;
int width=3;
int high=4;
int volum;
int main()
{
volum = lenth * width * high;
printf("体积=%d你好米醋",volum);
return 0;
}

占位符的作用是占一个位置

int类型占位符是%d

读入用户输入内容赋值给变量

#include <stdio.h>
int lenth;
int width;
int high;
int volum;
int main()
{
scanf_s(" %d", &lenth);
scanf_s(" %d", &width);
scanf_s(" %d", &high);
volum = lenth * width * high;
printf("体积=%d",volum);
return 0;
}

#include <stdio.h>
int lenth=1;
int width=2;
int high=3;
int volum;
int main()
{
volum = lenth * width * high;
scanf_s(" %d", &lenth);
scanf_s(" %d", &width);
scanf_s(" %d", &high);
printf("体积=%d",volum);
return 0;
}

两个代码结果不同,因为C是一行接一行编译

完善代码

#include <stdio.h>
int lenth=1;
int width=2;
int high=3;
int volum;
int main()
{
printf("请输入物体的长:");
scanf_s(" %d", &lenth);
printf("请输入物体的宽:");
scanf_s(" %d", &width);
printf("请输入物体的高:");
scanf_s(" %d", &high);
volum = lenth * width * high;
printf("体积:%d",volum);
return 0;
}

![image-20260118201510628](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118201510628.png?lastModify=1768746294)

添加注释

目的是让别人能看懂,但不执行语句

注释的类型

1.//注释//

2./ 注释 /

例如 ![image-20260118202528346](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118202528346.png?lastModify=1768746294)

整体代码结构分析

![image-20260118211727097](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118211727097.png?lastModify=1768746294)

预处理指令

#include 包含

#define 宏定义 例 #define PI 3.14f

m=3.14frr;等价m=PIrr;

变量声明

int 整数 float 小数

变量命名 包含英文字母 数字 下划线 temputure_2

不能以数字开头 不能以关键字开头

不能重复定义

变量区分大小写

可以一次性定义多个变量

int a,b,c; int a=1,b=2,c=3; int a=1,b=2,c;

格式化输入/输出

printf(“格式串”,参数1,参数2,参数3…);

#include <stdio.h>//预处理指令
#include<stdlib.h>
/*变量声明区*/
int a = 3;
float b = 3.14;
int main()//主函数区 main有且只有一个
{
printf("a=%d\nb=%f", a, b);
return 0;//返回
}

![image-20260118214138144](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118214138144.png?lastModify=1768746294)

#include <stdio.h>//预处理指令
#include<stdlib.h>
/*变量声明区*/
int a = 3;
float b = 3.14;
int main()//主函数区 main有且只有一个
{
printf("a=%9.5d\nb=%9.2f", a, b);
return 0;//返回
}

![image-20260118214538432](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118214538432.png?lastModify=1768746294)

其中 9代表显示宽度为9,.5,.2表示精度有几位,不足就补0

![image-20260118215424413](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118215424413.png?lastModify=1768746294)

变量比占位符多,没影响

![image-20260118215828829](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118215828829.png?lastModify=1768746294)

占位符比变量多,多的生成随机数

![image-20260118220236352](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118220236352.png?lastModify=1768746294)

数字类型不对,生成错的数

![image-20260118221021323](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118221021323.png?lastModify=1768746294)

![image-20260118221119627](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118221119627.png?lastModify=1768746294)

scanf输入格式要对应,否则出错

不改变变量的值直接调用,改变变量的值要取地址

随堂练习

1:

#include <stdio.h>
#define PI 3.14
float r1;
float V1;
int main()//主函数区 main有且只有一个
{
``

printf("请输入圆的半径:" );
scanf_s("%f", &r1);
​
V1 = 4/3*PI * r1 * r1*r1;
​
printf("圆的体积是:%f\n%f\n", V1);
return 0;//返回

}

![image-20260118225510066](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118225510066.png?lastModify=1768748097)

2:

#include <stdio.h>
float r1;
float r2;
float PI = 3.14;
float S1;
float S2;
int main()//主函数区 main有且只有一个
{
``

printf("请分别输入两个圆的半径:" );
scanf_s("%f%f", &r1, &r2);
​
S1 = 4/3*PI * r1 * r1*r1;
S2 = 4 / 3 * PI * r2 * r2 * r2;
printf("圆的面积是:%f\n%f\n", S1,S2);
return 0;//返回

}

![image-20260118224339537](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118224339537.png?lastModify=1768747882)

3:

![image-20260118225125281](file:///C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20260118225125281.png?lastModify=1768747882)

//更正:随堂练习题第二三题体积误打成面积