二级C常用函数总结_第1页
二级C常用函数总结_第2页
二级C常用函数总结_第3页
二级C常用函数总结_第4页
二级C常用函数总结_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、*数学相关*1、函数名称: abs函数原型: int abs(int x);函数功能: 求整数x的绝对值函数返回: 计算结果参数说明:所属文件: <math.h>,<stdlib.h>使用范例:#include <stdio.h>#include <math.h>int main()int number=-1234;printf("number: %d absolute value: %d",number,abs(number);return 0;2、函数名称: fabs函数原型: double fabs(double x);

2、函数功能: 求x的绝对值.函数返回: 计算结果参数说明:所属文件: <math.h>使用范例:#include <stdio.h>#include <math.h>int main()float number=-1234.0;printf("number: %f absolute value: %f",number,fabs(number);return 0;3、函数名称: sqrt函数原型: double sqrt(double x);函数功能: 计算x的开平方.函数返回: 计算结果参数说明: x>=0所属文件: <math

3、.h>使用范例:#include <math.h>#include <stdio.h>int main()double x=4.0,result;result=sqrt(x);printf("The square root of %lf is %lf",x,result);return 0;4、函数名称: pow函数原型: double pow(double x,double y);函数功能: 计算以x为底数的y次幂,即计算xy的值.函数返回: 计算结果参数说明: x-底数,y-幂数所属文件: <math.h>使用范例:#inclu

4、de <math.h>#include <stdio.h>int main()double x=2.0,y=3.0;printf("%lf raised to %lf is %lf",x,y,pow(x,y);return 0;5、函数名称: sin函数原型: double sin(double x);函数功能: 计算sinx的值.正弦函数函数返回: 计算结果参数说明: 单位为弧度所属文件: <math.h>使用范例:#include <stdio.h>#include <math.h>int main()doub

5、le result,x=0.5;result=sin(x);printf("The sin() of %lf is %lf",x,result);return 0; 6、函数名称: cos函数原型: double cos(double x);函数功能: 计算cos(x)的值.余弦函数.函数返回: 计算结果参数说明: x的单位为弧度所属文件: <math.h>使用范例:#include <stdio.h>#include <math.h>int main()double result;double x=0.5;result=cos(x);p

6、rintf("The cosine of %lf is %lf",x,result);return 0; 7、函数名称: tan函数原型: double tan(double x);函数功能: 计算tan(x)的值,即计算角度x的正切数值函数返回: 计算结果参数说明: x>=0单位为弧度所属文件: <math.h>使用范例:#include <stdio.h>#include <math.h>int main()double result,x;x=0.5;result=tan(x);printf("The tan of %

7、lf is %lf",x,result);return 0;8、 函数名称: rand函数原型: int rand(void);函数功能: 产生0到32767间的随机整数(0到0x7fff之间)函数返回: 随机整数参数说明:所属文件: <stdlib.h>#include <stdlib.h>#include <stdio.h>int main()int i;printf("Ten random numbers from 0 to 99");for(i=0;i<10;i+)printf("%d",ran

8、d()%100);return 0;函数名称: log函数原型: double log(double x);函数功能: 求logeX(e指的是以e为底),即计算x的自然对数(ln X)函数返回: 计算结果参数说明:所属文件: <math.h>使用范例:#include <math.h>#include <stdio.h>int main()double result;double x=8.6872;result=log(x);printf("The natural log of %lf is %lf",x,result);return 0

9、; 函数名称: log10函数原型: double log10(double x);函数功能: 求log10x(10指的是以10为底).计算x的常用对数函数返回: 计算结果参数说明:所属文件: <math.h>使用范例:#include <math.h>#include <stdio.h>int main()double result;double x=800.6872;result=log10(x);printf("The common log of %lf is %lf",x,result);return 0;*字符串相关*1、 函数

10、名称: strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把str2指向的字符串拷贝到str1中去函数返回: 返回str1,即指向str1的指针参数说明:所属文件: <string.h>#include <stdio.h>#include <string.h>int main()char string10;char *str1="abcdefghi"strcpy(string,str1);printf("the string is:%sn",string);re

11、turn 0;2、 函数名称: strcat函数原型: char* strcat(char * str1,char * str2);函数功能: 把字符串str2接到str1后面,str1最后的'0'被取消函数返回: str1参数说明:所属文件: <string.h>#include <stdio.h>#include <string.h>int main()char buffer80;strcpy(buffer,"Hello ");strcat(buffer,"world");printf("

12、%sn",buffer);return 0;3、 函数名称: strcmp函数原型: int strcmp(char * str1,char * str2);函数功能: 比较两个字符串str1,str2.函数返回: str1<str2,返回负数; str1=str2,返回 0; str1>str2,返回正数. 参数说明:所属文件: <string.h>#include <string.h>#include <stdio.h>int main()char *buf1="aaa", *buf2="bbb&quo

13、t;, *buf3="ccc"int ptr;ptr=strcmp(buf2, buf1);if(ptr>0)printf("buffer 2 is greater than buffer 1n");elseprintf("buffer 2 is less than buffer 1n");ptr=strcmp(buf2, buf3);if(ptr>0)printf("buffer 2 is greater than buffer 3n");elseprintf("buffer 2 is le

14、ss than buffer 3n");return 0;4、 函数名称: strlen函数原型: unsigned int strlen(char * str);函数功能: 统计字符串str中字符的个数(不包括终止符'0')函数返回: 返回字符串的长度.参数说明:所属文件: <string.h>#include <stdio.h>#include<string.h>int main()char str="how are you!"printf("the lence is:%dn",strle

15、n(str);return 0;*文件相关*1、 函数名称: fopen函数原型: FILE *fopen(char * filename,char * mode);函数功能: 以mode指定的方式打开名为filename的文件函数返回: 成功,返回一个文件指针(文件信息区的起始地址),否则返回0参数说明: filename-文件名称,mode-打开模式:r 只读方式打开一个文本文件rb 只读方式打开一个二进制文件w 只写方式打开一个文本文件wb 只写方式打开一个二进制文件a 追加方式打开一个文本文件ab 追加方式打开一个二进制文件r+ 可读可写方式打开一个文本文件rb+ 可读可写方式打开一个

16、二进制文件w+ 可读可写方式创建一个文本文件wb+ 可读可写方式生成一个二进制文件a+ 可读可写追加方式打开一个文本文件ab+ 可读可写方式追加一个二进制文件2、 函数名称: fclose函数原型: int fclose(FILE * fp);函数功能: 关闭fp所指的文件,释放文件缓冲区函数返回: 0-无错,否则非零参数说明:所属文件: <stdio.h>3、 函数名称: fgetc函数原型: int fgetc(FILE * fp);函数功能: 从fp所指定的文件中取得下一个字符函数返回: 返回所得到的字符.若读入出错,返回EOF参数说明: fp-文件指针所属文件: <s

17、tdio.h>4、 函数名称: fgets函数原型: char fgets(char * buf,int n,FILE * fp);函数功能: 从fp指向的文件中读取一个长度为(n-1)的字符串,存入起始地址为buf的空间函数返回: 返回地址buf,若遇文件结束或出错,返回NULL函数说明: buf-存放读入的字符数组指针,n-最大允许的读入字符数,fp-文件指针所属文件: <stdio.h>5、 函数名称: feof函数原型: int feof(FILE * fp);函数功能: 检查文件是否结束.函数返回: 遇文件结束符返回非零值,否则返回0参数说明: fp-文件指针所属文

18、件: <stdio.h>6、 函数名称: fputc函数原型: int fputc(char ch,FILE *fp);函数功能: 将字符ch输出到fp指向的文件中函数返回: 成功,则返回该字符;否则返回非0参数说明: fp-文件指针,ch-要写入的字符(舍去高位字节)所属文件: <stdio.h>7、 函数名称: fputs函数原型: int fputs(char * str,FILE *fp);函数功能: 将str指向的字符串输出到fp指向的文件中函数返回: 成功,则返回0;否则返回非0参数说明:所属文件: <stdio.h>8、 函数名称: fprin

19、tf函数原型: int fprintf(FILE * fp,char * format,args,.);函数功能: 把args的值以format指定的格式输出到fp所指定的流式文件中函数返回: 实际输出的字符数参数说明: fp-目标文件,format-格式符所属文件: <stdio.h>9、 函数名称: fscanf函数原型: int fscanf(FILE * fp,char format,args,.);函数功能: 从fp所指定的文件中按format给定的格式将数据输送到args所指向的内存单元函数返回: 已输入的数据个数参数说明:所属文件: <stdio.h>10

20、、 函数名称: fseek函数原型: int fseek(FILE * fp,long offset,int base);函数功能: 将fp所指文件的位置指针移到以base所指位置为基准,以offset为位移量的位置函数返回: 返回当前位置,否则返回-1参数说明: fp-文件指针offset-相对于origin规定的偏移位置量origin-指针移动的起始位置,可设置为以下三种情况:SEEK_SET 文件开始位置 0SEEK_CUR 文件当前位置 1SEEK_END 文件结束位置 2所属文件: <stdio.h>#include <stdio.h>long filesiz

21、e(FILE *stream);int main()FILE *stream;stream=fopen("MYFILE.TXT","w+");fprintf(stream,"This is a test");printf("Filesize of MYFILE.TXT is %ld bytes",filesize(stream);fclose(stream);return 0;long filesize(FILE *stream)long curpos,length;curpos=ftell(stream);fse

22、ek(stream,0L,SEEK_END);length=ftell(stream);fseek(stream,curpos,SEEK_SET);return length;11、 函数名称: ftell函数原型: long ftell(FILE * fp);函数功能: 得到文件位置指示器的数值函数返回: fp指向的文件中的读写位置参数说明:所属文件: <stdio.h>#include <stdio.h>int main()FILE *stream;stream=fopen("MYFILE.TXT","w+");fprintf

23、(stream,"This is a test");printf("The file pointer is at byte %ld",ftell(stream);fclose(stream);return 0;*开辟空间*1、 函数名称: malloc函数原型: void * malloc(unsigned size);函数功能: 分配size字节的存储区函数返回: 所分配的内存区地址,如果内存不够,返回0参数说明:所属文件: <stdlib.h>#include <stdio.h>#include <string.h>

24、;#include <stdlib.h>int main()char *str;if(str=malloc(10)=NULL)printf("Not enough memory to allocate buffer");exit(1);strcpy(str,"Hello");printf("String is %s",str);free(str);return 0;2、 函数名称: realloc函数原型: void * realloc(void * p,unsigned size);函数功能: 将p所指出的已分配内存区的

25、大小改为size,size可以比原来分配的空间大或小函数返回: 返回指向该内存区的指针.NULL-分配失败参数说明:所属文件: <stdlib.h>#include <stdio.h>#include <stdlib.h>#include <string.h>int main()char *str;str= malloc(10);strcpy(str,"Hello");printf("String is %s Address is %p",str,str);str=realloc(str,20);print

26、f("String is %s New address is %p",str,str);free(str);return 0;*输入输出*函数名称: scanf函数原型: int scanf(char * format,args,.);函数功能: 从标准输入设备按format指向的格式字符串规定的格式,输入数据给agrs所指向的单元函数返回: 读入并赋给args的数据个数.遇文件结束返回EOF,出错返回0参数说明: args-指针所属文件: <stdio.h>int main()int a,b,c;scanf("%d%d%d",&a,

27、&b,&c);printf("%d,%d,%dn",a,b,c);return 0; 函数名称: printf函数原型: int printf(char * format,args,.);函数功能: 按format指向的格式字符串所规定的格式,将输出表列args的值输出到标准输出设备函数返回: 输出字符的个数.若出错返回负数参数说明: format-是一个字串,或字符数组的起始地址所属文件: <stdio.h>#include <stdio.h>int main()char c='a'int i=97;printf("%c,%dn",c,c);printf("%c,%dn",i,i);return 0;函数名称: getc函数原型: int getc(FILE *fp);函数功能: 从fp所指向的文件中读入一个字符函数返回: 返回所读的字符,若文件结束或出错,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论