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

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语言函数那些事(2)


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

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

1. bdos函数

bdos函数DOS系统调用,其用法为:int bdos(int dosfun, unsigned dosdx,unsigneddosal); 程序例子如下:

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

}

2. bioscom函数

bioscom函数是为串行I/O通信服务的,其用法为:int bioscom(int cmd, char abyte, int port); 程序例子如下:

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

}

3. biostime函数

biostime函数是读取或设置BIOS时间,其用法为:long biostime(int cmd, long newtime);程序例子如下:

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

}

4. bsearch函数

bsearch函数是为二分法搜索提供服务的,其用法为:void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *)); 程序例子如下:

#include <stdlib.h>

#include <stdio.h>

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

int numarray[] = {123, 145, 512, 627, 800, 933};

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 = bsearch (&key, numarray, NELEMS(numarray),

      sizeof(int), (int(*)(const void *,const void *))numeric);

   return (itemptr != NULL);

}

int main(void)

{

   if (lookup(512))

      printf("512 is in the table.\n");

   else

      printf("512 isn't in the table.\n");

   return 0;

}