Initial commit: C language learning code

This commit is contained in:
2025-07-20 16:30:56 +08:00
commit 06e24173a6
139 changed files with 9303 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
/*
函数定义:函数是一段代码的封装,可以重复使用
函数的声明:告诉编译器函数的名字,函数的参数,返回值类型
函数的调用:使用函数名调用函数,并传入参数
*/
:
/**
* @brief 计算从begin到end之间所有整数的和
* @param begin 起始数(包含)
* @param end 结束数(包含)
* @return 返回区间内所有整数的累加和
* @note begin必须小于等于end
*/
int num_sum(int begin, int end)
/*
函数的头void num_sum(int begin, int end)
int是返回值类型,int需要返回一个值,但void不需要返回值
num_sum是函数名
int是参数类型
beginend是参数名
*/
{
// 函数体,即函数要执行的代码
int sum = 0;
for (int i = begin; i <= end; i++)
{
sum += i;
}
return sum; // 函数的返回值用return返回
}
int main()
{
printf("%d\n", num_sum(1, 10));
printf("%d\n", num_sum(20, 30));
printf("%d\n", num_sum(35, 45));
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
// !函数原型声明
int num_sum(int begin, int end);
int main()
{
printf("%d\n", num_sum(1, 10));
printf("%d\n", num_sum(20, 30));
printf("%d\n", num_sum(35, 45));
return 0;
}
// !函数定义
int num_sum(int begin, int end)
{
int sum = 0;
for (int i = begin; i <= end; i++)
{
sum += i;
}
return sum;
}
+26
View File
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
int jiecheng(int n);
int main()
{
int n;
printf("请输入一个整数:");
scanf("%d", &n);
printf("%d\n", jiecheng(n));
return 0;
}
int jiecheng(int n)
{
int sum = 1;
if (n == 1)
return sum;
else
sum = n * jiecheng(n-1);
}
+25
View File
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
int num_sum(int a, int b) // 函数定义
{
int sum = 0;
for (int i = a; i <= b;i++)
{
sum += i;
}
return sum;
}
int main()
{
int a, b;
printf("请输入两个整数:");
scanf("%d %d", &a, &b);
printf("%d和%d之间的所有数的和:sum = %d\n", a, b, num_sum(a, b));
return 0;
}
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
/*
void main(void);明确函数无返回值,即()内为空,没有参数
void main();明确函数无返回值,但()内可以有参数
*/
/*
C语言不允许函数的嵌套定义
*/
/*
int main(void)是一个函数,所以会有return 0;作为函数的返回值
*/
int main()
{
return 0;
}
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
/*
函数定义:函数是一段代码的封装,可以重复使用
函数的声明:告诉编译器函数的名字,函数的参数,返回值类型
函数的调用:使用函数名调用函数,并传入参数
*/
void num_sum(int begin, int end)
/*
函数的头void num_sum(int begin, int end)
void是返回值类型
num_sum是函数名
int是参数类型
beginend是参数名
*/
{
// 函数体,即函数要执行的代码
int sum = 0;
for (int i = begin; i <= end; i++)
{
sum += i;
}
printf("%d到%d的和为%d\n", begin, end, sum);
}
int main()
{
num_sum(1, 10);
num_sum(20, 30);
num_sum(35, 45);
return 0;
}
+34
View File
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
void swap(int a, int b); // !形参
int main()
{
// !参数传递
/*
* 1.值传递:可以理解为拷贝,C语言中所有的参数传递都是值传递,即把实参的值拷贝一份传递给形参
* 2.地址传递:
* 3.引用传递
* 4.每个函数有自己变量空间,参数也位于这个独立的空间中,和其它函数没有关系(函数调用时,形参和实参是不同的变量,形参变量在栈中分配内存,实参变量在栈中分配内存,形参和实参是不同的变量,它们之间是相互独立的,没有关系,改变形参的值并不会影响实参的值,改变实参的值也不会影响形参的值)
* 5.过去,对于函数参数表中的参数,叫做“形式参数”,调用函数时给的值,叫做“实际参数”。现在,把“形式参数”称为“参数”,“实际参数”称为“值”
*/
int a = 5;
int b = 6;
swap(a, b); // !实参
printf("%d %d\n", a, b);
return 0;
}
void swap(int a, int b) // !形参
{
int temp = a;
a = b;
b = temp;
}
+30
View File
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
// !本地变量(也叫局部变量)
/*
定义:所有在函数内定义的变量都是本地变量
生存期:变量从定义开始,到函数结束
作用域:在什么范围内可以访问这个变量(这个变量可以起作用)
对于本地变量,范围是函数大括号{}内
特点:
1.不同函数中的同名变量互不干扰
2.外层变量会被内层同名变量暂时隐藏(变量覆盖)
3.未初始化时值为随机数(栈内存特性)
*/
int main()
{
// 例子
int a = 10;
{
int a = 20;
printf("a = %d\n", a); // 输出20,内层变量a覆盖了外层变量a
}
printf("a = %d\n", a); // 输出10,内层变量a被隐藏了
return 0;
}