C语言函数大全--c开头的函数

简介: 【6月更文挑战第4天】本篇介绍 C语言中 c开头的函数【C语言函数大全】

image.png

总览

函数声明 函数功能
double cbrt (double x) 计算 x 的立方根(double)
float cbrtf (float x) 计算 x 的立方根(float)
long double cbrtl (long double x) 计算 x 的立方根(long double)
double ceil (double x) 计算大于或等于x的最小整数(double)
float ceilf (float x) 计算大于或等于x的最小整数(float)
long double ceill (long double x) 计算大于或等于x的最小整数(long double)
double copysign (double x, double y); 通过组合x的大小和y的符号生成一个值。(double) 例如,如果x为2.0,y为-1.0,则将生成值-2.0。
float copysignf (float x, float y); 通过组合x的大小和y的符号生成一个值。(float) 例如,如果x为2.0,y为-1.0,则将生成值-2.0。
long double copysignl (long double x, long double y); 通过组合x的大小和y的符号生成一个值。(long double) 例如,如果x为2.0,y为-1.0,则将生成值-2.0。
int chdir(const char *path); 更改当前的工作目录。如果成功返回 0,否则返回 -1
int chmod( const char *filename, int pmode); 变更文件或目录的权限。如果改变成功返回0,否则返回-1
int chsize(int handle, long size); 改变文件大小
void circle(int x, int y, int radius); 在给定半径radius,以(x, y)为圆心画圆
void cleardevice(void); 清除图形屏幕
void clearerr(FILE *stream); 复位错误标志,使用它可以使文件错误标志和文件结束标志置为 0。
void clearviewport(); 清除图形视区
int close(int handle); 通过文件描述符handle,来关闭文件,成功返回0,出错返回-1
long clock(void); 确定处理器调用某个进程或函数所用的时间
void closegraph(); 关闭图形系统
double cos(double x); 计算x的余弦(double)
float cosf(float x); 计算x的余弦(float)
long double cosl(long double x); 计算x的余弦(long double)
double cosh(double x); 计算x的双曲余弦(double)
float coshf(float x); 计算x的双曲余弦(float)
long double coshl(long double x); 计算x的双曲余弦(long double)
int creat (const char *filename, int mode); 创建一个新文件或重写一个已存在的文件
char *ctime(const time_t *time); 把日期和时间转换为字符串

1. cbrt,cbrtf,cbrtl

1.1 函数说明

函数声明 函数功能
double cbrt (double x) 计算 x 的立方根(double)
float cbrtf (float x) 计算 x 的立方根(float)
long double cbrtl (long double x) 计算 x 的立方根(long double)

1.2 演示示例

#include <stdio.h>
#include <math.h>

int main(void)
{
   
   
    double x = 8.0;
    float xf = 27.0;
    long double xL = 64.0;

    // 立方根
    printf("The cube root of the double value %.4lf is %.4lf\n", x, cbrt(x));
    printf("The cube root of the float value %.4f is %.4f\n", xf, cbrtf(xf));
    printf("The cube root of the long double value %.4Lf is %.4Lf", xL, cbrtl(xL));    

    return 0;
}

1.3 运行结果

image.png

2. ceil,ceilf,ceill

2.1 函数说明

函数声明 函数功能
double ceil (double x) 计算大于或等于x的最小整数(double)
float ceilf (float x) 计算大于或等于x的最小整数(float)
long double ceill (long double x) 计算大于或等于x的最小整数(long double)

2.2 演示示例

#include <stdio.h>
#include <math.h>

int main(void)
{
   
   
    double x = 2.3;
    float xf = 2.5;
    long double xL = 2.8;

    // 计算大于或等于x的最小整数
    printf("The minimum integer greater than or equal to the [x = %.4lf] is %.4lf\n", x, ceil(x));
    printf("The minimum integer greater than or equal to the [x = %.4f]  is %.4f\n", xf, ceilf(xf));
    printf("The minimum integer greater than or equal to the [x = %.4Lf] is %.4Lf", xL, ceill(xL));    
    return 0;
}

2.3 运行结果

image.png

3. copysign,copysignf,copysignl

3.1 函数说明

函数声明 函数功能
double copysign (double x, double y); 通过组合x的大小和y的符号生成一个值。(double) 例如,如果x为2.0,y为-1.0,则将生成值-2.0。
float copysignf (float x, float y); 通过组合x的大小和y的符号生成一个值。(float) 例如,如果x为2.0,y为-1.0,则将生成值-2.0。
long double copysignl (long double x, long double y); 通过组合x的大小和y的符号生成一个值。(long double) 例如,如果x为2.0,y为-1.0,则将生成值-2.0。

copysign 是一个数学函数,用于返回一个具有特定绝对值和符号的浮点数。它接受两个参数:第一个参数x是数值,第二个参数y是符号。该函数返回一个新的数,其值大小与x相同,但符号与y相同。这意味着如果y是正数,则结果与x相同;如果y是负数,则结果为x的相反数。

copysign 函数在数值分析和信号处理等领域非常有用,因为它允许程序员根据需要修改数据的符号而不改变其绝对大小。

3.2 演示示例

#include <stdio.h>
#include <math.h>

int main(void)
{
   
   
    double x = 2.0, y = -1.0;
    float xf = 2.0, yf = -1.0;
    long double xL = 2.0, yL = -1.0;

    // 通过组合x的大小和y的符号生成一个值
    printf("The double value by combining the magnitude of [x = %.4lf] and the sign of [y = %.4lf] is %.4lf\n", x, y, copysign(x, y));
    printf("The float value by combining the magnitude of [x = %.4f] and the sign of [y = %.4f] is %.4f\n", xf, yf, copysignf(xf, yf));
    printf("The long double value by combining the magnitude of [x = %.4Lf] and the sign of [y = %.4Lf] is %.4Lf", xL, yL, copysignl(xL, yL));    
    return 0;
}

3.3 运行结果

image.png

4. chdir

4.1 函数说明

函数声明 函数功能
int chdir(const char *path); 更改当前的工作目录。如果成功返回 0,否则返回 -1

4.2 演示示例

#include <stdio.h>
#include <stdlib.h>
#include <dir.h>

#define MAXDIR 1000
char old_dir[MAXDIR];
char new_dir[MAXDIR];

int main()
{
   
   
    if (!getcwd(old_dir, MAXDIR)) // 获取当前的目录
    {
   
   
        perror("getcwd()");
        exit(1);
    }
    printf("Current directory is: %s\n", old_dir); 

    if (chdir("D:\\")) // 切换目录,成功返回0,失败返回-1
    {
   
   
        perror("chdir()");
        exit(1);
    }

    if (!getcwd(new_dir, MAXDIR))
    {
   
   
        perror("getcwd()");
        exit(1);
    }
    printf("Current directory is now: %s\n", new_dir);

    printf("\nChanging back to orignal directory: %s\n", old_dir);

    if (chdir(old_dir))
    {
   
   
        perror("chdir()");
        exit(1);
    }

    return 0;
}

4.3 运行结果

image.png

5. chmod

5.1 函数说明

函数声明 函数功能
int chmod( const char *filename, int pmode); 变更文件或目录的权限。如果改变成功返回0,否则返回-1

5.2 演示示例

#include <sys\stat.h>
#include <stdio.h>
#include <io.h>

void make_read_only(char *filename);

int main(void)
{
   
   
    make_read_only("NOTEXIST.FIL");
    make_read_only("students.txt");
    return 0;
}

void make_read_only(char *filename)
{
   
   
    int stat;

    stat = chmod(filename, S_IREAD);
    if (stat)
        printf("Couldn't make %s read-only\n", filename);
    else
        printf("Made %s read-only\n", filename);
}

5.3 运行结果

image.png

6. chsize

6.1 函数说明

函数声明 函数功能
int chsize(int handle, long size); 改变文件大小

6.2 演示示例

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
   
   
   int fh, result;
   unsigned int nbytes = 2048;
   // 打开文件
   if((fh=open("temp.txt", _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE))  != -1)
   {
   
   
      printf("File length before: %ld\n", filelength(fh)); // 获取文件大小
      if((result=chsize(fh, nbytes))== 0)
         printf("Size successfully changed\n");
      else
         printf("Problem in changing the size\n");
      printf("File length after:  %ld\n", filelength(fh));
      close(fh);
   }
   return 0;
}

6.3 运行结果

image.png

7. circle

7.1 函数说明

函数声明 函数功能
void circle(int x, int y, int radius); 在给定半径radius,以(x, y)为圆心画圆

7.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
   
   
    /* request auto detection */
    int gdriver = DETECT, gmode, errorcode;
    int midx, midy;
    int radius = 100;

    /* initialize graphics and local variables */
    initgraph(&gdriver, &gmode, "");

    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk)  /* an error occurred */
    {
   
   
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
    }

    midx = getmaxx() / 2;
    midy = getmaxy() / 2;
    setcolor(getmaxcolor());

    /* draw the circle */
    circle(midx, midy, radius);

    /* clean up */
    getch();
    closegraph();
    return 0;
}

7.3 运行结果

image.png

8. cleardevice

8.1 函数说明

函数声明 函数功能
void cleardevice(void); 清除图形屏幕

8.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
   
   
    /* request auto detection */
    int gdriver = DETECT, gmode, errorcode;
    int midx, midy;

    /* initialize graphics and local variables */
    initgraph(&gdriver, &gmode, "");

    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk)  /* an error occurred */
    {
   
   
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        exit(1); /* terminate with an error code */
    }

    midx = getmaxx() / 2;
    midy = getmaxy() / 2;
    setcolor(getmaxcolor());

    /* for centering screen messages */
    settextjustify(CENTER_TEXT, CENTER_TEXT);

    /* output a message to the screen */
    outtextxy(midx, midy, "press any key to clear the screen:");

    /* wait for a key */
    getch();

    /* clear the screen */
    cleardevice();

    /* output another message */
    outtextxy(midx, midy, "press any key to quit:");

    /* clean up */
    getch();
    closegraph();
    return 0;
}

8.3 运行结果

9. clearerr

9.1 函数说明

函数声明 函数功能
void clearerr(FILE *stream); 复位错误标志,使用它可以使文件错误标志和文件结束标志置为 0。

9.2 演示示例

#include <stdio.h>

int main(void)
{
   
   
    FILE *fp;
    char ch;

    /* open a file for writing */
    fp = fopen("temp.txt", "w");

    /* force an error condition by attempting to read */
    ch = fgetc(fp);
    printf("%c\n",ch);

    int errorFlag = ferror(fp);
    printf("Error Flag : %d\n", errorFlag);
    if (errorFlag)
    {
   
   
        /* display an error message */
        printf("Error reading from temp.txt\n");

        /* reset the error and EOF indicators */
        clearerr(fp);
    }

    printf("Error Flag : %d", ferror(fp));

    fclose(fp);
    return 0;
}

9.3 运行结果

image.png

10. clearviewport

10.1 函数说明

函数声明 函数功能
void clearviewport(); 清除图形视区

10.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

#define CLIP_ON 1   /* activates clipping in viewport */

int main(void)
{
   
   
    /* request auto detection */
    int gdriver = DETECT, gmode, errorcode;
    int ht;

    /* initialize graphics and local variables */
    initgraph(&gdriver, &gmode, "");

    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk)  /* an error occurred */
    {
   
   
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        exit(1); /* terminate with an error code */
    }

    setcolor(getmaxcolor());
    ht = textheight("W");

    /* message in default full-screen viewport */
    outtextxy(0, 0, "* <-- (0, 0) in default viewport");

    /* create a smaller viewport */
    setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);

    /* display some messages */
    outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
    outtextxy(0, 2*ht, "Press any key to clear viewport:");

    /* wait for a key */
    getch();

    /* clear the viewport */
    clearviewport();

    /* output another message */
    outtextxy(0, 0, "Press any key to quit:");

    /* clean up */
    getch();
    closegraph();
    return 0;
}

10.3 运行结果

11. close

11.1 函数说明

函数声明 函数功能
int close(int handle); 通过文件描述符handle,来关闭文件,成功返回0,出错返回-1

11.2 演示示例

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main()
{
   
   
    int handle;
    char buf[11] = "0123456789";

    /* create a file containing 10 bytes */
    handle = open("temp.txt", _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE);
    if (handle > -1)
    {
   
   
        write(handle, buf, strlen(buf));
        printf("Write successfully\n");
        /* close the file */
        close(handle);
        printf("Close File successfully");
    }
    else
    {
   
   
        printf("Error opening file\n");
    }
    return 0;
}

11.3 运行结果

image.png
image.png

12. clock

12.1 函数说明

函数声明 函数功能
long clock(void); 确定处理器调用某个进程或函数所用的时间

12.2 演示示例

#include <time.h>
#include <stdio.h>
#include <dos.h>

int main(void)
{
   
   
    clock_t start, end;
    start = clock();
    printf("start = %ld\n", start);
    getchar();

    end = clock();
    printf("end = %ld\n", end);
    printf("The time was: %.3lfs\n", (double) (end - start) / CLK_TCK);

    return 0;
}

12.3 运行结果

image.png

13. closegraph

13.1 函数说明

函数声明 函数功能
void closegraph(); 关闭图形系统

13.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
   
   
    /* request auto detection */
    int gdriver = DETECT, gmode, errorcode;
    int x, y;

    /* initialize graphics mode */
    initgraph(&gdriver, &gmode, "");

    /* read result of initialization */
    errorcode = graphresult();

    if (errorcode != grOk)  /* an error occurred */
    {
   
   
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        exit(1); /* terminate with an error code */
    }

    x = getmaxx() / 2;
    y = getmaxy() / 2;

    /* output a message */
    settextjustify(CENTER_TEXT, CENTER_TEXT);
    outtextxy(x, y, "Press a key to close the graphics system:");

    /* wait for a key */
    getch();

    /* closes down the graphics system */
    closegraph();

    printf("We're now back in text mode.\n");
    printf("Press any key to halt:");
    getchar();
    return 0;
}

13.3 运行结果

1717140325843747518.gif

14. cos,cosf,cosl

14.1 函数说明

函数声明 函数功能
double cos(double x); 计算x的余弦(double)
float cosf(float x); 计算x的余弦(float)
long double cosl(long double x); 计算x的余弦(long double)

14.2 演示示例

// Huazie
#include <stdio.h>
#include <math.h>

int main(void)
{
   
   
    double x = 1.0;
    double result = cos(x); // 余弦

    float xf = 1.0f;
    float resultf = cosf(xf);

    long double xL = 1.0;
    long double resultL = cosl(xL);

    printf("The cosine of %lf is %.16lf\n", x, result);
    printf("The cosine of %f is %.16f\n", xf, resultf);
    printf("The cosine of %Lf is %.16Lf\n", xL, resultL);

    return 0;
}

14.3 运行结果

image.png

15. cosh,coshf,coshl

15.1 函数说明

函数声明 函数功能
double cosh(double x); 计算x的双曲余弦(double)
float coshf(float x); 计算x的双曲余弦(float)
long double coshl(long double x); 计算x的双曲余弦(long double)

15.2 演示示例

// Huazie
#include <stdio.h>
#include <math.h>

int main(void)
{
   
   
    double x = 1.0;
    double result = cosh(x); // 双曲余弦

    float xf = 1.0f;
    float resultf = coshf(xf);

    long double xL = 1.0;
    long double resultL = coshl(xL);

    printf("The hyperbolic coshine of %lf is %.16lf\n", x, result);
    printf("The hyperbolic coshine of %f is %.16f\n", xf, resultf);
    printf("The hyperbolic coshine of %Lf is %.16Lf\n", xL, resultL);

    return 0;
}

15.3 运行结果

image.png

16. creat

16.1 函数说明

函数声明 函数功能
int creat (const char *filename, int mode); 创建一个新文件或重写一个已存在的文件

16.2 演示示例

#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main(void)
{
   
   
   int handle;
   char buf[11] = "0123456789";

   handle = creat("temp1.txt", S_IREAD | S_IWRITE);

   printf("Create file successfully");

   /* write 10 bytes to the file */
   write(handle, buf, strlen(buf));

   /* close the file */
   close(handle);
   return 0;
}

16.3 运行结果

image.png

17. ctime

17.1 函数说明

函数声明 函数功能
char *ctime(const time_t *time); 把日期和时间转换为字符串

17.2 演示示例

#include <stdio.h>
#include <time.h>

int main(void)
{
   
   
    time_t t;
    time(&t);
    printf("Today's date and time: %s\n", ctime(&t));
    return 0;
}

17.3 运行结果

image.png

参考

  1. [API Reference Document]
目录
相关文章
|
1月前
|
存储 编译器 C语言
【C语言程序设计——函数】分数数列求和2(头歌实践教学平台习题)【合集】
函数首部:按照 C 语言语法,函数的定义首部表明这是一个自定义函数,函数名为fun,它接收一个整型参数n,用于指定要求阶乘的那个数,并且函数的返回值类型为float(在实际中如果阶乘结果数值较大,用float可能会有精度损失,也可以考虑使用double等更合适的数据类型,这里以float为例)。例如:// 函数体代码将放在这里函数体内部变量定义:在函数体中,首先需要定义一些变量来辅助完成阶乘的计算。比如需要定义一个变量(通常为float或double类型,这里假设用float。
37 3
|
1月前
|
存储 算法 安全
【C语言程序设计——函数】分数数列求和1(头歌实践教学平台习题)【合集】
if 语句是最基础的形式,当条件为真时执行其内部的语句块;switch 语句则适用于针对一个表达式的多个固定值进行判断,根据表达式的值与各个 case 后的常量值匹配情况,执行相应 case 分支下的语句,直到遇到 break 语句跳出 switch 结构,若没有匹配值则执行 default 分支(可选)。例如,在判断一个数是否大于 10 的场景中,条件表达式为 “num> 10”,这里的 “num” 是程序中的变量,通过比较其值与 10 的大小关系来确定条件的真假。常量的值必须是唯一的,且在同一个。
20 2
|
1月前
|
存储 C语言
【C语言程序设计——函数】递归求斐波那契数列的前n项(头歌实践教学平台习题)【合集】
本关任务是编写递归函数求斐波那契数列的前n项。主要内容包括: 1. **递归的概念**:递归是一种函数直接或间接调用自身的编程技巧,通过“俄罗斯套娃”的方式解决问题。 2. **边界条件的确定**:边界条件是递归停止的条件,确保递归不会无限进行。例如,计算阶乘时,当n为0或1时返回1。 3. **循环控制与跳转语句**:介绍`for`、`while`循环及`break`、`continue`语句的使用方法。 编程要求是在右侧编辑器Begin--End之间补充代码,测试输入分别为3和5,预期输出为斐波那契数列的前几项。通关代码已给出,需确保正确实现递归逻辑并处理好边界条件,以避免栈溢出或结果
66 16
|
1月前
|
存储 编译器 C语言
【C语言程序设计——函数】回文数判定(头歌实践教学平台习题)【合集】
算术运算于 C 语言仿若精密 “齿轮组”,驱动着数值处理流程。编写函数求区间[100,500]中所有的回文数,要求每行打印10个数。根据提示在右侧编辑器Begin--End之间的区域内补充必要的代码。如果操作数是浮点数,在 C 语言中是不允许直接进行。的结果是 -1,因为 -7 除以 3 商为 -2,余数为 -1;注意:每一个数据输出格式为 printf("%4d", i);的结果是 1,因为 7 除以 -3 商为 -2,余数为 1。取余运算要求两个操作数必须是整数类型,包括。开始你的任务吧,祝你成功!
52 1
|
1月前
|
C语言
【C语言程序设计——函数】亲密数判定(头歌实践教学平台习题)【合集】
本文介绍了通过编程实现打印3000以内的全部亲密数的任务。主要内容包括: 1. **任务描述**:实现函数打印3000以内的全部亲密数。 2. **相关知识**: - 循环控制和跳转语句(for、while循环,break、continue语句)的使用。 - 亲密数的概念及历史背景。 - 判断亲密数的方法:计算数A的因子和存于B,再计算B的因子和存于sum,最后比较sum与A是否相等。 3. **编程要求**:根据提示在指定区域内补充代码。 4. **测试说明**:平台对代码进行测试,预期输出如220和284是一组亲密数。 5. **通关代码**:提供了完整的C语言代码实现
61 24
|
1月前
|
存储 算法 C语言
【C语言程序设计——函数】素数判定(头歌实践教学平台习题)【合集】
本内容介绍了编写一个判断素数的子函数的任务,涵盖循环控制与跳转语句、算术运算符(%)、以及素数的概念。任务要求在主函数中输入整数并输出是否为素数的信息。相关知识包括 `for` 和 `while` 循环、`break` 和 `continue` 语句、取余运算符 `%` 的使用及素数定义、分布规律和应用场景。编程要求根据提示补充代码,测试说明提供了输入输出示例,最后给出通关代码和测试结果。 任务核心:编写判断素数的子函数并在主函数中调用,涉及循环结构和条件判断。
63 23
|
1月前
|
算法 C语言
【C语言程序设计——函数】利用函数求解最大公约数和最小公倍数(头歌实践教学平台习题)【合集】
本文档介绍了如何编写两个子函数,分别求任意两个整数的最大公约数和最小公倍数。内容涵盖循环控制与跳转语句的使用、最大公约数的求法(包括辗转相除法和更相减损术),以及基于最大公约数求最小公倍数的方法。通过示例代码和测试说明,帮助读者理解和实现相关算法。最终提供了完整的通关代码及测试结果,确保编程任务的成功完成。
68 15
|
2月前
|
存储 程序员 C语言
【C语言】文件操作函数详解
C语言提供了一组标准库函数来处理文件操作,这些函数定义在 `<stdio.h>` 头文件中。文件操作包括文件的打开、读写、关闭以及文件属性的查询等。以下是常用文件操作函数的详细讲解,包括函数原型、参数说明、返回值说明、示例代码和表格汇总。
69 9
|
2月前
|
C语言 开发者
【C语言】数学函数详解
在C语言中,数学函数是由标准库 `math.h` 提供的。使用这些函数时,需要包含 `#include <math.h>` 头文件。以下是一些常用的数学函数的详细讲解,包括函数原型、参数说明、返回值说明以及示例代码和表格汇总。
64 6
|
2月前
|
存储 C语言 开发者
【C语言】字符串操作函数详解
这些字符串操作函数在C语言中提供了强大的功能,帮助开发者有效地处理字符串数据。通过对每个函数的详细讲解、示例代码和表格说明,可以更好地理解如何使用这些函数进行各种字符串操作。如果在实际编程中遇到特定的字符串处理需求,可以参考这些函数和示例,灵活运用。
94 10

热门文章

最新文章