当前位置: 首页 > news >正文

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

C语言函数大全

本篇介绍C语言函数大全–d开头的函数

1. detectgraph

1.1 函数说明

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

1.2 演示示例

#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)
{/* returns detected hardware info. */int gdriver, gmode, errorcode;/* detect graphics hardware available */detectgraph(&gdriver, &gmode);/* initialize graphics and local variables */initgraph(&gdriver, &gmode, "");/* read result of detectgraph call */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 */}/* display the information detected */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 演示示例

#include <stdio.h>
#include <time.h>int main(void)
{time_t first, second; // time_t 相当于 longfirst = 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;
}

2.3 运行结果

在这里插入图片描述

3. div

3.1 函数说明

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

3.2 演示示例

#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;
}

3.3 运行结果

在这里插入图片描述

4. drawpoly

4.1 函数说明

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

4.2 演示示例

#include <graphics.h>
#include <stdio.h>int main(void)
{// request auto detectionint gdriver = DETECT, gmode, errorcode;int maxx, maxy;// our polygon arrayint poly[10];// initialize graphics and local variablesinitgraph(&gdriver, &gmode, "");// read result of initializationerrorcode = 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 codeexit(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 polygondrawpoly(5, poly);// clean upgetch();closegraph();return 0;
}

4.3 运行结果

在这里插入图片描述

5. dup

5.1 函数说明

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

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

5.2 演示示例

#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";/* create a file */fp = fopen("STU.FIL", "w");/* write some data to the file */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 the data to STU.FIL without closing it */flush(fp);printf("\nFile was flushed, Press any key to quit:");getchar();return 0;
}void flush(FILE *stream)
{int duphandle;/* flush TC's internal buffer */fflush(stream);/* make a duplicate file handle */duphandle = dup(fileno(stream));printf("duplicate file hanlde : %d", duphandle);/* close the duplicate handle to flush the DOS buffer */close(duphandle);
}

5.3 运行结果

在这里插入图片描述

6. dup2

6.1 函数说明

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

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

6.2 演示示例

#include <sys\stat.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>int main(void)
{#define STDOUT 1int handle, oldstdout;char msg[] = "This is a test1";/* create a file */handle = open("STU.FIL", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);printf("open file handle : %d\n", handle);/* create a duplicate handle for standard output */oldstdout = dup(STDOUT);printf("dup file handle : %d", oldstdout);/*redirect standard output to STU.FIL by duplicating the file handle onto the file handle for standard output.*/dup2(handle, STDOUT);/* close the handle for STU.FIL */close(handle);/* will be redirected into STU.FIL */write(STDOUT, msg, strlen(msg));/* restore original standard output handle */dup2(oldstdout, STDOUT);/* close duplicate handle for STDOUT */close(oldstdout);return 0;
}

6.3 运行结果

在这里插入图片描述

参考

  1. [API Reference Document]
http://www.lryc.cn/news/45006.html

相关文章:

  • 基于springboot实现福聚苑社区团购演示【项目源码】
  • 动静态库的制作
  • QMS-云质-质量软件-客诉,为什么应该用两段式来处理
  • JS:关于邮箱的正则表达式及规则
  • 两句话,ChatGPT帮我写一个打飞机的游戏
  • 计算机图形学14:三维图形的投影变换
  • 【ChatGPT4】王老师零基础《NLP》(自然语言处理)第二课
  • 设计模式之中介者模式在前端的应用
  • 2023年还能入行程序员吗?工作3年以上的黑马老学员怎么说?
  • 接收机的噪声来源与噪声分析
  • Android FrameWork——SystemServer
  • 婴儿推车ASTMF883测试
  • 射频接收机概述
  • 实验三Numpy知识点总结
  • Code Review时学到的技巧之isAssignableFrom
  • IP协议以及相关技术
  • SpringBoot 项目使用 Sa-Token 完成登录认证
  • javaScript 蓝桥杯----梅楼封的一天
  • 谷粒商城笔记+踩坑(18)——购物车
  • 进阶C语言:指针笔试题
  • 基于SSM(jsp)的宿舍管理系统
  • Java Web应用开发——作业四
  • 基于ASP的反垃圾邮件管理系统的设计与实现
  • 2010年9月计算机二级JAVA笔试试题及答案
  • 博客让谷歌或是百度收录
  • 机器学习分类算法评价指标
  • Socks5代理服务器示例详解
  • 使用 Docker 和 Nginx 反向代理访问 ChatGPT API
  • [前端笔记038]vue2之vueRouter、elementUI
  • ChatGPT使用案例之操作Excel