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