System related function

1)      delay:- delay() is an inbuilt function that reside into dos.h and it is responsible to pause a continue execution for specified time. It accept an argument for time in millisecond.
Declaration Syntax:-
void delay(unsigned millisecond);
 
 


For example:-
                                         delay(2000);
2)      sleep:- sleep() is an inbuilt function that reside into header file dos.h and it is also responsible to pause a continue execution  for specified time. It accept an argument in second.
Declaration Syntax:-
void sleep(unsigned second);
 
                                       
For example:-
                                         sleep(2);
Example:-
void main()
{
clrscr();
printf(“Welcome to C”);
delay(2000);
printf(“\n Welcome to programming”);
sleep(2);
printf(“\n It is a demo”);
getch();
}
Output-------- Welcome to C
                          Welcome to programming
                          It is a demo
3)      kbhit():- kbhit() is an inbuilt function of conio.h and it is specially used for check keyboard stroke i.e any key press or not and it return an integer value as a following way:-
(i)                  It return 0(zero) until a key pressed.
(ii)                It returns -1 (minus one) when a key is pressed.
int kbhit();    Declaration Syntax:-


Syntax to call kbhit():- 
   
kbhit();

For example:-
void main()
{
clrscr();
while(1)
{
r=kbhit();
printf(“r=%d”,r);
sleep(1);
break;
}
getch();
}
Output--------      r=0              
                            r=0                 when not any key is pressed
                            r=0
                            r=0
                           r=-1               when key is pressed during execution
Example:-
Write a program in C to display a message i.e Welcome to C until user press any key and pause execution each time for 2 second.
void main()
{
clrscr();
while(!kbhit())
{
printf(“Welcome to C \n”);
sleep(2);
}
printf(“Execution Complete”);
getch();
}
Output-----------                        Welcome to C
                                                         Welcome to C
                                                         Welcome to C
                                                         ………………………
                                                         Execution Complete

                          OR
void main()
{      clrscr();
        textbackground(2);
while(!kbhit())
{        i=i+1;
         textcoor(i);
         cprintf(“\n \r Welcome to C”);
         sleep(2);                 }
printf(“Execution Complete”);
getch();

}

Comments

Popular Posts