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

总览

函数声明 函数功能
char ecvt(double value, int ndigit, int *decpt, int *sign); 把一个双精度浮点型数转换为字符串,转换结果中不包括十进制小数点
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius); 画一段椭圆线
int eof(int *handle); 检测文件结束
int execl(const char *pathname, const char *arg0, ... const char *argn, NULL); 载入并运行其它程序
int execlp(char *pathname, char *arg0, ... const char *argn, NULL); 载入并运行其它程序
int execlpe(const char *pathname, const char *arg0, ... const char *argn, NULL, const char *const *envp); 载入并运行其它程序
int execv(const char *pathname, char *const *argv); 载入并运行其它程序
int execve(const char *pathname, char *const argv[], char *const envp[]); 载入并运行其它程序
int execvp(const char *pathname, char *const argv[]); 载入并运行其它程序
int execvpe(const char *pathname, char *const argv[], char *const envp[]); 载入并运行其它程序
void exit(int status); 终止程序
double exp(double x); 计算 x 的基数e指数(double)
float expf(float x); 计算 x 的基数e指数(float)
long double expl(long double x); 计算 x 的基数e指数(long double)
double exp2(double x); 计算 x 的基数为2的指数(double)
float exp2f(float x); 计算 x 的基数为2的指数(float)
long double exp2l(long double x); 计算 x 的基数为2的指数(long double)
double expm1 (double x); 计算 e 的 x 次方 减 1,即 (e^x) - 1 (double)
float expm1f (float x); 计算 e 的 x 次方 减 1,即 (e^x) - 1 (float)
long double expm1l (long double x); 计算 e 的 x 次方 减 1,即 (e^x) - 1 (long double)
double erf (double x); 计算 x 的 误差函数(double)
float erff (float x); 计算 x 的 误差函数(float)
long double erfl (long double x); 计算 x 的 误差函数(long double)
double erfc (double x); 计算 x 的互补误差函数(double)
float erfcf (float x); 计算 x 的互补误差函数(float)
long double erfcl (long double x); 计算 x 的互补误差函数(long double)

1. ecvt

1.1 函数说明

函数声明 函数功能
char ecvt(double value, int ndigit, int *decpt, int *sign); 把一个双精度浮点型数转换为字符串,转换结果中不包括十进制小数点
  • value : 待转换的双精度浮点数。
  • ndigit :存储的有效数字位数。这个函数存储最多 ndigit 个数字值作为一个字符串,并添加一个结束符(‘\0’),如果 value 中的数字个数超过 ndigit,低位数字被舍入。如果少于 ndigit 个数字,该字符串用 0填充。
  • decpt :指出给出小数点位置的整数值, 它是从该字符串的开头位置计算的。0 或负数指出小数点在第一个数字的左边。
  • sign :指出一个指出转换的数的符号的整数。如果该整数为 0,这个数为正数,否则为负数。

1.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main()
{
char *string;
double value;
int decpt, sign;

int ndigit = 10;
value = 9.876;
string = ecvt(value, ndigit, &decpt, &sign);
printf("string = %-16s decpt = %d sign = %d value = %lf\n", string, decpt, sign, value);

value = -123.45;
ndigit= 15;
string = ecvt(value, ndigit, &decpt, &sign);
printf("string = %-16s decpt = %d sign = %d value = %lf\n", string, decpt, sign, value);

value = 0.6789e5; /* 科学记数法 scientific notation */
ndigit = 5;
string = ecvt(value, ndigit, &decpt, &sign);
printf("string = %-16s decpt = %d sign = %d value = %lf\n", string, decpt, sign, value);

return 0;
}

1.3 运行结果

2. ellipse

2.1 函数说明

函数声明 函数功能
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius); 画一段椭圆线

(x, y) 为中心,xradiusyradiusx 轴y 轴 半径,从角 stangle 开始,endangle 结束,画一段椭圆线。当stangle=0,endangle=360 时,画出一个完整的椭圆

2.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
// request auto detection
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 0, endangle = 360, midangle = 180;
int xradius = 100, yradius = 50;

// initialize graphics, 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);
}

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

// draw ellipse
ellipse(midx, 50, midangle, endangle, xradius, yradius);

ellipse(midx, midy, stangle, endangle, xradius, yradius);

ellipse(midx, getmaxy() - 50, stangle, midangle, xradius, yradius);

getch();
closegraph();
return 0;
}

2.3 运行结果

3. eof

3.1 函数说明

函数声明 函数功能
int eof(int *handle); 检测文件结束

3.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <sys\stat.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main(void)
{
int handle;
char msg[] = "This is a test";
char ch;

// create a file
handle = open("STU.FIL", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);

// write some data to the file
write(handle, msg, strlen(msg));

// seek to the beginning of the file
lseek(handle, 0L, SEEK_SET);

// reads chars from the file until hit EOF
do
{
read(handle, &ch, 1);
printf("%c", ch);
} while (!eof(handle));

close(handle);
return 0;
}

3.3 运行结果

4. execl

4.1 函数说明

函数声明 函数功能
int execl(const char *pathname, const char *arg0, ... const char *argn, NULL); 载入并运行其它程序

注意: execl 函数,其后缀 l 代表 list,也就是参数列表的意思。第一个参数 path 字符指针指向要执行的文件路径, 接下来的参数代表执行该文件时传递的参数列表:argv[0],argv[1]... ,最后一个参数须用空指针 NULL 作结束。

4.2 演示示例

4.2.1 SubTest.c

1
2
3
4
5
6
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("exec %s, Hello, %s", argv[0], argv[1]);
return 0;
}

4.2.2 Test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <process.h>
#include <stdio.h>
#include <errno.h>

void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec SubTest with subargv ...\n");
// 成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印
// 第一个参数需要执行文件的全路径,这里写直接文件名,是因为和当前源码在同一目录中
int result = execl("SubTest.exe", argv[0], "Huazie" , NULL);
// 执行成功,这里不会执行到
printf("result = %d\n", result);
perror("exec error");
exit(1);
}

4.3 运行结果

执行失败:

执行成功:

5. execle

5.1 函数说明

函数声明 函数功能
int execle(const char *pathname, const char *arg0, ... const char *argn, NULL, const char *const *envp); 载入并运行其它程序

注意: execl 函数是用来执行参数 path 字符串所代表的文件路径。接下来的参数代表执行该文件时传递过去的 argv[0], argv[1]…,并且倒数第二个参数必须用空指针 NULL 作结束,最后一个参数为 环境变量

5.2 演示示例

5.2.1 SubEnvTest.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <process.h>
#include <processenv.h>
#include <stdio.h>

int main(int argc, char *argv[], char **envp)
{
printf("SubEnvTest Command line arguments:\n");
for (int i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec %s, Hello, %s\n", argv[0], argv[1]);

for (int i = 0; envp[i] != NULL; i++)
{
printf("%s\n", envp[i]);
}
// while(*envp != NULL)
// printf(" %s\n", *(envp++));

return 0;
}

5.2.2 Test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <process.h>
#include <stdio.h>
#include <errno.h>

void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec SubEnvTest with subargv ...\n");
const char *envp[] = {"AUTHOR=Huazie", "DATE=2023-03-28", NULL}; // 环境变量

// 成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印
// 第一个参数需要执行文件的全路径,这里直接写文件名,是因为和当前源码在同一目录中
int result = execle("SubEnvTest.exe", argv[0], "Huazie" , NULL, envp);
// 执行成功,这里不会执行到
printf("result = %d\n", result);
perror("exec error");
exit(1);
}

5.3 运行结果

6. execlp

6.1 函数说明

函数声明 函数功能
int execlp(char *pathname, char *arg0, ... const char *argn, NULL); 载入并运行其它程序

注意: execlp 函数会从 PATH 环境变量所指的目录中查找符合参数 pathname 的文件名,找到后便执行该文件,然后将第二个以后的参数当做该文件的arg0, arg1, …,最后一个参数必须用 空指针 NULL 作结束。

6.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <process.h>
#include <stdio.h>
#include <errno.h>

void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec java with subargv ...\n");
// 成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印
int result = execlp("java.exe", "java", "-version", NULL);
// 执行成功,这里不会执行到
printf("result = %d\n", result);
perror("exec error");
exit(1);
}

6.3 运行结果

7. execlpe

7.1 函数说明

函数声明 函数功能
int execlpe(const char *pathname, const char *arg0, ... const char *argn, NULL, const char *const *envp); 载入并运行其它程序

注意: execlp 函数会从 PATH 环境变量所指的目录中查找符合参数 pathname 的文件名,找到后便执行该文件,然后将第二个以后的参数当做该文件的arg0, arg1, …,其中倒数第二个参数必须用 空指针 NULL 作结束,最后一个参数为 环境变量

7.2 演示示例

7.2.1 SubEnvTest.c

参考 5.2.1SubEnvTest.c

7.2.2 Test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <process.h>
#include <stdio.h>
#include <errno.h>

void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec SubEnvTest with subargv ...\n");
const char *envp[] = {"AUTHOR=Huazie", "DATE=2023-03-28", NULL}; // 环境变量
// 成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印
int result = execlpe("SubEnvTest", argv[0], "Huazie", NULL, envp);
// 执行成功,这里不会执行到
printf("result = %d\n", result);
perror("exec error");
exit(1);
}

7.3 运行结果

8. execv

8.1 函数说明

函数声明 函数功能
int execv(const char *pathname, char *const *argv); 载入并运行其它程序

注意:execv 函数用来运行參数 pathname 字符串所指向的程序,第二个参数 argv 为參数列表【该数组的最后一个元素必须是空指针 NULL】。

8.2 演示示例

8.2.1 SubTest.c

参考 4.2.1SubTest.c

8.2.2 Test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <process.h>
#include <stdio.h>
#include <errno.h>

void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec SubTest with subargv ...\n");
char *const subargv[] = {argv[0], "Huazie" , NULL}; // 参数列表
// 成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印
// 第一个参数需要执行文件的全路径,这里写直接文件名,是因为和当前源码在同一目录中
int result = execv("SubTest.exe", subargv);
// 执行成功,这里不会执行到
printf("result = %d\n", result);
perror("exec error");
exit(1);
}

8.3 运行结果

9. execve

9.1 函数说明

函数声明 函数功能
int execve(const char *pathname, char *const argv[], char *const envp[]); 载入并运行其它程序

注意:execve 函数用来运行參数 pathname 字符串所指向的程序,第二个参数 argv 为參数列表【该数组的最后一个元素必须是空指针 NULL】,最后一个参数为 环境变量

9.2 演示示例

9.2.1 SubEnvTest.c

参考 5.2.1SubEnvTest.c

9.2.2 Test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <process.h>
#include <stdio.h>
#include <errno.h>

void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec SubEnvTest with subargv ...\n");
char *const subargv[] = {argv[0], "Huazie" , NULL}; // 参数列表
char *const envp[] = {"AUTHOR=Huazie", "DATE=2023-03-28", NULL}; // 环境变量
// 成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印
// 第一个参数需要执行文件的全路径,这里写直接文件名,是因为和当前源码在同一目录中
int result = execve("SubEnvTest.exe", subargv, envp);
// 执行成功,这里不会执行到
printf("result = %d\n", result);
perror("exec error");
exit(1);
}

9.3 运行结果

10. execvp

10.1 函数说明

函数声明 函数功能
int execvp(const char *pathname, char *const argv[]); 载入并运行其它程序

注意:execvp 函会从 PATH 环境变量所指的目录中查找符合参数 pathname 的文件名,找到后便执行该文件,第二个参数 argv 为參数列表【该数组的最后一个元素必须是空指针 NULL】。

10.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <process.h>
#include <stdio.h>
#include <errno.h>

void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec java with subargv ...\n");
char *const subargv[] = {"java", "-version" , NULL}; // 参数列表
// 成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印
int result = execvp("java.exe", subargv);
// 执行成功,这里不会执行到
printf("result = %d\n", result);
perror("exec error");
exit(1);
}

10.3 运行结果

11. execvpe

11.1 函数说明

函数声明 函数功能
int execvpe(const char *pathname, char *const argv[], char *const envp[]); 载入并运行其它程序

注意:execvpe 函会从 PATH 环境变量所指的目录中查找符合参数 pathname 的文件名,找到后便执行该文件,第二个参数 argv 为參数列表【该数组的最后一个元素必须是空指针 NULL】,最后一个参数为 环境变量

11.2 演示示例

11.2.1 SubEnvTest.c

参考 5.2.1SubEnvTest.c

11.2.2 Test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <process.h>
#include <stdio.h>
#include <errno.h>

void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec SubEnvTest with subargv ...\n");
char *const subargv[] = {argv[0], "Huazie" , NULL}; // 参数列表
char *const envp[] = {"AUTHOR=Huazie", "DATE=2023-03-28", NULL}; // 环境变量
// 成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印
int result = execvpe("SubEnvTest.exe", subargv, envp);
// 执行成功,这里不会执行到
printf("result = %d\n", result);
perror("exec error");
exit(1);
}

11.3 运行结果

12. exit

12.1 函数说明

函数声明 函数功能
void exit(int status); 终止程序

注意: exit 函数通常是用来终结程序用的,使用后程序自动结束,跳回操作系统。exit(0) 表示程序正常退出【相当于主函数 return 0;】,exit⑴ 表示程序异常退出【相当于主函数 return 1;】。

12.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int status;

printf("Enter either 1 or 2\n");
status = getchar();
/* Sets DOS errorlevel */
exit(status - '0');

/* Note: this line is never reached */
printf("this line is never reached!");
return 0;
}

12.3 运行结果

13. exp,expf,expl

13.1 函数说明

函数声明 函数功能
double exp(double x); 计算 x 的基数e指数(double)
float expf(float x); 计算 x 的基数e指数(float)
long double expl(long double x); 计算 x 的基数e指数(long double)

13.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>

int main(void)
{
double result, x = 4.0;
result = exp(x);

float resultf, xf = 4.0;
resultf = expf(xf);

long double resultL, xL = 4.0;
resultL = expl(xL);

printf("\n'e' raised to the power of %lf (e ^ %lf) = %.20lf\n", x, x, result);
printf("\n'e' raised to the power of %f (e ^ %f) = %.20f\n", xf, xf, resultf);
printf("\n'e' raised to the power of %Lf (e ^ %Lf) = %.20Lf\n", xL, xL, resultL);

return 0;
}

13.3 运行结果

14. exp2,exp2f,exp2l

14.1 函数说明

函数声明 函数功能
double exp2(double x); 计算 x 的基数为2的指数(double)
float exp2f(float x); 计算 x 的基数为2的指数(float)
long double exp2l(long double x); 计算 x 的基数为2的指数(long double)

14.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>

int main(void)
{
double result, x = 4.5;
result = exp2(x);

float resultf, xf = 4.5;
resultf = exp2f(xf);

long double resultL, xL = 4.5;
resultL = exp2l(xL);

printf("\n'2' raised to the power of %lf (2 ^ %lf) = %.20lf\n", x, x, result);
printf("\n'2' raised to the power of %f (2 ^ %f) = %.20f\n", xf, xf, resultf);
printf("\n'2' raised to the power of %Lf (2 ^ %Lf) = %.20Lf\n", xL, xL, resultL);

return 0;
}

14.3 运行结果

15. expm1,expm1f,expm1l

15.1 函数说明

函数声明 函数功能
double expm1 (double x); 计算 e 的 x 次方 减 1,即 (e^x) - 1 (double)
float expm1f (float x); 计算 e 的 x 次方 减 1,即 (e^x) - 1 (float)
long double expm1l (long double x); 计算 e 的 x 次方 减 1,即 (e^x) - 1 (long double)

15.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <math.h>

int main(void)
{
double result, result1, x = 4.0;
result = exp(x);
result1 = expm1(x);

float resultf, resultf1, xf = 4.0;
resultf = expf(xf);
resultf1 = expm1f(xf);

long double resultL, resultL1, xL = 4.0;
resultL = expl(xL);
resultL1 = expm1l(xL);

printf("\n'e' raised to the power of %lf (e ^ %lf) = %.20lf\n", x, x, result);
printf("\n'e' raised to the power of %lf minus one (e ^ %lf - 1) = %.20lf\n", x, x, result1);

printf("\n'e' raised to the power of %f (e ^ %f) = %.20f\n", xf, xf, resultf);
printf("\n'e' raised to the power of %f minus one (e ^ %f - 1) = %.20f\n", xf, xf, resultf1);

printf("\n'e' raised to the power of %Lf (e ^ %Lf) = %.20Lf\n", xL, xL, resultL);
printf("\n'e' raised to the power of %Lf minus one (e ^ %Lf - 1) = %.20Lf\n", xL, xL, resultL1);

return 0;
}

15.3 运行结果

16. erf,erff,erfl

16.1 函数说明

函数声明 函数功能
double erf (double x); 计算 x 的 误差函数(double)
float erff (float x); 计算 x 的 误差函数(float)
long double erfl (long double x); 计算 x 的 误差函数(long double)

16.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>

int main(void)
{
double result, x = 2.0;
result = erf(x); // 高斯误差函数

float resultf, xf = 2.0;
resultf = erff(xf);

long double resultL, xL = 2.0;
resultL = erfl(xL);

printf("the error function of %lf = %.20lf\n", x, result);
printf("the error function of %f = %.20f\n", xf, resultf);
printf("the error function of %Lf = %.20Lf\n", xL, resultL);

return 0;
}

16.3 运行结果

17. erfc,erfcf,erfcl

17.1 函数说明

函数声明 函数功能
double erfc (double x); 计算 x 的互补误差函数(double)
float erfcf (float x); 计算 x 的互补误差函数(float)
long double erfcl (long double x); 计算 x 的互补误差函数(long double)

17.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>

int main(void)
{
double result, x = 2.0;
result = erfc(x); // 互补误差函数

float resultf, xf = 2.0;
resultf = erfcf(xf);

long double resultL, xL = 2.0;
resultL = erfcl(xL);

printf("the complementary error function of %lf = %.20lf\n", x, result);
printf("the complementary error function of %f = %.20f\n", xf, resultf);
printf("the complementary error function of %Lf = %.20Lf\n", xL, resultL);

return 0;
}

17.3 运行结果

参考

  1. [API Reference Document]
  2. [ecvt 函数]
  3. [exec函数]

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

总览

函数声明 函数功能
void detectgraph(int *graphdriver, int *graphmode); 通过检测硬件确定图形驱动程序和模式
double difftime(time_t time2, time_t time1); 计算两个时刻之间的时间差
void disable(void); 屏蔽中断
div_t div(int number, int denom); 将两个整数相除, 返回商和余数
void drawpoly(int numpoints, int *polypoints); 画多边形
int dup(int handle); 复制文件描述符;若成功为新的文件描述,若出错为-1
int dup2(int oldhandle, int newhandle); 复制文件描述符;若成功为新的文件描述,若出错为-1。

1. detectgraph

1.1 函数说明

函数声明 函数功能
void detectgraph(int *graphdriver, int *graphmode); 通过检测硬件确定图形驱动程序和模式

1.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

/* names of the various cards supported */
char *dname[] = {
"requests detection",
"a CGA",
"an MCGA",
"an EGA",
"a 64K EGA",
"a monochrome EGA",
"an IBM 8514",
"a Hercules monochrome",
"an AT&T 6300 PC",
"a VGA",
"an IBM 3270 PC"
};

int main(void)
{
int gdriver, gmode, errorcode;

detectgraph(&gdriver, &gmode);

initgraph(&gdriver, &gmode, "");

errorcode = graphresult();

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

printf("You have [%s] video display card.\n", dname[gdriver]);
printf("Press any key to halt:");
getch();
return 0;
}

1.3 运行结果

2. difftime

2.1 函数说明

函数声明 函数功能
double difftime(time_t time2, time_t time1); 计算两个时刻之间的时间差

2.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <time.h>

int main(void)
{
time_t first, second; // time_t 相当于 long
first = time(NULL); // Gets system time
getchar();
second = time(NULL); // Gets system time again
printf("The difference is: %lf seconds\n", difftime(second, first));
return 0;
}

在上述的示例代码中,

  • 首先,我们定义了两个 time_t 类型的变量 firstsecond
  • 然后,调用 time(NULL) 函数获取当前的系统时间,并赋值给 first
  • 接着,调用 getchar() 函数等待用户输入,模拟延时的功能;
  • 再然后,继续调用 time(NULL) 函数获取当前的系统时间,并赋值给 second
  • 再接着,调用 difftime() 函数计算 firstsecond 之间的时间差【单位:秒】
  • 最终,输出时间差,并结束程序。

2.3 运行结果

3. disable

3.1 函数说明

函数声明 函数功能
void disable(void); 屏蔽中断

3.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 中断服务示例
#include <stdio.h>
#include <dos.h>
#include <conio.h>
// 定义一个宏INTR,代表时钟中断的十六进制向量值
#define INTR 0X1C
// 声明一个函数指针oldhandler,该指针指向一个没有参数且返回void的函数。
// 这种函数通常用于中断处理程序
void interrupt (*oldhandler)(void);

int count=0;

void interrupt handler(void)
{
// 禁用其他中断,确保此中断处理程序执行完毕前不再响应其他中断
disable();
count++;
// 启用其他中断
enable();
// 调用原先的中断处理程序
(*oldhandler)();
}

int main(void)
{
// 获取时钟中断的原始处理程序,并将其存储在oldhandler指向的函数中
oldhandler = getvect(INTR);
// 将时钟中断的处理程序设置为handler函数
setvect(INTR, handler);

while (count < 20)
printf("count is %d\n",count);
// 恢复时钟中断的原始处理程序
setvect(INTR, oldhandler);

return 0;
}

注意: 这个程序可能无法在现代操作系统上直接运行,因为其中的一些函数(如disable()enable()getvect()setvect())是特定于 DOS 的。如果你想在现代操作系统(如 LinuxWindows)上运行这个程序,你可能需要使用更现代的方法来处理中断或使用 DOS 模拟器。

4. div

4.1 函数说明

函数声明 函数功能
div_t div(int number, int denom); 将两个整数相除, 返回商和余数

4.2 演示示例

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <math.h>

int main(void)
{
div_t x = div(10,3);
// 商 和 余数
printf("10 div 3 = %d remainder %d\n", x.quot, x.rem);
return 0;
}

4.3 运行结果

5. drawpoly

5.1 函数说明

函数声明 函数功能
void drawpoly(int numpoints, int *polypoints); 画多边形

参数说明:

  • numpoints:多边形顶点的数量

  • polypoints:一个整数数组,包含多边形的各个顶点的坐标。

5.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <graphics.h>
#include <stdio.h>

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

// our polygon array
int poly[10];

// 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();
// terminate with an error code
exit(1);
}

maxx = getmaxx();
maxy = getmaxy();

poly[0] = 20;
poly[1] = maxy / 2;

poly[2] = maxx - 20;
poly[3] = 20;

poly[4] = maxx - 50;
poly[5] = maxy - 20;

poly[6] = maxx / 2;
poly[7] = maxy / 2;

// drawpoly doesn't automatically close the polygon, so we close it.

poly[8] = poly[0];
poly[9] = poly[1];

// draw the polygon
drawpoly(5, poly);

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

5.3 运行结果

6. dup

6.1 函数说明

函数声明 函数功能
int dup(int handle); 复制文件描述符;若成功为新的文件描述,若出错为-1

dup 返回的新文件描述符一定是当前可用文件描述中的最小数值。

6.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <fcntl.h>
#include <io.h>

void flush(FILE *stream);

int main(void)
{
FILE *fp;
char msg[] = "This is a test";

fp = fopen("STU.FIL", "w");

fwrite(msg, strlen(msg), 1, fp);

int handle;

handle = open("temp.txt", _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE);
printf("file hanlde : %d\n", handle);

printf("Press any key to flush STU.FIL:");
getchar();

flush(fp);

printf("\nFile was flushed, Press any key to quit:");
getchar();
return 0;
}

void flush(FILE *stream)
{
int duphandle;

fflush(stream);

duphandle = dup(fileno(stream));

printf("duplicate file hanlde : %d", duphandle);

close(duphandle);
}

上述代码可以简单总结如下:

  1. 首先,它打开一个名为"STU.FIL"的文件,并以写入模式打开。
  2. 然后,将字符串"This is a test"写入该文件。
  3. 接下来,它打开一个名为 "temp.txt" 的文件,并获取其文件句柄。
  4. 然后,提示用户按下任意键以刷新 "STU.FIL" 文件。
  5. 接着,调用自定义的flush函数来刷新文件缓冲区。
    • 首先调用fflush函数来刷新传入的文件流的缓冲区;
    • 然后,使用dup函数复制文件描述符,并将其存储在duphandle变量中;
    • 接着,打印出复制的文件句柄;
    • 最后,关闭复制的文件句柄。
  6. 最后,再次提示用户按下任意键以退出程序。

6.3 运行结果

7. dup2

7.1 函数说明

函数声明 函数功能
int dup2(int oldhandle, int newhandle); 复制文件描述符;若成功为新的文件描述,若出错为-1。

dup2 可以用 newhandle 参数指定新的描述符数值。如果 newhandle 已经打开,则先关闭。若 oldhandle = newhandle,则 dup2 返回 newhandle,而不关闭它。

7.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <sys\stat.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>

int main(void)
{
#define STDOUT 1

int handle, oldstdout;
char msg[] = "This is a test1";

handle = open("STU.FIL", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
printf("open file handle : %d\n", handle);

oldstdout = dup(STDOUT);
printf("dup file handle : %d", oldstdout);

dup2(handle, STDOUT);

close(handle);

write(STDOUT, msg, strlen(msg));

dup2(oldstdout, STDOUT);

close(oldstdout);

return 0;
}

上述代码简单分析如下:

  • 定义常量 STDOUT,其值为 1,表示标准输出的文件描述符;
  • 定义整型变量 handleoldstdout,以及字符数组 msg,用于存储要写入文件的字符串;
  • 使用 open 函数打开名为 "STU.FIL" 的文件,以创建和读写模式(O_CREAT | O_RDWR)打开,并设置文件权限为可读可写(S_IREAD | S_IWRITE);将返回的文件描述符赋值给 handle,并打印出来;
  • 使用 dup 函数备份当前的标准输出(STDOUT),将备份的文件描述符赋值给 oldstdout,并打印出来;
  • 使用 dup2 函数将标准输出重定向到 handle 指向的文件,即将后续的输出内容写入到 "STU.FIL" 文件中;
  • 关闭 handle 指向的文件句柄;
  • 使用 write 函数将 msg 字符串写入到标准输出(此时已经重定向到文件),长度为字符串的长度;
  • 使用 dup2 函数将标准输出恢复到备份的文件描述符 oldstdout,即将后续的输出内容输出到屏幕上。
  • 关闭 oldstdout 指向的文件句柄。

7.3 运行结果

参考

  1. [API Reference Document]

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

总览

函数声明 函数功能
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 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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 运行结果

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 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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 运行结果

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。

3.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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 运行结果

4. chdir

4.1 函数说明

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

4.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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 运行结果

5. chmod

5.1 函数说明

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

5.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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 运行结果

6. chsize

6.1 函数说明

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

6.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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 运行结果

7. circle

7.1 函数说明

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

7.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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 运行结果

8. cleardevice

8.1 函数说明

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

8.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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 运行结果

10. clearviewport

10.1 函数说明

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

10.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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 运行结果

12. clock

12.1 函数说明

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

12.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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 运行结果

13. closegraph

13.1 函数说明

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

13.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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 运行结果

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 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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 运行结果

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 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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 运行结果

16. creat

16.1 函数说明

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

16.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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 运行结果

17. ctime

17.1 函数说明

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

17.2 演示示例

1
2
3
4
5
6
7
8
9
10
#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 运行结果

参考

  1. [API Reference Document]

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

总览

函数声明 函数功能
double cabs (double complex z); 计算复数 z 的绝对值(double)
float cabsf (float complex z); 计算复数 z 的绝对值(float)
long double cabsl (long double complex z); 计算复数 z 的绝对值(long double)
double creal (double complex z); 计算复数z的实部(double)
float crealf (float complex z); 计算复数z的实部(float)
long double creall (long double complex z); 计算复数z的实部(long double)
double cimag (double complex z); 计算复数z的虚部(double)
float cimagf (float complex z); 计算复数z的虚部(float)
long double cimagl (long double complex z); 计算复数z的虚部(long double)
double carg (double complex z); 计算复数z的相位角 (double)
float cargf (float complex z); 计算复数z的相位角(float)
long double cargl (long double complex z); 计算复数z的相位角(long double)
double complex cacos (double complex z); 计算复数z的反余弦 (double complex)
float complex cacosf (float complex z); 计算复数z的反余弦(float complex)
long double complex cacosl (long double complex z); 计算复数z的反余弦(long double complex)
double complex cacosh (double complex z); 计算复数z的反双曲余弦(double complex)
float complex cacoshf (float complex z); 计算复数z的反双曲余弦(float complex)
long double complex cacoshl (long double complex z); 计算复数z的反双曲余弦(long double complex)
double complex casin (double complex z); 计算复数z的反正弦(double complex)
float complex casinf (float complex z); 计算复数z的反正弦(float complex)
long double complex casinl (long double complex z); 计算复数z的反正弦(long double complex)
double complex casinh (double complex z); 计算复数z的反双曲正弦(double complex)
float complex casinhf (float complex z); 计算复数z的反双曲正弦(float complex)
long double complex casinhl (long double complex z); 计算复数z的反双曲正弦(long double complex)
double complex catan (double complex z); 计算复数z的反正切(double complex)
float complex catanf (float complex z); 计算复数z的反正切(float complex)
long double complex catanl (long double complex z); 计算复数z的反正切(long double complex)
double complex catanh (double complex z); 计算复数z的反双曲正切(double complex)
float complex catanhf (float complex z); 计算复数z的反双曲正切(float complex)
long double complex catanhl (long double complex z); 计算复数z的反双曲正切(long double complex)
double complex ccos (double complex z); 计算复数z的余弦(double complex)
float complex ccosf (float complex z); 计算复数z的余弦(float complex)
long double complex ccosl (long double complex z); 计算复数z的余弦(long double complex)
double complex ccosh (double complex z); 计算复数z的双曲余弦(double complex)
float complex ccoshf (float complex z); 计算复数z的双曲余弦(float complex)
long double complex ccoshl (long double complex z); 计算复数z的双曲余弦(long double complex)
double complex csin (double complex z); 计算复数z的正弦(double complex)
float complex csinf (float complex z); 计算复数z的正弦(float complex)
long double complex csinl (long double complex z); 计算复数z的正弦(long double complex)
double complex csinh (double complex z); 计算复数z的双曲正弦(double complex)
float complex csinhf (float complex z); 计算复数z的双曲正弦(float complex)
long double complex csinhl (long double complex z); 计算复数z的双曲正弦(long double complex)
double complex ctan (double complex z); 计算复数z的正切(double complex)
float complex ctanf (float complex z); 计算复数z的正切(float complex)
long double complex ctanl (long double complex z); 计算复数z的正切(long double complex)
double complex ctanh (double complex z); 计算复数z的双曲正切(double complex)
float complex ctanhf (float complex z); 计算复数z的双曲正切(float complex)
long double complex ctanhl (long double complex z); 计算复数z的双曲正切(long double complex)
double complex cexp (double complex z); 计算复数z的指数基数e(double complex)
float complex cexpf (float complex z); 计算复数z的指数基数e(float complex)
long double complex cexpl (long double complex z); 计算复数z的指数基数e(long double complex)
double complex clog (double complex z); 计算复数z的自然对数(以e为底)(double complex)
float complex clogf (float complex z); 计算复数z的自然对数(以e为底)(float complex)
long double complex clogl (long double complex z); 计算复数z的自然对数(以e为底)(long double complex)
double complex conj (double complex z); 计算复数z的共轭(double complex)
float complex conjf (float complex z); 计算复数z的共轭(float complex)
long double complex conjl (long double complex z); 计算复数z的共轭(long double complex)
double complex cpow (double complex x, double complex y); 计算x的y次方值 (double complex)
float complex cpowf (float complex x, float complex y); 计算x的y次方值 (float complex)
long double complex cpowl (long double complex x, long double complex y); 计算x的y次方值 (double complex)
double complex cproj (double complex z); 计算复数z在黎曼球面上的投影(double complex)
float complex cprojf (float complex z); 计算复数z在黎曼球面上的投影(float complex)
long double complex cprojl (long double complex z); 计算复数z在黎曼球面上的投影(long double complex)
double complex csqrt (double complex z); 计算复数z的平方根(double complex)
float complex csqrtf (float complex z); 计算复数z的平方根(float complex)
long double complex csqrtl (long double complex z); 计算复数z的平方根(long double complex)

1. cabs,cabsf,cabsl

1.1 函数说明

函数声明 函数功能
double cabs (double complex z); 计算复数 z 的绝对值(double)
float cabsf (float complex z); 计算复数 z 的绝对值(float)
long double cabsl (long double complex z); 计算复数 z 的绝对值(long double)

1.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Huazie
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex z;
double x = 2.0, y = 2.0, val;
z = x + y * I; // I 代指 虚数单位 i
val = cabs(z); // 计算复数 z 的绝对值

float complex zf;
float xf = 2.0, yf = 2.0, valf;
zf = xf + yf * I;
valf = cabsf(zf);

long double complex zL;
long double xL = 2.0, yL = 2.0, valL;
zL = xL + yL * I;
valL = cabsl(zL);

printf("The absolute value of (%.4lf + %.4lfi) is %.20lf\n", x, y, val);
printf("The absolute value of (%.4f + %.4fi) is %.20f\n", xf, yf, valf);
printf("The absolute value of (%.4Lf + %.4Lfi) is %.20Lf", xL, yL, valL);

return 0;
}

1.3 运行结果

2. creal,crealf,creall

2.1 函数说明

函数声明 函数功能
double creal (double complex z); 计算复数z的实部(double)
float crealf (float complex z); 计算复数z的实部(float)
long double creall (long double complex z); 计算复数z的实部(long double)

2.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Huazie
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex z;
double x = 2.0, y = 1.0;
z = x + y * I; // I 代指 虚数单位 i

float complex zf;
float xf = 3.0, yf = 1.0;
zf = xf + yf * I;

long double complex zL;
long double xL = 4.0, yL = 1.0;
zL = xL + yL * I;

printf("The real part of (%.4lf + %.4lfi) is %.4lf\n", x, y, creal(z));
printf("The real part of (%.4f + %.4fi) is %.4f\n", xf, yf, crealf(zf));
printf("The real part of (%.4Lf + %.4Lfi) is %.4Lf", xL, yL, creall(zL));

return 0;
}

2.3 运行结果

3. cimag,cimagf,cimagl

3.1 函数说明

函数声明 函数功能
double cimag (double complex z); 计算复数z的虚部(double)
float cimagf (float complex z); 计算复数z的虚部(float)
long double cimagl (long double complex z); 计算复数z的虚部(long double)

3.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Huazie
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex z;
double x = 1.0, y = 2.0;
z = x + y * I; // I 代指 虚数单位 i

float complex zf;
float xf = 1.0, yf = 3.0;
zf = xf + yf * I;

long double complex zL;
long double xL = 1.0, yL = 4.0;
zL = xL + yL * I;

printf("The imaginary part of (%.4lf + %.4lfi) is %.4lf\n", x, y, cimag(z));
printf("The imaginary part of (%.4f + %.4fi) is %.4f\n", xf, yf, cimagf(zf));
printf("The imaginary part of (%.4Lf + %.4Lfi) is %.4Lf", xL, yL, cimagl(zL));

return 0;
}

3.3 运行结果

4. carg,cargf,cargl

4.1 函数说明

函数声明 函数功能
double carg (double complex z); 计算复数z的相位角 (double)
float cargf (float complex z); 计算复数z的相位角(float)
long double cargl (long double complex z); 计算复数z的相位角(long double)

相位角是描述波形在时间轴上的位置的一个重要参数,它决定了波形的起始位置和变化状态。在实际应用中,相位角的测量和控制对于电路设计和信号处理至关重要。通过对相位角的理解和应用,可以更好地分析和控制波动现象,从而实现对电力系统和通信系统的优化。

4.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex z;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i

float complex zf;
zf = 1.0f + 2.0f * I;

long double complex zL;
zL = (long double) 1.0 + (long double) 2.0 * I;

printf("The phase angle of (%.4lf + %.4lfi) is %.60lf\n", creal(z), cimag(z), carg(z));
printf("The phase angle of (%.4f + %.4fi) is %.60f\n", crealf(zf), cimagf(zf), cargf(zf));
printf("The phase angle of (%.4Lf + %.4Lfi) is %.60Lf", creall(zL), cimagl(zL), cargl(zL));

return 0;
}

4.3 运行结果

5. cacos,cacosf,cacosl

5.1 函数说明

函数声明 函数功能
double complex cacos (double complex z); 计算复数z的反余弦 (double complex)
float complex cacosf (float complex z); 计算复数z的反余弦(float complex)
long double complex cacosl (long double complex z); 计算复数z的反余弦(long double complex)

5.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcacos;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcacos = cacos(z); // 计算复数z的反余弦

float complex zf, zcacosf;
zf = 1.0f + 2.0f * I;
zcacosf = cacosf(zf);

long double complex zL, zcacosl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcacosl = cacosl(zL);

double zimag = cimag(zcacos);
float zimagf = cimagf(zcacosf);
long double zimagl = cimagl(zcacosl);
if (zimag < 0)
printf("The arc cosine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcacos), fabs(zimag));
else
printf("The arc cosine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcacos), zimag);

if (zimagf < 0)
printf("The arc cosine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcacosf), fabsf(zimagf));
else
printf("The arc cosine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcacosf), zimagf);

if (zimagl < 0)
printf("The arc cosine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcacosl), fabsl(zimagl));
else
printf("The arc cosine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcacosl), zimagl);
return 0;
}

5.3 运行结果

6. cacosh,cacoshf,cacoshl

6.1 函数说明

函数声明 函数功能
double complex cacosh (double complex z); 计算复数z的反双曲余弦(double complex)
float complex cacoshf (float complex z); 计算复数z的反双曲余弦(float complex)
long double complex cacoshl (long double complex z); 计算复数z的反双曲余弦(long double complex)

6.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcacosh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcacosh = cacosh(z); // 反双曲余弦

float complex zf, zcacoshf;
zf = 1.0f + 2.0f * I;
zcacoshf = cacoshf(zf);

long double complex zL, zcacoshl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcacoshl = cacoshl(zL);

double zimag = cimag(zcacosh);
float zimagf = cimagf(zcacoshf);
long double zimagl = cimagl(zcacoshl);
if (zimag < 0)
printf("The inverse hyperbolic cosine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcacosh), fabs(zimag));
else
printf("The inverse hyperbolic cosine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcacosh), zimag);

if (zimagf < 0)
printf("The inverse hyperbolic cosine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcacoshf), fabsf(zimagf));
else
printf("The inverse hyperbolic cosine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcacoshf), zimagf);

if (zimagl < 0)
printf("The inverse hyperbolic cosine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcacoshl), fabsl(zimagl));
else
printf("The inverse hyperbolic cosine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcacoshl), zimagl);
return 0;
}

6.3 运行结果

7. casin,casinf,casinl

7.1 函数说明

函数声明 函数功能
double complex casin (double complex z); 计算复数z的反正弦(double complex)
float complex casinf (float complex z); 计算复数z的反正弦(float complex)
long double complex casinl (long double complex z); 计算复数z的反正弦(long double complex)

7.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcasin;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcasin = casin(z); // 反正弦

float complex zf, zcasinf;
zf = 1.0f + 2.0f * I;
zcasinf = casinf(zf);

long double complex zL, zcasinl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcasinl = casinl(zL);

double zimag = cimag(zcasin);
float zimagf = cimagf(zcasinf);
long double zimagl = cimagl(zcasinl);
if (zimag < 0)
printf("The arcsine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcasin), fabs(zimag));
else
printf("The arcsine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcasin), zimag);

if (zimagf < 0)
printf("The arcsine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcasinf), fabsf(zimagf));
else
printf("The arcsine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcasinf), zimagf);

if (zimagl < 0)
printf("The arcsine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcasinl), fabsl(zimagl));
else
printf("The arcsine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcasinl), zimagl);
return 0;
}

7.3 运行结果

8. casinh,casinhf,casinhl

8.1 函数说明

函数声明 函数功能
double complex casinh (double complex z); 计算复数z的反双曲正弦(double complex)
float complex casinhf (float complex z); 计算复数z的反双曲正弦(float complex)
long double complex casinhl (long double complex z); 计算复数z的反双曲正弦(long double complex)

8.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcasinh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcasinh = casinh(z); // 反双曲正弦

float complex zf, zcasinhf;
zf = 1.0f + 2.0f * I;
zcasinhf = casinhf(zf);

long double complex zL, zcasinhl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcasinhl = casinhl(zL);

double zimag = cimag(zcasinh);
float zimagf = cimagf(zcasinhf);
long double zimagl = cimagl(zcasinhl);
if (zimag < 0)
printf("The inverse hyperbolic sine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcasinh), fabs(zimag));
else
printf("The inverse hyperbolic sine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcasinh), zimag);

if (zimagf < 0)
printf("The inverse hyperbolic sine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcasinhf), fabsf(zimagf));
else
printf("The inverse hyperbolic sine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcasinhf), zimagf);

if (zimagl < 0)
printf("The inverse hyperbolic sine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcasinhl), fabsl(zimagl));
else
printf("The inverse hyperbolic sine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcasinhl), zimagl);
return 0;
}

8.3 运行结果

9. catan,catanf,catanl

9.1 函数说明

函数声明 函数功能
double complex catan (double complex z); 计算复数z的反正切(double complex)
float complex catanf (float complex z); 计算复数z的反正切(float complex)
long double complex catanl (long double complex z); 计算复数z的反正切(long double complex)

9.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcatan;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcatan = catan(z); // 反正切

float complex zf, zcatanf;
zf = 1.0f + 2.0f * I;
zcatanf = catanf(zf);

long double complex zL, zcatanl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcatanl = catanl(zL);

double zimag = cimag(zcatan);
float zimagf = cimagf(zcatanf);
long double zimagl = cimagl(zcatanl);
if (zimag < 0)
printf("The arc tangent of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcatan), fabs(zimag));
else
printf("The arc tangent of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcatan), zimag);

if (zimagf < 0)
printf("The arc tangent of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcatanf), fabsf(zimagf));
else
printf("The arc tangent of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcatanf), zimagf);

if (zimagl < 0)
printf("The arc tangent of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcatanl), fabsl(zimagl));
else
printf("The arc tangent of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcatanl), zimagl);
return 0;
}

9.3 运行结果

10. catanh,catanhf,catanhl

10.1 函数说明

函数声明 函数功能
double complex catanh (double complex z); 计算复数z的反双曲正切(double complex)
float complex catanhf (float complex z); 计算复数z的反双曲正切(float complex)
long double complex catanhl (long double complex z); 计算复数z的反双曲正切(long double complex)

10.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcatanh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcatanh = catanh(z); // 反双曲正切

float complex zf, zcatanhf;
zf = 1.0f + 2.0f * I;
zcatanhf = catanhf(zf);

long double complex zL, zcatanhl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcatanhl = catanhl(zL);

double zimag = cimag(zcatanh);
float zimagf = cimagf(zcatanhf);
long double zimagl = cimagl(zcatanhl);
if (zimag < 0)
printf("The inverse hyperbolic tangent of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcatanh), fabs(zimag));
else
printf("The inverse hyperbolic tangent of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcatanh), zimag);

if (zimagf < 0)
printf("The inverse hyperbolic tangent of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcatanhf), fabsf(zimagf));
else
printf("The inverse hyperbolic tangent of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcatanhf), zimagf);

if (zimagl < 0)
printf("The inverse hyperbolic tangent of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcatanhl), fabsl(zimagl));
else
printf("The inverse hyperbolic tangent of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcatanhl), zimagl);
return 0;
}

10.3 运行结果

11. ccos,ccosf,ccosl

11.1 函数说明

函数声明 函数功能
double complex ccos (double complex z); 计算复数z的余弦(double complex)
float complex ccosf (float complex z); 计算复数z的余弦(float complex)
long double complex ccosl (long double complex z); 计算复数z的余弦(long double complex)

11.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zccos;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zccos = ccos(z); // 余弦

float complex zf, zccosf;
zf = 1.0f + 2.0f * I;
zccosf = ccosf(zf);

long double complex zL, zccosl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zccosl = ccosl(zL);

double zimag = cimag(zccos);
float zimagf = cimagf(zccosf);
long double zimagl = cimagl(zccosl);
if (zimag < 0)
printf("The cosine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zccos), fabs(zimag));
else
printf("The cosine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zccos), zimag);

if (zimagf < 0)
printf("The cosine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zccosf), fabsf(zimagf));
else
printf("The cosine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zccosf), zimagf);

if (zimagl < 0)
printf("The cosine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zccosl), fabsl(zimagl));
else
printf("The cosine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zccosl), zimagl);
return 0;
}

11.3 运行结果

12. ccosh,ccoshf,ccoshl

12.1 函数说明

函数声明 函数功能
double complex ccosh (double complex z); 计算复数z的双曲余弦(double complex)
float complex ccoshf (float complex z); 计算复数z的双曲余弦(float complex)
long double complex ccoshl (long double complex z); 计算复数z的双曲余弦(long double complex)

12.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zccosh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zccosh = ccosh(z); // 双曲余弦

float complex zf, zccoshf;
zf = 1.0f + 2.0f * I;
zccoshf = ccoshf(zf);

long double complex zL, zccoshl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zccoshl = ccoshl(zL);

double zimag = cimag(zccosh);
float zimagf = cimagf(zccoshf);
long double zimagl = cimagl(zccoshl);
if (zimag < 0)
printf("The hyperbolic cosine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zccosh), fabs(zimag));
else
printf("The hyperbolic cosine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zccosh), zimag);

if (zimagf < 0)
printf("The hyperbolic cosine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zccoshf), fabsf(zimagf));
else
printf("The hyperbolic cosine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zccoshf), zimagf);

if (zimagl < 0)
printf("The hyperbolic cosine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zccoshl), fabsl(zimagl));
else
printf("The hyperbolic cosine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zccoshl), zimagl);
return 0;
}

12.3 运行结果

13. csin,csinf,csinl

13.1 函数说明

函数声明 函数功能
double complex csin (double complex z); 计算复数z的正弦(double complex)
float complex csinf (float complex z); 计算复数z的正弦(float complex)
long double complex csinl (long double complex z); 计算复数z的正弦(long double complex)

13.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcsin;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcsin = csin(z); // 正弦

float complex zf, zcsinf;
zf = 1.0f + 2.0f * I;
zcsinf = csinf(zf);

long double complex zL, zcsinl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcsinl = csinl(zL);

double zimag = cimag(zcsin);
float zimagf = cimagf(zcsinf);
long double zimagl = cimagl(zcsinl);
if (zimag < 0)
printf("The sine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcsin), fabs(zimag));
else
printf("The sine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcsin), zimag);

if (zimagf < 0)
printf("The sine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsinf), fabsf(zimagf));
else
printf("The sine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsinf), zimagf);

if (zimagl < 0)
printf("The sine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcsinl), fabsl(zimagl));
else
printf("The sine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcsinl), zimagl);
return 0;
}

13.3 运行结果

14. csinh,csinhf,csinhl

14.1 函数说明

函数声明 函数功能
double complex csinh (double complex z); 计算复数z的双曲正弦(double complex)
float complex csinhf (float complex z); 计算复数z的双曲正弦(float complex)
long double complex csinhl (long double complex z); 计算复数z的双曲正弦(long double complex)

14.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcsinh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcsinh = csinh(z); // 双曲正弦

float complex zf, zcsinhf;
zf = 1.0f + 2.0f * I;
zcsinhf = csinhf(zf);

long double complex zL, zcsinhl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcsinhl = csinhl(zL);

double zimag = cimag(zcsinh);
float zimagf = cimagf(zcsinhf);
long double zimagl = cimagl(zcsinhl);
if (zimag < 0)
printf("The hyperbolic sine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcsinh), fabs(zimag));
else
printf("The hyperbolic sine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcsinh), zimag);

if (zimagf < 0)
printf("The hyperbolic sine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsinhf), fabsf(zimagf));
else
printf("The hyperbolic sine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsinhf), zimagf);

if (zimagl < 0)
printf("The hyperbolic sine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcsinhl), fabsl(zimagl));
else
printf("The hyperbolic sine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcsinhl), zimagl);
return 0;
}

14.3 运行结果

15. ctan,ctanf,ctanl

15.1 函数说明

函数声明 函数功能
double complex ctan (double complex z); 计算复数z的正切(double complex)
float complex ctanf (float complex z); 计算复数z的正切(float complex)
long double complex ctanl (long double complex z); 计算复数z的正切(long double complex)

15.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zctan;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zctan = ctan(z); // 正切

float complex zf, zctanf;
zf = 1.0f + 2.0f * I;
zctanf = ctanf(zf);

long double complex zL, zctanl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zctanl = ctanl(zL);

double zimag = cimag(zctan);
float zimagf = cimagf(zctanf);
long double zimagl = cimagl(zctanl);
if (zimag < 0)
printf("The tangent of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zctan), fabs(zimag));
else
printf("The tangent of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zctan), zimag);

if (zimagf < 0)
printf("The tangent of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zctanf), fabsf(zimagf));
else
printf("The tangent of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zctanf), zimagf);

if (zimagl < 0)
printf("The tangent of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zctanl), fabsl(zimagl));
else
printf("The tangent of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zctanl), zimagl);
return 0;
}

15.3 运行结果

16. ctanh,ctanhf,ctanhl

16.1 函数说明

函数声明 函数功能
double complex ctanh (double complex z); 计算复数z的双曲正切(double complex)
float complex ctanhf (float complex z); 计算复数z的双曲正切(float complex)
long double complex ctanhl (long double complex z); 计算复数z的双曲正切(long double complex)

16.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zctanh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zctanh = ctanh(z); // 双曲正切

float complex zf, zctanhf;
zf = 1.0f + 2.0f * I;
zctanhf = ctanhf(zf);

long double complex zL, zctanhl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zctanhl = ctanhl(zL);

double zimag = cimag(zctanh);
float zimagf = cimagf(zctanhf);
long double zimagl = cimagl(zctanhl);
if (zimag < 0)
printf("The inverse hyperbolic tangent of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zctanh), fabs(zimag));
else
printf("The inverse hyperbolic tangent of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zctanh), zimag);

if (zimagf < 0)
printf("The inverse hyperbolic tangent of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zctanhf), fabsf(zimagf));
else
printf("The inverse hyperbolic tangent of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zctanhf), zimagf);

if (zimagl < 0)
printf("The inverse hyperbolic tangent of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zctanhl), fabsl(zimagl));
else
printf("The inverse hyperbolic tangent of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zctanhl), zimagl);
return 0;
}

16.3 运行结果

17. cexp,cexpf,cexpl

17.1 函数说明

函数声明 函数功能
double complex cexp (double complex z); 计算复数z的指数基数e(double complex)
float complex cexpf (float complex z); 计算复数z的指数基数e(float complex)
long double complex cexpl (long double complex z); 计算复数z的指数基数e(long double complex)

17.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcexp;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcexp = cexp(z); // 指数基数e

float complex zf, zcexpf;
zf = 1.0f + 2.0f * I;
zcexpf = cexpf(zf);

long double complex zL, zcexpl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcexpl = cexpl(zL);

double zimag = cimag(zcexp);
float zimagf = cimagf(zcexpf);
long double zimagl = cimagl(zcexpl);
if (zimag < 0)
printf("The base-e exponential of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcexp), fabs(zimag));
else
printf("The base-e exponential of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcexp), zimag);

if (zimagf < 0)
printf("The base-e exponential of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcexpf), fabsf(zimagf));
else
printf("The base-e exponential of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcexpf), zimagf);

if (zimagl < 0)
printf("The base-e exponential of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcexpl), fabsl(zimagl));
else
printf("The base-e exponential of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcexpl), zimagl);
return 0;
}

17.3 运行结果

18. clog,clogf,clogl

18.1 函数说明

函数声明 函数功能
double complex clog (double complex z); 计算复数z的自然对数(以e为底)(double complex)
float complex clogf (float complex z); 计算复数z的自然对数(以e为底)(float complex)
long double complex clogl (long double complex z); 计算复数z的自然对数(以e为底)(long double complex)

18.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zclog;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zclog = clog(z); // 自然对数(以e为底)

float complex zf, zclogf;
zf = 1.0f + 2.0f * I;
zclogf = clogf(zf);

long double complex zL, zclogl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zclogl = clogl(zL);

double zimag = cimag(zclog);
float zimagf = cimagf(zclogf);
long double zimagl = cimagl(zclogl);
if (zimag < 0)
printf("The natural (base-e) logarithm of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zclog), fabs(zimag));
else
printf("The natural (base-e) logarithm of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zclog), zimag);

if (zimagf < 0)
printf("The natural (base-e) logarithm of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zclogf), fabsf(zimagf));
else
printf("The natural (base-e) logarithm of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zclogf), zimagf);

if (zimagl < 0)
printf("The natural (base-e) logarithm of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zclogl), fabsl(zimagl));
else
printf("The natural (base-e) logarithm of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zclogl), zimagl);
return 0;
}

18.3 运行结果

19. conj,conjf,conjl

19.1 函数说明

函数声明 函数功能
double complex conj (double complex z); 计算复数z的共轭(double complex)
float complex conjf (float complex z); 计算复数z的共轭(float complex)
long double complex conjl (long double complex z); 计算复数z的共轭(long double complex)

19.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zconj;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zconj = conj(z); // 共轭

float complex zf, zconjf;
zf = 1.0f + 2.0f * I;
zconjf = conjf(zf);

long double complex zL, zconjl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zconjl = conjl(zL);

double zimag = cimag(zconj);
float zimagf = cimagf(zconjf);
long double zimagl = cimagl(zconjl);
if (zimag < 0)
printf("The conjugate of (%.4lf + %.4lfi) is (%.4lf - %.4lfi)\n", creal(z), cimag(z), creal(zconj), fabs(zimag));
else
printf("The conjugate of (%.4lf + %.4lfi) is (%.4lf + %.4lfi)\n", creal(z), cimag(z), creal(zconj), zimag);

if (zimagf < 0)
printf("The conjugate of (%.4f + %.4fi) is (%.4f - %.4fi)\n", crealf(zf), cimagf(zf), crealf(zconjf), fabsf(zimagf));
else
printf("The conjugate of (%.4f + %.4fi) is (%.4f + %.4fi)\n", crealf(zf), cimagf(zf), crealf(zconjf), zimagf);

if (zimagl < 0)
printf("The conjugate of (%.4Lf + %.4Lfi) is (%.4Lf - %.4Lfi)", creall(zL), cimagl(zL), creall(zconjl), fabsl(zimagl));
else
printf("The conjugate of (%.4Lf + %.4Lfi) is (%.4Lf + %.4Lfi)", creall(zL), cimagl(zL), creall(zconjl), zimagl);
return 0;
}

19.3 运行结果

20. cpow,cpowf,cpowl

20.1 函数说明

函数声明 函数功能
double complex cpow (double complex x, double complex y); 计算x的y次方值 (double complex)
float complex cpowf (float complex x, float complex y); 计算x的y次方值 (float complex)
long double complex cpowl (long double complex x, long double complex y); 计算x的y次方值 (double complex)

20.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex x, y, z;
x = 1.0 + 2.0 * I; // I 代指 虚数单位 i
y = 2.0 + 1.0 * I;
z = cpow(x, y); // x的y次方值

float complex xf, yf, zf;
xf = 1.0f + 2.0f * I;
yf = 2.0f + 1.0f * I;
zf = cpowf(xf, yf);

long double complex xL, yL, zL;
xL = (long double) 1.0 + (long double) 2.0 * I;
yL = (long double) 2.0 + (long double) 1.0 * I;
zL = cpowl(xL, yL);

double zimag = cimag(z);
float zimagf = cimagf(zf);
long double zimagl = cimagl(zL);
if (zimag < 0)
printf("the value of (%.4lf + %.4lfi) raised to the (%.4lf + %.4lfi) power is (%.20lf - %.20lfi)\n",
creal(x), cimag(x), creal(y), cimag(y), creal(z), fabs(zimag));
else
printf("the value of (%.4lf + %.4lfi) raised to the (%.4lf + %.4lfi) power is (%.20lf + %.20lfi)\n",
creal(x), cimag(x), creal(y), cimag(y), creal(z), zimag);

if (zimagf < 0)
printf("the value of (%.4f + %.4fi) raised to the (%.4f + %.4fi) power is (%.20f - %.20fi)\n",
crealf(xf), cimagf(xf), crealf(yf), cimagf(yf), crealf(zf), fabs(zimagf));
else
printf("the value of (%.4f + %.4fi) raised to the (%.4f + %.4fi) power is (%.20f + %.20fi)\n",
crealf(xf), cimagf(xf), crealf(yf), cimagf(yf), crealf(zf), zimagf);
if (zimagl < 0)
printf("the value of (%.4Lf + %.4Lfi) raised to the (%.4Lf + %.4Lfi) power is (%.20Lf - %.20Lfi)\n",
creall(xL), cimagl(xL), creall(yL), cimagl(yL), creall(zL), fabs(zimagl));
else
printf("the value of (%.4Lf + %.4Lfi) raised to the (%.4Lf + %.4Lfi) power is (%.20Lf + %.20Lfi)\n",
creall(xL), cimagl(xL), creall(yL), cimagl(yL), creall(zL), zimagl);
return 0;
}

20.3 运行结果

21. cproj,cprojf,cprojl

21.1 函数说明

函数声明 函数功能
double complex cproj (double complex z); 计算复数z在黎曼球面上的投影(double complex)
float complex cprojf (float complex z); 计算复数z在黎曼球面上的投影(float complex)
long double complex cprojl (long double complex z); 计算复数z在黎曼球面上的投影(long double complex)

黎曼球面上的投影是一种将三维空间中的黎曼球面与二维复平面通过立体投影方式建立一一对应关系的映射。

21.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcproj;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcproj = cproj(z); // 计算复数z在黎曼球面上的投影

float complex zf, zcprojf;
zf = NAN + INFINITY * I;
zcprojf = cprojf(zf);

long double complex zL, zcprojl;
zL = INFINITY + (long double) 3.0 * I;
zcprojl = cprojl(zL); // 结果相当于 INFINITY + i*copysign(0.0, cimag(z)).

double zimag = cimag(zcproj);
float zimagf = cimagf(zcprojf);
long double zimagl = cimagl(zcprojl);
if (zimag < 0)
printf("The projection of the (%.4lf + %.4lf i) onto the Riemann sphere is (%.4lf - %.4lf i)\n", creal(z), cimag(z), creal(zcproj), fabs(zimag));
else
printf("The projection of the (%.4lf + %.4lf i) onto the Riemann sphere is (%.4lf + %.4lf i)\n", creal(z), cimag(z), creal(zcproj), zimag);

if (zimagf < 0)
printf("The projection of the (%.4f + %.4f i) onto the Riemann sphere is (%.4f - %.4f i)\n", crealf(zf), cimagf(zf), crealf(zcprojf), fabsf(zimagf));
else
printf("The projection of the (%.4f + %.4f i) onto the Riemann sphere is (%.4f + %.4f i)\n", crealf(zf), cimagf(zf), crealf(zcprojf), zimagf);

if (zimagl < 0)
printf("The projection of the (%.4Lf + %.4Lf i) onto the Riemann sphere is (%.4Lf - %.4Lf i)", creall(zL), cimagl(zL), creall(zcprojl), fabsl(zimagl));
else
printf("The projection of the (%.4Lf + %.4Lf i) onto the Riemann sphere is (%.4Lf + %.4Lf i)", creall(zL), cimagl(zL), creall(zcprojl), zimagl);
return 0;
}

21.3 运行结果

22. csqrt,csqrtf,csqrtl

22.1 函数说明

函数声明 函数功能
double complex csqrt (double complex z); 计算复数z的平方根(double complex)
float complex csqrtf (float complex z); 计算复数z的平方根(float complex)
long double complex csqrtl (long double complex z); 计算复数z的平方根(long double complex)

22.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcsqrt;
z = 9.0 + 9.0 * I; // I 代指 虚数单位 i
zcsqrt = csqrt(z); // 平方根

float complex zf, zcsqrtf;
zf = 4.0f + 4.0f * I;
zcsqrtf = csqrtf(zf);

long double complex zL, zcsqrtl;
zL = (long double) 16.0 + (long double) 16.0 * I;
zcsqrtl = csqrtl(zL);

double zimag = cimag(zcsqrt);
float zimagf = cimagf(zcsqrtf);
long double zimagl = cimagl(zcsqrtl);
if (zimag < 0)
printf("The square root of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcsqrt), fabs(zimag));
else
printf("The square root of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcsqrt), zimag);

if (zimagf < 0)
printf("The square root of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsqrtf), fabsf(zimagf));
else
printf("The square root of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsqrtf), zimagf);

if (zimagl < 0)
printf("The square root of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcsqrtl), fabsl(zimagl));
else
printf("The square root of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcsqrtl), zimagl);
return 0;
}

22.3 运行结果

参考

  1. 【MATH-标准C库】

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

总览

函数声明 函数功能
void bar(int left, int top, int right, int bottom); 画一个二维条形图
void bar3d(int left, int top, int right, int bottom, int depth, int topflag); 画一个三维条形图
int bdos(int dosfun, unsigned dosdx, unsigned dosal); DOS系统调用
int bdosptr(int dosfun, void *argument, unsigned dosal); DOS系统调用
int bioscom(int cmd, char abyte, int port); 串行I/O通信
int biosdisk(int cmd, int drive, int head, int track, int sector, int nsects, void *buffer); 软硬盘I/O
int bioskey(int cmd); 直接使用BIOS服务的键盘接口
int biosmemory(void); 返回存储块大小,以K为单位
int biosprint(int cmd, int byte, int port); 直接使用BIOS服务的打印机I/O
long biostime(int cmd, long newtime); 读取或设置BIOS时间
int brk(void *endds); 用来改变分配给调用程序的数据段的空间数量
void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *)); 二分法搜索

1. bar

1.1 函数说明

函数声明 函数功能
void bar(int left, int top, int right, int bottom); 画一个二维条形图

关注点: 绘制二维条形图需要左上角和右下角的坐标。 left 指定左上角的 X 坐标,top 指定左上角的 Y 坐标,right 指定右下角的 X 坐标,bottom 指定右下角的 Y 坐标。 当前填充图案和填充颜色用于填充条形图。 要更改填充图案和填充颜色,请使用 setfillstyle

1.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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, i;

/* 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;

/* loop through the fill patterns */
for (i=SOLID_FILL; i<USER_FILL; i++)
{
/* set the fill style */
setfillstyle(i, getmaxcolor());

/* draw the bar */
bar(midx-50, midy-50, midx+50, midy+50);

getch();
}

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

1.3 运行结果

2. bar3d

2.1 函数说明

函数声明 函数功能
void bar3d(int left, int top, int right, int bottom, int depth, int topflag); 画一个三维条形图

关注点: 绘制三维条形图需要条形左上角和右下角的坐标。 left 指定左上角的 X 坐标,top 指定左上角的 Y 坐标,right 指定右下角的 X 坐标,bottom 指定右下角的 Y 坐标,depth 指定条的深度 以像素为单位,topflag 确定是否将 3 维顶部放置在条形图上(如果它不为零,则放置否则不放置)。 当前填充图案和填充颜色用于填充条形图。 要更改填充图案和填充颜色,请使用 setfillstyle

2.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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, i;

/* initialize graphics, 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 error code */
}

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

/* loop through the fill patterns */
for (i=EMPTY_FILL; i<USER_FILL; i++)
{
/* set the fill style */
setfillstyle(i, getmaxcolor());

/* draw the 3-d bar */
bar3d(midx-50, midy-50, midx+50, midy+50, 10, 1);

getch();
}

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

2.3 运行结果

3. bdos

3.1 函数说明

函数声明 函数功能
int bdos(int dosfun, unsigned dosdx, unsigned dosal); DOS系统调用

3.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <dos.h>

/* Get current drive as 'A', 'B', ... */
char current_drive(void)
{
char curdrive;

/* Get current disk as 0, 1, ... */
curdrive = bdos(0x19, 0, 0);
return('A' + curdrive);
}

int main(void)
{
printf("The current drive is %c:\n", current_drive());
return 0;
}

4. bdosptr

4.1 函数说明

函数声明 函数功能
int bdosptr(int dosfun, void *argument, unsigned dosal); DOS系统调用

4.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string.h>
#include <stdio.h>
#include <dir.h>
#include <dos.h>
#include <errno.h>
#include <stdlib.h>

#define BUFLEN 80

int main(void)
{
char buffer[BUFLEN];
int test;

printf("Enter full pathname of a directory\n");
gets(buffer);

test = bdosptr(0x3B,buffer,0);
if(test)
{
printf("DOS error message: %d\n", errno);
/* See errno.h for error listings */
exit (1);
}

getcwd(buffer, BUFLEN);
printf("The current directory is: %s\n", buffer);

return 0;
}

5. bioscom

5.1 函数说明

函数声明 函数功能
int bioscom(int cmd, char abyte, int port); 串行I/O通信

5.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bios.h>
#include <conio.h>

#define COM1 0
#define DATA_READY 0x100
#define TRUE 1
#define FALSE 0

#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)

int main(void)
{
int in, out, status, DONE = FALSE;

bioscom(0, SETTINGS, COM1);
cprintf("... BIOSCOM [ESC] to exit ...\n");
while (!DONE)
{
status = bioscom(3, 0, COM1);
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)
putch(out);
if (kbhit())
{
if ((in = getch()) == '\x1B')
DONE = TRUE;
bioscom(1, in, COM1);
}
}
return 0;
}

6. biosdisk

6.1 函数说明

函数声明 函数功能
int biosdisk(int cmd, int drive, int head, int track, int sector, int nsects, void *buffer); 软硬盘I/O

6.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bios.h>
#include <stdio.h>

int main(void)
{
int result;
char buffer[512];

printf("Testing to see if drive a: is ready\n");
result = biosdisk(4,0,0,0,0,1,buffer);
result &= 0x02;
(result) ? (printf("Drive A: Ready\n")) : (printf("Drive A: Not Ready\n"));

return 0;
}

7. bioskey

7.1 函数说明

函数声明 函数功能
int bioskey(int cmd); 直接使用BIOS服务的键盘接口

7.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <bios.h>
#include <ctype.h>

#define RIGHT 0x01
#define LEFT 0x02
#define CTRL 0x04
#define ALT 0x08

int main(void)
{
int key, modifiers;

/* function 1 returns 0 until a key is pressed */
while (bioskey(1) == 0);

/* function 0 returns the key that is waiting */
key = bioskey(0);

/* use function 2 to determine if shift keys were used */
modifiers = bioskey(2);
if (modifiers)
{
printf("[");
if (modifiers & RIGHT) printf("RIGHT");
if (modifiers & LEFT) printf("LEFT");
if (modifiers & CTRL) printf("CTRL");
if (modifiers & ALT) printf("ALT");
printf("]");
}
/* print out the character read */
if (isalnum(key & 0xFF))
printf("'%c'\n", key);
else
printf("%#02x\n", key);
return 0;
}

8. biosmemory

8.1 函数说明

函数声明 函数功能
int biosmemory(void); 返回存储块大小,以K为单位

8.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <bios.h>

int main(void)
{
int memory_size;

memory_size = biosmemory(); /* returns value up to 640K */
printf("RAM size = %dK\n",memory_size);
return 0;
}

9. biosprint

9.1 函数说明

函数声明 函数功能
int biosprint(int cmd, int byte, int port); 直接使用BIOS服务的打印机I/O

9.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <conio.h>
#include <bios.h>

int main(void)
{
#define STATUS 2 /* printer status command */
#define PORTNUM 0 /* port number for LPT1 */

int status, abyte=0;

printf("Please turn off your printer. Press any key to continue\n");
getch();
status = biosprint(STATUS, abyte, PORTNUM);
if (status & 0x01)
printf("Device time out.\n");
if (status & 0x08)
printf("I/O error.\n");

if (status & 0x10)
printf("Selected.\n");
if (status & 0x20)
printf("Out of paper.\n");

if (status & 0x40)
printf("Acknowledge.\n");
if (status & 0x80)
printf("Not busy.\n");

return 0;
}

10. biostime

10.1 函数说明

函数声明 函数功能
long biostime(int cmd, long newtime); 读取或设置BIOS时间

10.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <bios.h>
#include <time.h>
#include <conio.h>

int main(void)
{
long bios_time;

clrscr();
cprintf("The number of clock ticks since midnight is:\r\n");
cprintf("The number of seconds since midnight is:\r\n");
cprintf("The number of minutes since midnight is:\r\n");
cprintf("The number of hours since midnight is:\r\n");
cprintf("\r\nPress any key to quit:");
while(!kbhit())
{
bios_time = biostime(0, 0L);

gotoxy(50, 1);
cprintf("%lu", bios_time);

gotoxy(50, 2);
cprintf("%.4f", bios_time / CLK_TCK);

gotoxy(50, 3);
cprintf("%.4f", bios_time / CLK_TCK / 60);

gotoxy(50, 4);
cprintf("%.4f", bios_time / CLK_TCK / 3600);
}
return 0;
}

11. brk

11.1 函数说明

函数声明 函数功能
int brk(void *endds); 用来改变分配给调用程序的数据段的空间数量

11.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <alloc.h>

int main(void)
{
char *ptr;

printf("Changing allocation with brk()\n");
ptr = malloc(1);
printf("Before brk() call: %lu bytes free\n", coreleft());
brk(ptr+1000);
printf(" After brk() call: %lu bytes free\n", coreleft());
return 0;
}

12. bsearch

12.1 函数说明

函数声明 函数功能
void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *)); 二分法搜索

12.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdlib.h>
#include <stdio.h>

#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))

int numarray[] = {1, 2, 3, 5, 6, 8, 9, 10, 12, 14};

int numeric (const int *p1, const int *p2)
{
return(*p1 - *p2);
}

int lookup(int key)
{
int *itemptr;

/* The cast of (int(*)(const void *,const void*)) is needed to avoid a type mismatch error at compile time */
itemptr = (int(*))bsearch(&key, numarray, NELEMS(numarray), sizeof(int), (int(*)(const void *,const void *))numeric);
return (itemptr != NULL);
}

int main(void)
{
int a;
printf("Please input key: ");
scanf("%d", &a);
if (lookup(a))
printf("%d is in the table.\n", a);
else
printf("%d isn't in the table.\n", a);
return 0;
}

12.3 运行结果


参考

  1. [API Reference Document]
  2. [c语言中的 bar 函数]
  3. [c语言中的 bar3d 函数]

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

总览

函数声明 函数功能
void abort(void); 异常终止一个进程
int abs(int i); 求整数的绝对值
int absread(int drive, int nsects, int sectno, void *buffer); 从drive指定的驱动器磁盘上,sectno指定的逻辑扇区号开始读取nsects个(最多64K个)扇区的内容,储存于buffer所指的缓冲区中。
int abswrite(int drive, int nsects, int sectno, void *buffer); 将指定内容写入磁盘上的指定扇区
int access(const char *filename, int amode); 确定文件的访问权限
double acos(double x); 反余弦函数
int allocmem(unsigned size, unsigned *seg); 分配DOS存储段
void arc(int x, int y, int stangle, int endangle, int radius); 画一弧线
char *asctime(const struct tm *tblock); 转换日期和时间为ASCII码
double asin(double x); 反正弦函数
void assert(int test); 测试一个条件并可能使程序终止
double atan(double x); 反正切函数
double atan2(double y, double x); 计算Y/X的反正切值
int atexit(atexit_t func); 注册终止函数
double atof(const char *nptr); 把字符串转换成浮点数
int atoi(const char *nptr); 把字符串转换成整型数
long atol(const char *nptr); 把字符串转换成长整型数

1. abort

1.1 函数说明

函数声明 函数功能
void abort(void); 异常终止一个进程

注意: abort() 函数用于终止当前程序的执行。当程序调用 abort() 函数时,它会立即退出,并生成一个错误信号,通知操作系统程序非正常终止。如果程序已经打开了一些文件或句柄,但尚未关闭它们,则这些资源可能无法被正确释放。

1.2 演示示例

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("Calling abort()\n");
abort();
printf("already abort()"); // 这里永远也到不了
return 0;
}

1.3 运行结果

2. abs

2.1 函数说明

函数声明 函数功能
int abs(int i); 求整数的绝对值

2.2 演示示例

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <math.h>

int main(void)
{
int number = -666;
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}

2.3 运行结果

3. absread

3.1 函数说明

函数声明 函数功能
int absread(int drive, int nsects, int sectno, void *buffer); 从drive指定的驱动器磁盘上,sectno指定的逻辑扇区号开始读取nsects个(最多64K个)扇区的内容,储存于buffer所指的缓冲区中。

3.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dos.h>

int main(void)
{
int i, strt, ch_out, sector;
char buf[512];

printf("Insert a diskette into drive A and press any key\n");
getch();
sector = 0;
if (absread(0, 1, sector, &buf) != 0)
{
perror("Disk problem");
exit(1);
}
printf("Read OK\n");
strt = 3;
for (i=0; i<80; i++)
{
ch_out = buf[strt+i];
putchar(ch_out);
}
printf("\n");
return(0);
}

上述的代码实现了从 A 驱动器读取一个扇区的数据,并将其中一些字符输出到屏幕上。

  • 首先提示用户插入一个软盘到 A 驱动器中。
  • 然后读取 A 驱动器上第 0 个扇区的数据到缓冲区 buf 中。
  • 接着检查读取是否成功。如果不成功,输出错误信息并退出程序。
  • 最后将 buf 缓冲区中偏移量为 3 到偏移量为 82 的字符依次输出到屏幕上。

注意: 程序中使用了一些 DOS 特定的函数,比如 absread()getch(),可能不适用于其他操作系统或编译器环境。

4. abswrite

4.1 函数说明

函数声明 函数功能
int abswrite(int drive, int nsects, int sectno, void *buffer); 将指定内容写入磁盘上的指定扇区

4.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <dos.h>
#include <stdio.h>

unsigned char buff[512];

int main()
{
int i;
char c;
printf("\nQuick Format 1.44MB\n");
printf("Program by ChenQingyang.\n");
printf("ALL DATA IN THE FLOPPY DISK WILL BE LOST!!\n");
printf("\nInsert a diskette for drive A:\n");
printf("and press ENTER when ready. . .");
c=getchar();
printf("\n\nCleaning FAT area. . .");
buff[0]=0xf0;
buff[1]=buff[2]=0xff;
for (i=3;i<512;i++)
buff[i]=0;
abswrite(0,1,1,buff);
abswrite(0,1,10,buff);
for (i=0;i<512;i++)
buff[i]=0;
for (i=2;i<10;i++)
abswrite (0,1,i,buff);
for (i=11;i<19;i++)
abswrite (0,1,i,buff);
printf("\nCleaning ROOT area. . .");
for (i=19;i<33;i++)
abswrite (0,1,i,buff);
printf("\n\nQuickFormat Completed!\n");
}

上述代码是一个使用 DOS 命令格式化软盘的程序。它会提示用户输入软盘,然后清空软盘的FAT和根目录区域,并在完成后打印 “QuickFormat Completed!” 的信息。程序使用了 <dos.h><stdio.h> 头文件,其中包含了一些 DOS 和标准输入输出函数。

5. access

5.1 函数说明

函数声明 函数功能
int access(const char *filename, int amode); 确定文件的访问权限

5.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <io.h>

int file_exists(char *filename);

int main(void)
{
printf("Does students1.txt exist: %s\n",
file_exists("students1.txt") ? "YES" : "NO");
return 0;
}

int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}

5.3 运行结果


6. acos

6.1 函数说明

函数声明 函数功能
double acos(double x); 反余弦函数

6.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <math.h>

int main(void)
{
double result;
double x = 0.5;

result = acos(x);
printf("The arc cosine of %lf is %lf\n", x, result);
return 0;
}

6.3 运行结果

7. allocmem

7.1 函数说明

函数声明 函数功能
int allocmem(unsigned size, unsigned *seg); 分配DOS存储段

7.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <dos.h>
#include <alloc.h>
#include <stdio.h>

int main(void)
{
unsigned int size, segp;
int stat;
size = 64; /* (64 x 16) = 1024 bytes */
stat = allocmem(size, &segp);
if (stat == -1)
printf("Allocated memory at segment: %x\n", segp);
else
printf("Failed: maximum number of paragraphs available is %u\n",stat);
return 0;
}

在上述的示例代码,

  • 首先调用了 allocmem() 函数来分配内存,其中传递了两个参数:size 表示请求的内存大小(以段为单位),这里设置为 64 段;&segp 表示返回的内存段地址将存储在此变量中。
  • 如果成功分配内存,allocmem()函数将返回 -1,并打印出已分配内存的段地址;
  • 否则,它将返回最大可用段数,并打印出失败的消息。

8. arc

8.1 函数说明

函数声明 函数功能
void arc(int x, int y, int stangle, int endangle, int radius); 画一弧线

8.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

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

int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;

/* initialize graphics and local variables */
char ch[] = "";
initgraph(&gdriver, &gmode, ch);

/* read result of initialization */
errorcode = graphresult(); /* an error occurred */
if (errorcode != grOk)
{
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 arc */
arc(midx, midy, stangle, endangle, radius);

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

8.3 运行结果

9. asctime

9.1 函数说明

函数声明 函数功能
char *asctime(const struct tm *tblock); 转换日期和时间为ASCII码

9.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void)
{
struct tm t;
char str[80];

/* sample loading of tm structure */

t.tm_sec = 1; /* Seconds */
t.tm_min = 30; /* Minutes */
t.tm_hour = 9; /* Hour */
t.tm_mday = 22; /* Day of the Month */
t.tm_mon = 11; /* Month */
t.tm_year = 56; /* Year - does not include century */
t.tm_wday = 4; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */

/* converts structure to null terminated
string */

strcpy(str, asctime(&t));
printf("%s\n", str);

return 0;
}

9.3 运行结果

10. asin

10.1 函数说明

函数声明 函数功能
double asin(double x); 反正弦函数

10.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <math.h>

int main(void)
{
double result;
double x = 0.5;

result = asin(x);
printf("The arc sin of %lf is %lf\n", x, result);
return(0);
}

10.3 运行结果

11. assert

11.1 函数说明

函数声明 函数功能
void assert(int test); 测试一个条件并可能使程序终止

11.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct ITEM {
int key;
int value;
};

/* add item to list, make sure list is not null */
void additem(struct ITEM *itemptr) {
assert(itemptr != NULL);
/* add item to list */
}

int main(void)
{
additem(NULL);
return 0;
}

11.3 运行结果

12. atan

12.1 函数说明

函数声明 函数功能
double atan(double x); 反正切函数

12.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <math.h>

int main(void)
{
double result;
double x = 0.5;

result = atan(x);
printf("The arc tangent of %lf is %lf\n", x, result);
return(0);
}

12.3 运行结果

13. atan2

13.1 函数说明

函数声明 函数功能
double atan2(double y, double x); 计算Y/X的反正切值

13.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <math.h>

int main(void)
{
double result;
double x = 60.0, y = 30.0;

result = atan2(y, x);
printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);
return 0;
}

13.3 运行结果

14. atexit

14.1 函数说明

函数声明 函数功能
int atexit(atexit_t func); 注册终止函数

14.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>

void exit_fn1(void)
{
printf("Exit function #1 called\n");
}

void exit_fn2(void)
{
printf("Exit function #2 called\n");
}

int main(void)
{
/* post exit function #1 */
atexit(exit_fn1);
/* post exit function #2 */
atexit(exit_fn2);
return 0;
}

14.3 运行结果

15. atof

15.1 函数说明

函数声明 函数功能
double atof(const char *nptr); 把字符串转换成浮点数

15.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
float f;
char *str = "1234.5678";

f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}

15.3 运行结果

16. atoi

16.1 函数说明

函数声明 函数功能
int atoi(const char *nptr); 把字符串转换成整型数

16.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int n;
char *str = "1234.5678";

n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}

16.3 运行结果

17. atol

17.1 函数说明

函数声明 函数功能
long atol(const char *nptr); 把字符串转换成长整型数

17.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
long l;
char *lstr = "87654321";

l = atol(lstr);
printf("string = %s integer = %ld\n", lstr, l);
return(0);
}

17.3 运行结果

参考

  1. [API Reference Document]