一、<stdio.h>
这个是通常用到的,提供了输入输出的函数
printf():用于格式化输出
scanf():通过键盘给变量赋值
fgets():用于从文件流中读取字符串。
//使用fgets读取一行
if(fgets(line,sizeof(line),file)!=NULL){
printf("%s\n",line);
else{
printf("Failed\n");
}
fputs():将一个字符串写入文件流中,文件流可以是磁盘文件、网络连接、标准输出等。
int fputs(const char *str,FILE *stream);
//fputs函数的声明
//str:要写入的字符串要以null字符('\o')
//stream:一个指向FILE对象的指针,该对象标识目标文件流通常用fopen函数打开并获取FILE指针。
#include <stdio.h>
int main() {
FILE *fp;
fp=fopen("hello.txt","w"); //打开文件用于写入
if(fp==NULL){
perror("Error"); //用于打印与最近的错误的代码相关的错误的信息
return 1;
}
fputs("hello\n",fp); //将字符串写入文件
fclose(fp); //关闭文件
return 0;
}
getchar():只能读取一个字符
# include <stdio.h>
int main(){
int ch;
ch=getchar(); //读取一个字符
printf("%c",ch) ; //输出读取的字符
return 0;
}
因为只能读取一个字符,所以输出的只有"h".
puts():用于向标准输出设备屏幕输出字符串并且自动在字符串的末尾添加一个换行符
# include <stdio.h>
int main(){
char str[]="good,morning";
puts(str);
return 0;
}
gets():输入一行字符串,以回车结束。
# include <stdio.h>
int main(){
char arr[50];
printf("请输入文本:");
gets(arr) ;
printf("%s\n",arr);
return 0;
}
二、<stdlib.h>
包含了内存分配、程序控制、转换函数等
malloc() :用于动态分配内存,允许程序在运行时请求一块内存。
realloc():允许程序改变已经分配好的内存大小
# include<stdio.h>
# include <stdlib.h>
int main(){
int *ch;
ch=(int*)malloc(sizeof(int)); //分配内存 malloc
if(ch==NULL){ //检验是否成功
printf("fail");
return 1;
}
*ch=35; //使用内存
printf("%d\n",*ch);
ch=(int*)realloc(ch, 4*sizeof(int)); //增加内存大小 realloc
if(ch==NULL){
printf("fail");
return 1;
}
ch[1]=678;
printf("%d\n",ch[1]);
ch[2]=123;
printf("%d\n",ch[2]);
free(ch); //free()是stdlib.h头文件的函数,用于释放内存
return 0;
}
calloc():用于动态内存分配,通常用于分配一个数组
int *ary=(int *)calloc(10,sizeof(int));
//分配10个整数的空间
……
for(int i=0;i<10;i++){
ary[i]=i+1;
}
//使用ary指向的内存
free(ary); //释放内存
system():用于执行windows系统或Linux/unix系统命令
system("cls") | 清空屏幕 |
system("pause") | 暂停 |
system("mode con:clos=100 lines=10") | 设置窗口的大小 |
system("color 1A") | 1为背景色,A为前景色 |
还有很多命令是通过system()函数实现的,我这里只是简单举了几个例子
atoi():将字符串转化为整数
char arr[]="1890";
int num=atoi(arr);
需要注意的是atoi不会处理字符串中的逗号与空格,如果字符串不以数字或正负号开头,将返回0
rand():用来生成一个伪随机数的函数
#include<stdio.h>
#include<stdlib.h>
int main(){
for(int i=0;i<10;i++){
printf("%d ",rand()); //生成并打印10个随机数
}
return 0;
}
exit():用来终止当前进程并退出程序的函数
三、<string.h>
strlen():用于计算一个字符串的长度(字符的数目)
# include<stdio.h>
#include<string.h>
int main(){
const char *lan="good morning";
char arr[100]={'A','B','C'};
size_t num=strlen(lan);
int sum=strlen(arr);
printf("%d %d",num,sum);
return 0;
}
strlen()计算字符串中字符的数量,包括空格,但不包含字符串末尾的'\0'.
strcpy():是一个字符串复制函数
# include<stdio.h>
# include<string.h>
int main(){
const char *arr="hello,world";
char dest[100];
strcpy(dest,arr);
printf("%s\n",dest);
return 0;
}
strcat():用于连接两个字符串
# include<stdio.h>
# include<string.h>
int main(){
char arr[20]="hello";
char brr[12]="world";
strcat(arr,brr);
printf("%s\n",arr);
return 0;
}
strcmp():用于字符串的比较
# include<stdio.h>
# include<string.h>
int main(){
char arr[20]="hello";
char brr[12]="world";
int result=strcmp(arr,brr);
printf("%d",result);
return 0;
}
这里的-1是指第一个字符串小于第二个字符串
当第一个字符串大于第二个字符串是返回1
相等时返回0(如何比较大小:通过ASCLL码进行比较)
四、<math.h>
sin(x)、cos(x) tan(x) asin(x) acos(x) atan(x) atan2(x,y):三角函数、反三角函数
sinh()、cosh()、tanh() asinh() acosh() atanh():双曲函数、反双曲函数
#include<stdio.h>
#include<math.h>
int main() {
double pi=3.1415926;
printf("%f\n",sin(pi/4.0));
printf("%f\n",acos(1));
printf("%f\n",asinh(2));
printf("%f\n",sinh(1.2));
return 0;
}
exp(x):计算e^x
ldexp(x,n):计算x*2^n
log():计算ln(x),而log10()则是计算以10为底的对数,x>0;log2()计算以2为底的对数
expm1():计算e的x次减一
#include<stdio.h>
#include<math.h>
int main(){
printf("%f\n",exp(5));
printf("%f\n",log(19));
printf("%f\n",ldexp(3,7));
return 0;
}
pow(x,y):计算x^y
sqrt(x):计算x的平方根
cbrt():计算x的立方根
hypot(x,y):计算(x^2+y^2)的平方根,用于求三角的斜边
fabs():计算x(double、float)的绝对值
abs():计算x(int )的绝对值
#include<stdio.h>
#include<math.h>
int main(){
printf("%f\n",pow(2,6));
printf("%f\n",sqrt(4));
printf("%f\n",hypot(12,5));
printf("%f\n",fabs(-3.14));
printf("%d\n",abs(-89));
return 0;
}
此外<time.h> :时间函数; <ctype.h> :字符处理函数; <windows.h>:wondows API头文件,用于windows运用程序。