
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>
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 演示示例
#include <stdio.h>
#include <time.h>int main(void)
{time_t first, second; first = time(NULL); getchar();second = time(NULL); 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)
{int gdriver = DETECT, gmode, errorcode;int maxx, maxy;int poly[10];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk) {printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();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;poly[8] = poly[0];poly[9] = poly[1];drawpoly(5, poly);getch();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";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);
}
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";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;
}
6.3 运行结果

参考
- [API Reference Document]