Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getc Vs getchar Vs Scanf for reading a character from stdin

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?

like image 922
Jay Avatar asked Mar 24 '10 11:03

Jay


People also ask

What is the purpose of scanf() function compare it with getchar() function?

scanf is a C function to read input from the standard input until encountering whitespace, newline or EOF while getchar is a C function to read a character only from the standard input stream(stdin), which is the keyboard. Thus, this is the main difference between scanf and getchar.

Why is Getchar safer than scanf?

scanf is faster because it can read multiple characters at once. However, getchar is safer because it can only read one character at a time.

Is Getchar a Stdin?

getchar is a function in C programming language that reads a single character from the standard input stream stdin, regardless of what it is, and returns it to the program. It is specified in ANSI-C and is the most basic input function in C. It is included in the stdio. h header file.

What is the difference between GETC and Fgetc?

getc returns the next character from the named input stream. getchar is identical to getc(stdin). fgetc behaves like getc, but is a genuine function, not a macro; it may therefore be used as an argument. fgetc runs more slowly than getc, but takes less space per invocation.


2 Answers

If you simply want to read a single character from stdin, then getchar() is the appropriate choice. If you have more complicated requirements, then getchar() won't be sufficient.

  • getc() allows you to read from a different stream (say, one opened with fopen());
  • 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 a char variable to store the result. You need to use an int variable, since the range of values getchar() returns is "a value in the range of unsigned char, plus the single negative value EOF". A char variable doesn't have sufficient range for this, which can mean that you can confuse a completely valid character return with EOF. The same applies to getc().

like image 101
caf Avatar answered Oct 16 '22 18:10

caf


from Beej's Guide to C Programming

All of these functions in one way or another, read a single character from the console or from a FILE. The differences are fairly minor, and here are the descriptions:

getc() returns a character from the specified FILE. From a usage standpoint, it's equivalent to the same fgetc() call, and fgetc() is a little more common to see. Only the implementation of the two functions differs.

fgetc() returns a character from the specified FILE. From a usage standpoint, it's equivalent to the same getc() call, except that fgetc() is a little more common to see. Only the implementation of the two functions differs.

Yes, I cheated and used cut-n-paste to do that last paragraph.

getchar() returns a character from stdin. In fact, it's the same as calling getc(stdin).

like image 5
Dchris Avatar answered Oct 16 '22 18:10

Dchris