C语言不支持函数重载
一、函数的使用方法
1. 直接使用
(1)定义的函数在main函数 之上
#include<stdio.h> int sum(int num1, int num2){int res = 0;res = num1 + num2;return res;
}int main(){int res = sum(2, 3);printf("%d", res);return 0;
}
(2) 定义的函数在main函数之下
#include<stdio.h> int sum(int num1, int num2);int main(){int res = sum(2, 3);printf("%d", res);return 0;
}int sum(int num1, int num2){int res = 0;res = num1 + num2;return res;
}
2. 头文件
(1)头文件是扩展名为 .h 的文件,包含了 C 函数声明和宏定义,被多个源文件中引用共享;(2)头文件只能声明函数,不能定义函数;(3)头文件名和源文件名保持一致;(4)C语言中include<> 引用的是编译器的类库路径里的头文件,用于引用系统头文件,include " " 引用的是程序目录的相对路径中的头文件,如果在程序目录没有找到引用的头文件则到编译器的类库路径的目录下找该头文件,找不到则报错,用于引用用户头文件;(5)建议把所有的常量、系统全局变量和函数原型写在头文件中,在需要的时候随时引用这些头文件;(6)一个 .h 对应的 .c 文件中又可以引用其他 .h 文件。
使用示例:
① 创建一个自定义函数的头文件 function.h
int sum(int num1, int num2);
② 创建自定义函数头文件 function.h 所对应的 C文件 function.c
int sum(int num1, int num2){int res = 0;res = num1 + num2;return res;
}
③ 在调用自定义函数的主文件 master.c 中引入头文件
#include<stdio.h>
#include "function.h" // 引入头文件int main(){int res = sum(2, 3);printf("%d", res);return 0;
}
注意:由于引入了自定义函数的头文件,在运行时需要链接到该头文件对应的C文件才能成功调用到该函数,命令为:gcc master.c function.c -o master.exe
二、函数调用
函数调用的规则:(1)当调用(执行)一个函数时,就会开辟一个独立的空间(栈);(2)每个栈空间是相互独立;(3)函数栈中的内容都会被完整的执行;(4)当调用的函数栈全部执行完毕后,栈空间销毁,会返回到调用该函数的位置,继续往后执行
// 例子一:最终输出是:2 3 4
#include<stdio.h> void test(int num){if(num > 2){test(num - 1);}printf("%d ", num);
}int main(){test(4);return 0;
}
// 例子二:最终输出是:2
#include<stdio.h> void test(int num){if(num > 2){test(num - 1);}else{printf("%d ", num); }}int main(){test(4);return 0;
}
三、函数变量传递
1. 值传递
#include<stdio.h> void add(int num){num++;
}int main(){int n = 3;add(n);printf("n = %d", n); // n = 3return 0;
}
2. 指针地址传递
#include<stdio.h> void add(int *num){ // *num = &n :num 等于变量 n 的地址(*num)++; // *num 指向变量 n 的值
}int main(){int n = 3;add(&n);printf("n = %d", n); // n = 4return 0;
}
#include<stdio.h> void swap(int *num1, int *num2){int temp = *num1; // 将num1这个指针指向的变量的值赋值给temp*num1 = *num2; // 将num2这个指针指向的变量的值赋值给num1这个指针指向的变量*num2 = temp; // 将temp的值赋值给num2这个指针指向的变量
}int main(){int n1 = 2;int n2 = 3;swap(&n1, &n2);printf("n1 = %d, n2 = %d", n1, n2); // n1 = 3, n2 = 2return 0;
}
四、全局变量和局部变量
局部变量:(1)局部变量存放在栈中,函数被调用时才动态地为变量分配存储单元,它的作用域仅限于函数内,局部变量如果初始化时不赋值则没有默认值;(2)局部变量被 static 修饰后,称为静态局部变量,静态局部变量存放在静态存储区(全局性质的区域),如果初始化时不赋值会有默认值,静态局部变量只会被初始化一次;
#include<stdio.h> void test1(void){int num = 1;num++;printf("num = %d \n", num);
}void test2(void){static int num = 1;num++;printf("num = %d \n", num);
}int main(){test1(); // num = 2test1(); // num = 2test2(); // num = 2test2(); // num = 3return 0;
}
全局变量:(1)全局变量和静态变量存放在静态存储区,它的作用域默认是整个程序,全局变量如果初始化时不赋值,有默认值
malloc动态分配的数据存放在堆中
五、常用的系统函数
1. 字符串函数
引入头文件 <string.h>
#include<stdio.h>
#include<string.h>int main(){char temp[100]; // 定义字符数组char dest[100] = "ABC";// strlen(): 统计字符串的大小char *str = "123";printf("字符串str的长度 = %d \n", strlen(str)); // 字符串str的长度 = 3// strcopy: 字符串拷贝strcpy(temp, str); // 将str拷贝到temp中printf("%s \n", temp); // 123strcpy(dest, str); // 将str拷贝到dest中,会覆盖原本的值printf("%s \n", dest); // 123 // strcat: 字符串连接strcat(temp, str); // 将str追加到temp中printf("%s \n", temp); // 123123strcat(temp, dest); // 将dest追加到temp中printf("%s \n", temp); // 123123123return 0;
}
2. 时间函数
引入头文件 <time.h>
#include<stdio.h>
#include<time.h>void test(void){int sum = 0;for(int i = 0; i < 1000000000; i++){sum += i;}
}int main(){//char *ctime(const time_t *timer):获取当前时间time_t curtime;time(&curtime); // time()进行初始化printf("当前时间:%s \n", ctime(&curtime));// double difftime(time_t time1, time_t time2):返回time1和time2之间的差,用于统计某段代码的执行时间(秒)time_t start, end;double test_time;time(&start); // 初始化得到此时的时间test(); // 执行time(&end); // 初始化得到此时的时间test_time = difftime(end, start);printf("执行时间:%lf \n", test_time);return 0;
}
3. 数学函数
引入头文件 <math.h>
#include<stdio.h>
#include<math.h>int main(){// double exp(double x) : e 的 x 次幂// double log(double x) :x 以 e 为底的对数// double pow(double x, double y) :x 的 y 次幂// double sqrt(double x) :x 的平方根// double fabs(double x) :x 的绝对值return 0;
}
4. 基本数据类型和字符串转换
(1)基本数据类型 转 字符串
引入头文件 <stdio.h>
#include<stdio.h> // 基本数据类型 转 字符串类型 :sprintf
int main(){char str1[100];char str2[100];char str3[100];int a = 1234;double b = 3.1415;sprintf(str1, "%d", a);sprintf(str2, "%lf", b); // 默认是6位小数sprintf(str3, "%d %lf", a, b);printf("str1 = %s, str2 = %s, str3 = %s", str1, str2, str3); // str1 = 1234, str2 = 3.141500, str3 = 1234 3.141500return 0;
}
(2)字符串 转 基本数据类型
引入头文件 <stdlib.h>
#include<stdio.h>
#include<stdlib.h>// 基本数据类型 转 字符串类型 :sprintf
int main(){char str1[10] = "12345";char str2[10] = "3.1415";char str3[10] = "A";int a = atoi(str1);short b = atoi(str1);long c = atol(str1);double d = atof(str2);char e = str3[0];printf("a = %d, b = %d, c = %ld, d = %f, e = %c", a, b, c, d, e); // a = 12345, b = 12345, c = 12345, d = 3.141500, e = Areturn 0;
}