Tuesday, January 10, 2012

understanding getch(), getche() and getchar() in C

/* program to understand the getch(), getche() and getchar() differences */

//note that these three functions are for getting character from the cosole only but they differ the way they take

#include<stdio.h>
#include<conio.h>
void main()
{
char ch[2],op;
int i=0;
clrscr();
while((i++)<=2)      //just i am rotating this for 3 times
{
printf("\na.getch() \nb. getche() \nc.getchar()\n\t");
puts("enter your option");
ch=getche();

switch(op)
{
case 'a': puts("\nenter char");
ch[0]=getch();      //here character entered is stored but not displayed on console
break;                                             
case 'b':puts("\nenter char");      
ch[1]=getche();   //here char entered is taken(no need to press enter) and displayed on console
break;                                          
case 'c': puts("\nenter char");
ch[2]=getchar();           //here character entered is displayed on console and
break;                           //entered char is stored only after pressing 'enter key'

}
}
printf("\nstored values are %s",ch);
getch();
}

No comments:

Post a Comment