74 Programming and Data Structures
4.3 UNFORMATTED FUNCTIONS
C has three types of I/O functions.
D) C h a ra cte r I/O
E) S tri n g I/O
F) F il e I/O
a) Character I/O
1. g e t ch a r () This function reads character type data from the standard input. It reads one character
at a time till the user presses the enter key.
4.14 Write a program to accept characters through keyboard using getchar I) function.
# include <stdio.h>
xnainO
{
c h a r c h [ 2 0 ] ;
in t c« 0;
c l r s c r ( ) ;
w hile ( (c h [c ] « g e tc h a r ( ) ) l o ' n ')
C++;
c h [c ] ;
p r i n t f ("n % 8"#ch);
}
OUTPUT;
COMPILER
COMPILER
Explanation In the above program a character a r r a y is declared. Using the w h ile loop
continuously, characters are read through the keyboard using g e tc h a r () function till the user
presses enter. Using p r in t f () statement the entered string is displayed.
Note: Thewhile loop refer chapter 6] The co n tro l sta tem en ts.
2. putchar () - This function prints one character on the screen at a time which is read by the standard
input.
4.15 Write a program to print the characters using putcharO funcions.
# in clu d e < std io.h >
mainQ
{
char ch[20];
in t c=0;
c lr s c r ();
printf (" Enter Text Here
Input and Output in C 75
scanf ("%s",ch);
printf ("n The Entered Text: ");
while (ch[c]l='')
I
putchar(ch[c]);
C++;
Qirrrui ;
Enter Text Here : Characters
The Entered Text: Characters
Explanation In the above program the sc a n f () statement reads the string from the terminal.
The p u tchar () function displays one character at a time on the console. The w h ile loop causes
repetitive execution of putchar () function and increases the counter' c *. The counter' c is used to
display the successive characters. When ' 0 ' is encountered program is terminated.
3. getc h O & getch e () These functions read any alphanumeric characters from the standard
input device. The character entered is not displayed by g e c tc h () function.
4.16 Write a program to show the effect of getcheO and getchO.
# Include <stdio.h>
# Include <conio.h>
void main ()
{
c lrs c r ();
printf ("Enter any two alphabetics ");
getcheO;
getchO;
I
OUTPUT:
Enter any two alphabetics A
Explanation In the above program even though the two characters are entered, the user can see only
one character on the screen. The second character is accepted but not displayed on the console. The
getch e () accepts and displays the character whereas g e tch () accepts but dose not display the
character.
4. putch () This function prints any alphanumeric character taken by the standard input device.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.27.251