当前位置: 首页 > 图文教程 > 开发语言 > C/C++ > C/C++:小编谈C语言函数那些事(11)
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. kbhit函数
kbhit函数是检查当前按下的键,其用法为:int kbhit(void);程序例子如下:
#include <conio.h>
int main(void)
{
cprintf("Press any key to continue:");
while (!kbhit()) /* do nothing */ ;
cprintf("\r\nA key was pressed...\r\n");
return 0;
}
2. keep函数
keep函数是退出并继续驻留,其用法为:void keep(int status, int size);程序例子如下:
#include <dos.h>
#define INTR 0x
#define ATTR 0x7900
extern unsigned _heaplen = 1024;
extern unsigned _stklen = 512;
void interrupt ( *oldhandler)(void);
void interrupt handler(void)
{
unsigned int (far *screen)[80];
static int count;
screen = MK_FP(0xB800,0);
count++;
count %= 10;
screen[0][79] = count + '0' + ATTR;
oldhandler();
}
int main(void)
{
oldhandler = getvect(INTR);
setvect(INTR, handler);
keep(0, (_SS + (_SP/16) - _psp));
return 0;
}
3. lfind函数
Lfind函数是执行线性搜索,其用法为:void *lfind(void *key, void *base, int *nelem, int width, int (*fcmp)()); 程序例子如下:
#include <stdio.h>
#include <stdlib.h>
int compare(int *x, int *y)
{
return( *x - *y );
}
int main(void)
{
int array[5] = {35, 87, 46, 99, 12};
size_t nelem = 5;
int key;
int *result;
key = 99;
result = lfind(&key, array, &nelem,
sizeof(int), (int(*)(const void *,const void *))compare);
if (result)
printf("Number %d found\n",key);
else
printf("Number %d not found\n",key);
return 0;
}
评论 (0) All