Home › Forums › C Programming › difference between getchar(), getch(), and getc() › Re: RE: difference between getchar(), getch(), and getc()
Amir welcome to MYCPLUS Online Community Forums. Thanks that you like the design of the website. Here is what you asked, hopefully you will understand. If there is anyone who can ad more to it I will appriciate.
GETCHAR:
getchar is a macro defined in (stdin), getchar returns the next character on the input stream stdin.
It returns the character read, after converting it to an int without sign extension. If there is error reading the character or (and on end-of-file for getchar), it returns EOF.
1 | Declaration: int getchar(void); |
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h><br /> int main(void)<br /> {<br /> int c;<br /> /* Note that getchar reads from stdin and is line buffered;<br /> this means it will not return until you press ENTER. */<br /> while ((c = getchar()) != 'n')<br /> printf("%c", c);<br /> return 0;<br /> } |
GETCH:
getch gets a character from console but does not echo ( write ) to the screen. It reads a single character directly from the keyboard, without echoing
to the screen. getche reads a single character from the keyboard and echoes it to the current text window, using direct video or BIOS. Both functions return the character read from the keyboard.
Declaration:
1 2 | int getch(void);<br /> int getche(void); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <br /> #include <conio.h><br /> #include <stdio.h><br /> int main(void)<br /> {<br /> int c;<br /> int extended = 0;<br /> c = getch();<br /> if (!c)<br /> extended = getch();<br /> if (extended)<br /> printf("The character is extendedn");<br /> else<br /> printf("The character isn't extendedn");<br /> return 0;<br /> } |
GETC:
getc is a macro that gets one character from a stream. It returns the next character on the given input stream and increments the stream’s file pointer to point to the next character.It returns the character read, after converting it to an int without sign extension. If there is error (and on end-of-file for getc), both functions return EOF.
1 | Declaration: int getc(FILE *stream); |
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h><br /> int main(void)<br /> {<br /> char ch;<br /> printf("Input a character:");<br /> /* read a character from the<br /> standard input stream */<br /> ch = getc(stdin);<br /> printf("The character input was: '%c'n", ch);<br /> return 0;<br /> }<br /> |