当前位置: 首页 > 图文教程 > 开发语言 > C/C++ > C/C++:小编谈C语言函数那些事(4)

C/C++
2009年编程开发语言的使用率
C++对象模型笔记:dynamic binding
cstl -- c语言编写通用数据结构和常用算法库(模仿SGI STL)
子串匹配:不回溯算法
C++ Builder 访问 USB 口的方法
C++中二维数组的动态分配
数组和指针在编译的时候的区别
如何利用doxygen生成pdf文档
有关C++析构函数的异常(Exceptions in Destructors)
C++模板学习之函数对象之谓词
5月编程语言排行榜:D语言风采不再
一个C++类实现文件全盘搜索
C语言编程宝典之一 读书笔记
C语言嵌入式系统编程修炼(内存操作)
C++内存管理
带头结点的双循环链表
有关VA_LIST的用法
C++标准库简介(转)
一个栈类的实现(链栈)
C 引用与指针的比较

C/C++:小编谈C语言函数那些事(4)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 53 ::
收藏到网摘: n/a

C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。

1.       delline函数

delline函数的功能是在文本窗口中删去一行,其用法为:void delline(void);程序例子如下:

#include <conio.h>

int main(void)

{

   clrscr();

   cprintf("The function DELLINE deletes \

    the line containing the\r\n");

   cprintf("cursor and moves all lines \

    below it one line up.\r\n");

   cprintf("DELLINE operates within the \

    currently active text\r\n");

   cprintf("window.  Press any key to \

    continue . . .");

   gotoxy(1,2);  /* Move the cursor to the

      second line and first column */

   getch();

   delline();

   getch();

   return 0;

}

2.       detectgraph函数

detectgraph函数的功能是通过检测硬件确定图形驱动程序和模式,其用法为:void far detectgraph(int far *graphdriver, int far *graphmode); 程序例子如下:

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

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

   clrscr();

   printf("You have %s video display \

   card.\n", dname[gdriver]);

   printf("Press any key to halt:");

   getch();

   return 0;

}

 

3.       difftime函数

difftime函数的功能是计算两个时刻之间的时间差,其用法为:double difftime(time_t time2, time_t time1);  程序例子如下:

#include <time.h>

#include <stdio.h>

#include <dos.h>

#include <conio.h>

int main(void)

{

   time_t first, second;

   clrscr();

   first = time(NULL);  /* Gets system

      time */

   delay(2000);         /* Waits 2 secs */

   second = time(NULL); /* Gets system time

      again */

   printf("The difference is: %f \

   seconds\n",difftime(second,first));

   getch();

   return 0;

}

4.       dup函数

dup函数的功能是复制一个文件句柄,其用法为:int dup(int handle); 程序例子如下:

#include <string.h>

#include <stdio.h>

#include <conio.h>

#include <io.h>

void flush(FILE *stream);

int main(void)

{

   FILE *fp;

   char msg[] = "This is a test";

   /* create a file */

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

   /* write some data to the file */

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

   clrscr();

   printf("Press any key to flush \

   DUMMY.FIL:");

   getch();

   /* flush the data to DUMMY.FIL without

      closing it */

   flush(fp);

   printf("\nFile was flushed, Press any \

   key to quit:");

   getch();

   return 0;

}

void flush(FILE *stream)

{

   int duphandle;

   /* flush TC's internal buffer */

   fflush(stream);

   /* make a duplicate file handle */

   duphandle = dup(fileno(stream));

   /* close the duplicate handle to flush the

      DOS buffer */

   close(duphandle);

}