函数的作用是什么?
首先,我们都知道,在c语言中我们有且仅有一个主函数——main()函数。
我们在面对具体问题的时候,通常会有不同的功能需要相互之间配合,但是又不能都写在主函数里面。
有的函数需要满足一定条件的时候才会触发,程序的执行过程是由上而下、由左到右执行的。
函数能够帮助我们对程序进行模块化设计
常用的有哪些函数??
请大家先看一下这段代码
#include<stdio.h>
void main()
{
printf("hello!world");
}
那么这段代码里面有几个函数????
我们来看看,从头开始看。
首先第一个函数main()函数,这是每一个c程序都不可或缺的函数 ,代码的执行必须从main()函数开始
viod main()
{
}
第二个函数printf()
#include<stdio.h>
int main()
{
printf("hello!world");
}
printf函数是的输出函数,是大家经常会用到的函数。但是有没有想过一个问题??
printf函数从哪里来的???
printf()来自于stdio.h这个头文件
除了printf函数还有以下函数是来自于stdio头文件
序号 | 函数 & 描述 |
---|---|
1 | int fclose(FILE *stream) 关闭流 stream。刷新所有的缓冲区。 |
2 | void clearerr(FILE *stream) 清除给定流 stream 的文件结束和错误标识符。 |
3 | int feof(FILE *stream) 测试给定流 stream 的文件结束标识符。 |
4 | int ferror(FILE *stream) 测试给定流 stream 的错误标识符。 |
5 | int fflush(FILE *stream) 刷新流 stream 的输出缓冲区。 |
6 | int fgetpos(FILE *stream, fpos_t *pos) 获取流 stream 的当前文件位置,并把它写入到 pos。 |
7 | FILE *fopen(const char *filename, const char *mode) 使用给定的模式 mode 打开 filename 所指向的文件。 |
8 | size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) 从给定流 stream 读取数据到 ptr 所指向的数组中。 |
9 | FILE *freopen(const char *filename, const char *mode, FILE *stream) 把一个新的文件名 filename 与给定的打开的流 stream 关联,同时关闭流中的旧文件。 |
10 | int fseek(FILE *stream, long int offset, int whence) 设置流 stream 的文件位置为给定的偏移 offset,参数 offset 意味着从给定的 whence 位置查找的字节数。 |
11 | int fsetpos(FILE *stream, const fpos_t *pos) 设置给定流 stream 的文件位置为给定的位置。参数 pos 是由函数 fgetpos 给定的位置。 |
12 | long int ftell(FILE *stream) 返回给定流 stream 的当前文件位置。 |
13 | size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) 把 ptr 所指向的数组中的数据写入到给定流 stream 中。 |
14 | int remove(const char *filename) 删除给定的文件名 filename,以便它不再被访问。 |
15 | int rename(const char *old_filename, const char *new_filename) 把 old_filename 所指向的文件名改为 new_filename。 |
16 | void rewind(FILE *stream) 设置文件位置为给定流 stream 的文件的开头。 |
17 | void setbuf(FILE *stream, char *buffer) 定义流 stream 应如何缓冲。 |
18 | int setvbuf(FILE *stream, char *buffer, int mode, size_t size 另一个定义流 stream 应如何缓冲的函数。 |
19 | FILE *tmpfile(void) 以二进制更新模式(wb+)创建临时文件。 |
20 | char *tmpnam(char *str) 生成并返回一个有效的临时文件名,该文件名之前是不存在的。 |
21 | int fprintf(FILE *stream, const char *format, …) 发送格式化输出到流 stream 中。 |
22 | int printf(const char *format, …) 发送格式化输出到标准输出 stdout。 |
23 | int sprintf(char *str, const char *format, …) 发送格式化输出到字符串。 |
24 | int vfprintf(FILE *stream, const char *format, va_list arg) 使用参数列表发送格式化输出到流 stream 中。 |
25 | int vprintf(const char *format, va_list arg) 使用参数列表发送格式化输出到标准输出 stdout。 |
26 | int vsprintf(char *str, const char *format, va_list arg) 使用参数列表发送格式化输出到字符串。 |
27 | int fscanf(FILE *stream, const char *format, …) 从流 stream 读取格式化输入。 |
28 | int scanf(const char *format, …) 从标准输入 stdin 读取格式化输入。 |
29 | int sscanf(const char *str, const char *format, …) 从字符串读取格式化输入。 |
30 | int fgetc(FILE *stream) 从指定的流 stream 获取下一个字符(一个无符号字符),并把位置标识符往前移动。 |
31 | char *fgets(char *str, int n, FILE *stream) 从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内。当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。 |
32 | int fputc(int char, FILE *stream) 把参数 char 指定的字符(一个无符号字符)写入到指定的流 stream 中,并把位置标识符往前移动。 |
33 | int fputs(const char *str, FILE *stream) 把字符串写入到指定的流 stream 中,但不包括空字符。 |
34 | int getc(FILE *stream) 从指定的流 stream 获取下一个字符(一个无符号字符),并把位置标识符往前移动。 |
35 | int getchar(void) 从标准输入 stdin 获取一个字符(一个无符号字符)。 |
36 | char *gets(char *str) 从标准输入 stdin 读取一行,并把它存储在 str 所指向的字符串中。当读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。 |
37 | int putc(int char, FILE *stream) 把参数 char 指定的字符(一个无符号字符)写入到指定的流 stream 中,并把位置标识符往前移动。 |
38 | int putchar(int char) 把参数 char 指定的字符(一个无符号字符)写入到标准输出 stdout 中。 |
39 | int puts(const char *str) 把一个字符串写入到标准输出 stdout,直到空字符,但不包括空字符。换行符会被追加到输出中。 |
40 | int ungetc(int char, FILE *stream) 把字符 char(一个无符号字符)推入到指定的流 stream 中,以便它是下一个被读取到的字符。 |
41 | void perror(const char *str) 把一个描述性错误消息输出到标准错误 stderr。首先输出字符串 str,后跟一个冒号,然后是一个空格。 |
math.h头文件
函数名 | 函数&描述 | 说明 | 返回值 |
---|---|---|---|
函数acos | double acos(double x); | acos计算x的三角反余弦函数主值,如果x不在[-1,+1]内,则发生定义域错误 | 返回[0,180]范围内的x的反余弦值。 |
函数asin | double asin(double x); | asin计算x的三角反正弦函数主值,如果x不在[-1,+1]内,则发生定义域错误 | 返回[-90,+90]范围内的x的反正弦值。 |
函数atan | double atan(double x); | atan计算x的三角反正切函数主值 | 返回[-90,+90]范围内的x的反正切值。 |
函数atan2 | double atan2(double y,double x); | atan2计算y/x的三角反正切函数主值,根据两参数的符号判断返回值的象限。如果两个参数都为0,则发生定义域错误 | 返回[-180,+180]范围内的y/x的反正切值。 |
函数cos | double cos(double x); | cos计算x的余弦值。 | 返回余弦值。 |
函数sin | double sin(double x); | sin计算x的正弦值。 | 返回正弦值。 |
函数tan | double tan(double x); | tan计算x的正切值。 | 返回正切值。 |
函数cosh | double cosh(double x); | cosh计算x的双曲余弦值,如果x太大,则发生值域错误。 | 回双曲余弦值。 |
函数sinh | double sinh(double x); | sinh计算x的双曲正弦值,如果x太大,则发生值域错误。 | 返回双曲正弦值。 |
函数tanh | double tanh(double x); | 计算x的双曲正切值。 | 返回双曲正切值 |
函数exp | double exp(double x); | sinh计算x的指数函数,如果x太大,则发生值域错误。 | 返回指数值。 |
函数frexp | double frexp(double value,int *exp); | frexp把一个浮点数分成一个规格化小数和一个2的整数幂。它的整数值存储在exp指向的int类型的对象中。 | frexp返回值x,使x为[1/2,1)范围内的double值,或者为0,value等于x乘以2的*exp次幂。如果value为0,则结果的两部分都为0 |
函数ldexp | double ldexp(double x,int exp); | ldexp计算一个浮点数和2的整数幂的乘积,有可能发生值域错误。 | 返回x乘以2的exp次幂的值 |
函数log | double log(double x); | log计算x的自然对数。如果参数为负,则发生定义域错误;如果参数为0,则发生值域错误。 | 返回自然对数值 |
函数log10 | double log10(double x); | og10计算x的以10为底的对数。如果参数为负,则发生定义域错误;如果参数为0,则发生值域错误。 | 返回以10为底的对数值。 |
函数modf | double modf(double value,double *iptr); | modf把参数value分成整数和小数部分,它们的符号和参数相同,它把整数部分存储在iptr指向的double类型的对象中。 | 返回value的带符号的小数部分。 |
函数pow | double pow(double x,double y); | pow计算x的y次幂,如果x是负值且y不是一个整数值,则发生定义域错误;当x为0且y小于0时,如果结果不能表示,则发生定义域错误,也可能发生值域错误 | 返回x的y次幂。 |
函数sqrt | double sqrt(double x) | sqrt计算x的非负的平方根。如果参数为负,则发生定义域错误。 | 回平方根的值。 |
函数ceil | double ceil(double x) | ceil计算不小于x的最小整数。 | 返回不小于x的最小整数,表示为double类型。 |
函数fabs | double fabs(double x) | fabs计算浮点数x的绝对值 | 返回x的绝对值。 |
函数floor | double floor(double x) | fabs计算不大于x的最大整数。 | 大于x的最大整数。 |
函数fmod | double floor(double x,double y) | floor计算x/y的浮点余数。 | 如果y不为0,结果的符号和x相同且数值上比y小;如果y为0,则发生定义域错误,或者函数返回0。 |