C/C++——小编谈C语言函数那些事(17)

 

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

 

1.    registerbgidriver函数

registerbgidriver函数的功能是登录已连接进来的图形驱动程序代码,其用法为:int registerbgidriver(void(*driver)(void));程序实例如下:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
   int gdriver = DETECT, gmode, errorcode;
   errorcode = registerbgidriver(EGAVGA_driver);
     if (errorcode < 0)
   {
      printf("Graphics error: %s/n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); 
   }
    initgraph(&gdriver, &gmode, "");

   errorcode = graphresult();
   if (errorcode != grOk)  

   {
      printf("Graphics error: %s/n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }
    line(0, 0, getmaxx(), getmaxy());
     getch();
   closegraph();
   return 0;
} 2.      randbrd
函数

randbrd函数的功能是随机块读,其用法为int randbrd(struct fcb *fcbptr, int reccnt);程序实例代码如下:

#include <process.h>
#include <string.h>
#include <stdio.h>
#include <dos.h>
int main(void)
{
   char far *save_dta;
   char line[80], buffer[256];
   struct fcb blk;
   int i, result;
    printf("Enter drive and file name (no path - i.e. a:file.dat)/n");
   gets(line);
     if (!parsfnm(line, &blk, 1))
   {
      printf("Error in call to parsfnm/n");
      exit(1);
   }
   printf("Drive #%d  File: %s/n/n", blk.fcb_drive, blk.fcb_name);
    bdosptr(0x0F, &blk, 0);
    save_dta = getdta();
   setdta(buffer);
   /* set up info for the new dta */
   blk.fcb_recsize = 128;
   blk.fcb_random = 0L;
   result = randbrd(&blk, 1);

   if (!result)
      printf("Read OK/n/n");
   else
   {
      perror("Error during read");
      exit(1);
   }
   printf("The first 128 characters are:/n");
   for (i=0; i<128; i++)
      putchar(buffer[i]);
   setdta(save_dta);
   return 0;
}

3.     realloc函数

realloc函数的功能是重新分配主存, 其用法为:void *realloc(void *ptr, unsigned newsize);程序实例代码如下:

#include <stdio.h>
#include <alloc.h>
#include <string.h>
int main(void)
{
   char *str;
   str = malloc(10);

   strcpy(str, "Hello");
   printf("String is %s/n  Address is %p/n", str, str);
   str = realloc(str, 20);
   printf("String is %s/n  New address is %p/n", str, str);
     free(str);
   return 0;
}

4.      rectangle函数

rectangle函数的功能是画一个矩形,其用法为:void far rectangle(int left, int top, int right, int bottom);程序实例代码如下:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
   int gdriver = DETECT, gmode, errorcode;
   int left, top, right, bottom;
    initgraph(&gdriver, &gmode, "");
     errorcode = graphresult();
   if (errorcode != grOk) 
   {
      printf("Graphics error: %s/n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);

   }
   left = getmaxx() / 2 - 50;
   top = getmaxy() / 2 - 50;
   right = getmaxx() / 2 + 50;
   bottom = getmaxy() / 2 + 50;
    rectangle(left,top,right,bottom);

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值