The GETCHAR() Function


The GETCHAR() Function:

The getchar() is one of the single character input / output functions.
Using the C library function – getchar() , one is allowed to imput a single character from the terminal . This function is the simplest input mechanism which reads character at a time.
It returns a single character that is typed in from the keyboard.
The function does not require any argument though a pair of empty parenthesis must follow the word getchar.

A reference to the getchar function is written as
        Character _variable = getchar();
Where character_variable refers to some previously declared character variable.

See the below program for better understanding

getchar function example

The output for the above program is as follows

getchar function output

In the third line we declared a character variable c,
In the fourth line we are using printf() function which prints everything written within double quotes. Lets discuss about printf() in our upcoming posts.
In the fifth line the getchar function gets the user input and the value is stored in character variable c.
In the sixth line we are printing out what is character that the user entered to the console screen.


To distinguish the end of the input from valid data , the getchar() function returns a distinctive value when there is no  more input , a value that cannot be confused with any real character. This value is called EOF , which denotes “End Of File”.
EOF is an integer defined in the library file <stdio.h> .typically EOF will be assigned the value -1 .(this value vary from compiler to compiler). The variable should be declared to integer type for EOF .

Pressing the key CTRL + Z cause EOT(End Of Terminal) to be sent to program.This character is interpreted by getchar() as an end of file and getchar() then returns the defined  value of EOF.

Given below is an sample program denote eof
eof program example

Output :
eof program output

Note : we will discuss about if () statement in the upcoming posts.


Thank you….


 


Previous Post
Next Post
Related Posts

0 comments:

Popular Posts