Friday, September 9, 2011

Difference between getch() and getche()


Both getch() and getche() are used to read single character there is very little difference

following example will clear this.....
1.
    main()
   {
   getch();
   }
2.
   main()
  {
  getche();
  }
after running above programs...............
when you press any key, you'll exit from output screen
verify the output by pressing alt+F5
1. will not show anything
2.will show the key you were pressed......

__________________________________________________________________
getch() - get character from screen without echo and compiler didn't wait for another key.
getche() - get character from screen and compiler didn't wait for another key
getchar() - get character from screen and compiler wait for another key.

getche- displays the character that u have typed on the screen to exit from output.
getch- : It reads a character and never wait for Enter key just gets processed after getting any key pressed and just returns the character that had been typed without displaying on the screen.
getchar- : It works differently from others two. Whenever you are pressing any key then the these are kept in Buffer After hitting enter the first character gets processed. And it obviously echoes on the screen.
what is echo?
In
this case, echo means that when you type a character, it prints to the screen so you can see what you typed.
gets()
Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached. The ending newline character ('\n') is not included in the string.
Example Program:

 #include<stdio.h>
#include<conio.h>
main()
{
  clrscr();
  printf(“good morning”);
  getch();
}

// alt f9:
   ctrl f9:good morning  /press any key instead of enter you will exit from output.

   press alt f5 only output is displayed.


Getche(): press any key you will enter into program then press alt f5 then output and the key you pressed will be displayed.

Getchar():only enter should be pressed to exit from output. Press alt f5 then output and the key pressed will be displayed i.e, any key can be pressed instead of enter but after pressing enter it goes into program.

No comments:

Post a Comment