Of the below three functions:
getc
getchar &
scanf
which is the best one for reading a character from stdin and why?
Are there any known disadvantages or limitations for any of these functions which makes one better than the other?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you simply want to read a single character from stdin, then
getchar()is the appropriate choice. If you have more complicated requirements, thengetchar()won’t be sufficient.getc()allows you to read from a different stream (say, one opened withfopen());scanf()allows you to read more than just a single character at a time.The most common error when using
getchar()is to try and use acharvariable to store the result. You need to use anintvariable, since the range of valuesgetchar()returns is “a value in the range ofunsigned char, plus the single negative valueEOF“. Acharvariable doesn’t have sufficient range for this, which can mean that you can confuse a completely valid character return withEOF. The same applies togetc().