Pro-Tutr
What it's difference between getchar(),getch(),getche () in c programming - Printable Version

+- Pro-Tutr (http://protutr.com)
+-- Forum: Basic Programming Languages (http://protutr.com/forumdisplay.php?fid=23)
+--- Forum: C (http://protutr.com/forumdisplay.php?fid=21)
+--- Thread: What it's difference between getchar(),getch(),getche () in c programming (/showthread.php?tid=333)



What it's difference between getchar(),getch(),getche () in c programming - Ravishankar Chavare - 01-06-2018

Hello guys you already know the difference between void main()& int main () if you not read Here


Today we are learning that what it's difference between getchar(),getch(),getche ()

All these are unformatted standard inputs functions to read single character from console or keyboard

What is mean by unformatted ?
Unformatted means they don't read foramted string or character such as /n /t etc.They normal read only single normal character from either console or either keyboard keys.


1.getchar():

-getchar() is standard unformatted input functions used to read single character from console

-when you used getchar() then cursor waiting to input for reading from keyboard when you enter single character it read and wait to press enter key to perform next operations

Example:
Code:
#include<stdio.h>
int main() {
   char ch;
   printf("Enter any charcter");
   ch=getchar();
   printf("%c", ch);
}


2.getch():

-getch() when you used it then cursor is waiting for reading input when you press any key on keyboard then getch () function read that character and don't print on console

-And perform another operations as soon as user press any key

Example:
Code:
#include<stdio.h>
int main() {
   char ch;
   printf("Enter any charcter");
   ch=getch();
   printf("%c", ch);
}

3.getche():

-This is similar to getch() function but there is some quite difference
Is that
-getche() functions read input from keyboard and show it on console also and perform next operations


Example:
Code:
#include<stdio.h>
int main() {
   char ch;
   printf("Enter any charcter");
   ch=getche();
   printf("%c", ch);
}

Conclusion

1.getchar():
-wait for keyboard input press
-after pressing key show it on console
-wait to press enter key
-after press enter key perform next operations

2.getch():
-wait for keyboard input press
-after pressing any key don't show on console
-perform next operations automatically when you enter any single character

3.getche():
-wait for keyboard input press
-after pressing any key show it on console first
--perform next operations automatically when you enter any single character