Monday, February 20, 2012

C Language - getchar() : get character from stdin

getchar

function
<cstdio>
int getchar ( void );
Get character from stdin
Returns the next character from the standard input (stdin).
It is equivalent to getc with stdin as its argument.

Parameters

(none)

Return Value

The character read is returned as an int value.
If the End Of File is reached or a reading error happens, the function returns EOF and the corresponding error or eof indicator is set. You can use either ferror or feof to determine whether an error happened or the End-Of-File was reached.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
/* getchar example : typewriter */
#include <stdio.h>

int main ()
{
  char c;
  puts ("Enter text. Include a dot ('.') in a sentence to exit:");
  do {
    c=getchar();
    putchar (c);
  } while (c != '.');
  return 0;
}


A simple typewriter. Every sentence is echoed once ENTER has been pressed until a dot (.) is included in the text.

No comments:

Post a Comment