当前位置: 首页 > 图文教程 > 开发语言 > C/C++ > C/C++:小编谈C语言函数那些事(17)
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
save_dta = getdta();
setdta(buffer);
blk.fcb_recsize = 128;
blk.fcb_random =
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) All