Reading a Character

The following functions are used to read a character from the keyboard.

getchar ()

Reads a character from the keyboard; waits for carriage return. Returns an integer, in which the low – order byte contains the character. any key, including RETURN, TAB and ESC may be returned by getchar (). The function does not require any arguments, though a pair of empty parentheses must follow the word getchar. The getchar () takes the following from:

var _ name = getchar ();

where var _ name is a valid char type variable.

Because it waits for a carriage returns, getchar () is not useful in an interactive environment The ANSI standard does not define any function guaranteed to provide interactive input, but many C compilers include alternative keyboard input functions which achieve this. For example:

char c;
c= getchar ();


program- Use of getchar () function
#include <stdio.h>
void main ()
{
    char ch;
    printf (“\nEnter a character:”);
    ch = getchar ();
    printf (“Entered character is: %c\n”, ch);
}

Output

Enter a character: a
Enter character is: a

If an end – of – file condition is encountered when reading a character with getchar () function, the value of the symbolic constant EOF will automatically be returned. The getchar() function can also be used to read multi character strings, by reading one-character strings, by reading one character at a time within a multipass loop.


getch ()

getch () is similar to the getchar (). But it waits for a keypress, after which it returns immediately. It reads a character without an echo to the screen. For example:

char;
c= getch ();


getche ()

getche () is same as getch (), but it echoes the character on the screen. For example:

char c;
getche ();



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext