相信我们在写程序的时候一定会遇到很多的函数,面对不同的函数的时候就要遇到不同的头文件。有时候我们往往因为自己忘记或者不知道头文件所对应的函数而感到苦恼,那么这篇文章相信一定可以帮到你。

 

1.<stdio.h>

这个头文件最常用,一般在使用scanf和printf的时候都需要用到否则就会报错未找到相应的库函数。示例如下:

2.<string.h>

这个头文件的使用也很常见。一般用于引用对字符串进行操作的函数。如:

strlen该函数用于检测字符串长度。

strlen函数在使用的时候需要在括号里放入想要检验长度的字符串,可以是事先创造好的字符型数组也可以是直接用双引号引出的字符串。他会直接返回一个整数
,这个整数就是字符串的长度。
#include<stdio.h> #include<string.h> int main() { char ch[] = "abcdef"; int
ret=strlen(ch); printf("%d", ret); }

 strcmp函数用于判断两个字符串是否相等。

strcmp函数括号里放的是两组字符串,当两组字符串相等的时候会返回0;当两个整数不相等时会返回非0的数字(由ASCII码值决定);
#include<stdio.h> #include<string.h> int main() { char ch[20]; scanf("%s",
ch); if (strcmp(ch, "abcd") == 0) { printf("左右字符串相等\n"); } else {
printf("不相等"); } return 0; }

 strcpy函数用于将B中字符串放到A中。

strcpy函数括号中放入的同样是两个字符串,但是不同的是这两个字符串有顺序。作为容器的字符串需要放到前面,strcpy(A,B)。
#include<stdio.h> #include<string.h> int main() { char ch[20] = "abcdef"; char
alt[20] = "ghijk"; printf("%s\n", ch); printf("%s\n", alt); strcpy(ch, alt);
printf("%s\n", ch); printf("%s\n", alt); return 0; }

strcat函数作用是将A字符串中的内容与B中内容联通。(实质是指针上的连接)

strcat函数括号中放的两个字符串前一个字符串的内容直接可以承接后一个字符串上。
#include<stdio.h> #include<string.h> int main() { char ch[20] = "abcde"; char
alt[10] = "fghij"; printf("%s", strcat(ch, alt)); return 0; }

 strstr函数作用是判断B是否为A的子字符串如果是则返回B在A中的首地址,若不是返回NULL

strstr函数中两个字符串前面的字符串A范围比较大,后面的字符串B为A字符串的子集。

#include<stdio.h> #include<string.h> int main() { char ch[20] = "abcdefg";
char alt[10] = "defg"; printf("%p", strstr(ch, alt)); return 0; }

 3.<Windows.h>

该函数为系统函数,常用的有system函数和Sleep函数。system函数又有很多种变形。

Sleep函数常用于系统休眠。

Sleep函数经常和cls函数一起使用。在清屏前可以使系统休眠,是的内容可以看清。括号里的内容是休眠的时间,单位是毫秒。
#include<stdio.h> #include<Windows.h> int main() { printf("好好学习,天天向上");
Sleep(2000); system("cls"); printf("加油"); return 0; }

 system函数

 该函数为<Windows.h>头文件下的主要一类,比较常用也比较总要,那么我们将其单独列出来认识一下。

1.system(“cls”)清屏操作,上面已经举过例子不再重复。

2.system(“color”)用于更改控制板背景以及字体颜色。
#include<stdio.h> #include<Windows.h> int main() { printf("好好学习,天天向上");
system("color F3"); return 0; }

 3.system("shutdown -s -t 60")关机程序,用于系统定时关机。

   system(“shutdown -a”)取消关机。经常配套使用。
#include<stdio.h> #include<windows.h> int main() { printf("承认吧你和猪一样笨!\n");
printf("输入我和猪一样,不然的话你的电脑将在1分钟之内关机\n"); system("shutdown -s -t 60"); char
ch[20]; while (1) { scanf("%s", ch); if (0 == strcmp(ch, "我和猪一样")) {
printf("乖乖,我这就给你取消关机\n"); system("shutdown -a"); } else {
printf("不乖哦,快点小笨蛋\n"); } } return 0; }

 4.<stdlib.h>和<time.h>

rand函数和srand函数 以及time函数

rand函数用于生成随机数,经常和srand函数一起使用。srand函数用于生成一个随机初始化数值。在使用的时候需要引用一个时间戳来使srand随机生成的数值都各不相同。

time函数括号里需要传于一个指针,在这里我们用空指针我们不需要向其中传入具体的指针内容,所以我们传入一个空指针NULL进行替代。其返回值需要强制类型转化为unsigned
int类型。
#include<stdio.h> #include<stdlib.h> #include<time.h> int main() {
srand((unsigned int)time(NULL)); int i = 0; for (i = 0; i < 20; i++) {
printf("%d ", rand() % 100 + 1); } return 0; }

5.<math.h> 

math头文件下方有许多分支,不同的函数可以起到不同的作用,比如说开平方,求绝对值,向上或向下取整,以及求次方等。我们来分别认识一下他们,并了解一下他们的作用。

 1.fabs函数

这个函数的作用是求绝对值,我们可以根据需求进行使用并判断。在其括号里放入的是一个任意类型的数字,正数求得就是它本身,负数求得的是相反数。
#include<stdio.h> #include<math.h> int main() { int i = -1; int n = 2;
printf("%.0f\n", fabs(-1)); printf("%.0f", fabs(-8)); return 0; }

 2.floor函数和ceil函数

这两个函数分别用于向上取整和向下取整,即向上忽略小数部分得到整数部分,向下忽略小数部分整数部分加一。
#include<stdio.h> #include<math.h> int main() { printf("%.0f\n", floor(4.8));
//向上取整 printf("%.0f", ceil(4.2)); //向下取整 return 0; }

 3.pow函数

这个函数用于求次方,需要向函数的括号中传入两个参数(x,y)。x为底数,y为指数进行次方操作。

#include<stdio.h> #include<math.h> int main() { printf("%.0f\n", pow(2, 3));
printf("%f\n",pow(2.3, 2.1)); return 0; }

4.sqrt函数

这个函数的作用是对一个数字进行开平方,也就是求一个数字的算术平方根。
#include<stdio.h> #include<math.h> int main() { printf("%.0f\n", sqrt(4));
printf("%f", sqrt(5.2)); return 0; }

 5.round函数

这个函数就是我们所熟悉的四舍五入操作,我们在适当的时候使用可以简化不少我们的操作。
#include<stdio.h> #include<math.h> int main() { printf("%.0f\n", round(4.21));
printf("%.0f\n", round(4.67)); return 0; }

 以上就是初识C语言最经常用的函数,希望可以帮到大家,祝大家天天开心。

技术
今日推荐
PPT
阅读数 115
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信