F.
Console Input/Output
C provides (through its libraries) a variety of input/output routines. At the character level,
getchar() , getche() and getch() read one character at a time from the keyboard, while
putchar() prints a character to the screen at the current cursor location. In terms of complexity
and power, are the functions gets () and puts() . These two functions enable the user to
read and write strings of characters at the console (screen). There are also two functions that
perform formatted input and output on the built-ir- data types: printf() and scanf(). The
printf() function as discussed from sections D and E is used to write data on the screen while
scanf () , its complement, reads data from the keyboard.
F.l Single Character Input/Output
The simplest of the user input functions are getchar (), getche () and
getch(). These functions require the conio.h header file.
getchar ( ) Reads a character from the keyboard and waits for a carriage return.
getche( ) Reads a character from the keyboard, with echo on the screen and does waits for
a carriage return.
getch ( ) Reads a character from the keyboard, without echo on the a screen and does
not wait for a carriage return.
putchar( ) Prints a character to the screen at the current cursor location.
Program F.l Reading user input from the keyboard using getch()
#include <stdio .h>
#include <conio.h>
int main ( )
{
char choice;
clrscr ( ) ;
printf ("Select an option (A - C)”) ;
printf(“\n\n A. Check Spelling");
printf ("\nB. Correct Spelling Errors");
printf ("\nC. Display Spelling Errors");
printf (“\n\n Type your choice:);
choice = getch() ;
choice = toupper (choice) ;
printf("\n\nYou chose %c", choice) ;
getch ( ) ;
} // end of Program
Sample Dialogue
Select an option (A - C)
A. Check Spelling
B. Correct Spelling Errors
C. Display Spelling Errors
Type your choice:
You chose B
Program F.l is an example of how user input is read from the keyboard. The program uses the
getch() function, so as a key is pressed, the function returns the value and stores it in the
variable choice. The toupper() function is then used to convert the input into upper case, if it
is not yet in upper case. The last printf displays the value of the variable choice, which is the user's
input in upper case.
If the user wants to have time to think about his choice on Program F. l, the use of
getchar() function would be an alternative choice because it requires that the user to press the
enter key before the input is accepted. The modified program is shown in Program F.2.
Program F.2 Reading and Writing user input using getchar() and putchar()
include <stdio.h>
include <conio.h>
int main()
{
char choice;
clrscr( );
printf("Select an option (A—C), then press the ENTER Key");
printf( Check Spelling");
printf("\nB. Correct Spelling Errors");
printf("\nC. Display Spelling Errors");
printf(“\n\nType your choice:" );
choice = getchar(); choice = toupper(choice);
printf( "\n");
putchar (choice);
getch() ;
} //end of Program F. 2
Sample Dialogue
Select an option (A - C)
A. Check Spelling
B. Correct Spelling Errors
C. Display Spelling Errors
Type your choice: c
You chose C
F.2 Accepting General Inputs
The scanf() function is a general-purpose input routine that reads all the built-in data types and
automatically convert them into the proper internal format. It is much like the reverse of
printf() function. The general format is
scanf ("conversion character", 1 value of variable) ;
scanf() uses most of the conversion characters of printf( ) , but the conversion character appears
within double quotes. The second argument is the lvalue of the variable where the value of the
input is to be stored. The address_of operator (&) must therefore precede the variable name. If
storage will be in an array, only the array name (without any brackets) is used.
Program F.3 Using scanf() function
#include <stdio .h>
#include <conio.h>
int main ( )
{
float kilos,pounds;/*variable declaration*/
clrscr ( ) ;
printf ("Enter your weight in kilograms:”);
scanf(“%f”,&kilos);
pounds = kilos*2.2;
printf(“\n\n Your weight in pounds is %.3f",pounds);
getch ( ) ;
} // end of Program
Sample Dialogue
Enter your weight in kilograms:48.2
Your weight in pounds is 106.04
F.3 gets( ) and puts( )
The gets() function reads a string of characters entered at the keyboard and places them
at the address pointed to by its character pointer argument. You may type characters at the
keyboard (including spaces) until you strike a carriage return (enter key). The carriage return does
not become part of the string; instead a null terminator is placed on its end. The puts() function
on the other hand writes its string argument to the screen followed by a newline. Program F .4
shows the use of gets() and puts() functions.
Program F.4 Using the gets( ) and puts( ) functions
#include <stdio .h>
#include <conio.h>
#include <string.h>
int main ( )
{
char str[80];
clrscr ( ) ;
printf ("Enter a string of characters:”);
gets(str);
printf(“\nThe length of the string is %d”,strlen(str));
printf(“\n\nThe string that you typed is);
puts(str);
getch ( ) ;
} // end of Program
Sample Dialogue
Enter a string of characters: C IS A VERSATILE PROGRAMMING LANGUAGE.
The length of the string is 38
The string that you type is C IS A VERSATILE PROGRAMMING LANGUAGE.
Using the gets() function, the program reads a string into the array str. It can hold up to 80
characters including spaces. The strlen is a function included in the string header file and will
return the length of characters in array str. Lastly, the puts() function will print on the screen
the string of characters inputted by the user. More uses of arrays will be discussed later.