Home › Forums › C Programming › difference between getchar(), getch(), and getc()
- This topic has 4 replies, 4 voices, and was last updated 19 years, 9 months ago by VIGNESH.
- AuthorPosts
- August 14, 2004 at 11:06 pm #1862MelisaParticipant
Hello everybody!
This is my first post here in this forum. I like design. My question is: C language has so many functions for getting one character. GETCHAR, GETCH, GETC. Why is so many and what is the difference between these functions? I tried to read in the books but I am still confused. If somebody has good examples I would appreciate that.
Thanks! - August 15, 2004 at 4:11 am #3104msaqibParticipant
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.1Declaration: int getchar(void);12345678910#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:12int getch(void);<br />int getche(void);12345678910111213141516<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.1Declaration: int getc(FILE *stream);123456789101112#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 /> - August 17, 2004 at 8:57 pm #3105MelisaParticipant
Thanks msaqib, this was very useful for me.
- February 19, 2005 at 10:05 am #3106prasannaParticipant
If you open the Torbo C++ Compiler help it will give you the following details about these functions.
getchar is a macro defined as getc(stdin) getchar returns the next character on the input stream stdin.
Return Value:
On success
– getchar returns the character read, after converting it to an int without sign extension.
On error (and on end-of-file for getchar), both macros return EOF.getc returns the next character on the given input stream and increments the stream’s file pointer to point to the next character.
Return Value:
On success
– getc returns the character read, after converting it to an int without sign extension.
On error (and on end-of-file for getc), both functions return EOF.I hope you can now wel understand this.
- February 19, 2005 at 9:11 pm #3107VIGNESHParticipant
thank you very msaqib and guru for clearing my doubt.
genetic.
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.