-2

What are the practical uses of these functions? I mean if we use them using while loop and EOF we get just what we type like if we give input "hello" we will get the output as "hello". So what's the point?

Tamer Tas
  • 3,288
  • 13
  • 22
  • Maybe you want to store the input you get from getchar() and manipulate the data before you output it with putchar(). – nos Jun 17 '14 at 10:46

2 Answers2

0

Using getchar and putchar are simpler for character I/O because, they do not require a format string as do scanf and printf.

Also, scanf stores a data item in an object whose address is given by its argument, whereas getchar returns the value of the character read as its value. Both scanf and getchar return EOF as their value to indicate that the End-of-File has been reached, an invalid character has been read or to signal some other failure conditions.

So, getchar and putchar allows you to write solid code without dealing with the format of the input or the input.

Tamer Tas
  • 3,288
  • 13
  • 22
  • end-of-file is a condition, not a marker. `getchar()` returns `EOF` to signal it failed. You can test the reason for failure with `ferror()` and/or `feof()` – pmg Jun 17 '14 at 11:26
  • @pmg EOF is used as the value returned by several functions to indicate that the End-of-File has been reached, an invalid character has been read or to signal some other failure conditions. EOF is a macro indicating several failures so, marker in a sense. But, I mentioned EOF generally in my answer. I don't see how you understood what you understood. Surely it's not a reason to downvote don't you think? – Tamer Tas Jun 17 '14 at 11:33
  • I guess it's a question of pedantism: the functions return `EOF` when they **fail to read data** (for one of several reasons), not when they read something. – pmg Jun 17 '14 at 11:38
  • @pmg I can see how my wording might misled some. I edited the answer to clarify the `EOF` part. – Tamer Tas Jun 17 '14 at 11:41
-1

int getchar() = Returns the next character from the standard input (stdin).

Basically read one character at a time.

putchar(int) = Writes a character to the standard output (stdout).

It allows you to deal with any type of input/output without knowing their exact data type. It provides more control and flexibility on IO operation. It is also faster than scanf/pritnf so you can use it in online programming competitions where lot of IO operations are required.

Nishant Kumar
  • 2,199
  • 2
  • 22
  • 43