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

C/C++
C/C++:小编谈C语言函数那些事(3)
C/C++:小编谈C语言函数那些事(4)
C/C++:小编谈C语言函数那些事(5)
C/C++:小编谈C语言函数那些事(6)
C/C++:小编谈C语言函数那些事(7)
C/C++:小编谈C语言函数那些事(8)
C/C++:小编谈C语言函数那些事(9)
C/C++:小编谈C语言函数那些事(10)
C/C++:小编谈C语言函数那些事(11)
C/C++:小编谈C语言函数那些事(12)
C/C++:小编谈C语言函数那些事(13)
C/C++:小编谈C语言函数那些事(14)
C/C++:小编谈C语言函数那些事(15)
C/C++:小编谈C语言函数那些事(16)
C/C++:小编谈C语言函数那些事(17)
C/C++:小编谈C语言函数那些事(18)
C/C++:小编谈C语言函数那些事(19)
C/C++:小编谈C语言函数那些事(20)
C/C++:小编谈C语言函数那些事(21)
C/C++:小编谈C语言函数那些事(22)

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


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

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

 

1.     raise函数

raise函数的功能是向正在执行的程序发送一个信号,其用法为:int raise(int sig);程序实例如下:

#include <signal.h>
int main(void)
{
   int a, b;
   a = 10;
   b = 0;
   if (b == 0)
      raise(SIGFPE);
   a = a / b;
   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(0x
0F, &blk, 0);
    save_dta = getdta();
   setdta(buffer);
   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;
}