There are several standard library functions available under this category—those that can deal with a single character and those that can deal with a string of characters. For openers let us look at those which handle one character at a time.
So far for input we have consistently used the scanf( ) function. However, for some situations the scanf( ) function has one glaring weakness... you digest what you have typed. However, we often want a function that will read a single character the instant it is typed without waiting for the Enter key to be hit. getch( ) and getche( ) are two functions which serve this purpose. These functions return the character that has been most recently typed. The ‘e’ in getche( ) function means it echoes (displays) the character that you typed to the screen. As against this getch( ) just returns the character that you typed without echoing it on the screen. getchar( ) works similarly and echo’s the character that you typed on the screen, but unfortunately requires Enter key to be typed following the character that you typed. The difference between getchar( ) and fgetchar( ) is that the former is a macro whereas the latter is a function. Here is a sample program that illustrates the use of these functions.
So far for input we have consistently used the scanf( ) function. However, for some situations the scanf( ) function has one glaring weakness... you digest what you have typed. However, we often want a function that will read a single character the instant it is typed without waiting for the Enter key to be hit. getch( ) and getche( ) are two functions which serve this purpose. These functions return the character that has been most recently typed. The ‘e’ in getche( ) function means it echoes (displays) the character that you typed to the screen. As against this getch( ) just returns the character that you typed without echoing it on the screen. getchar( ) works similarly and echo’s the character that you typed on the screen, but unfortunately requires Enter key to be typed following the character that you typed. The difference between getchar( ) and fgetchar( ) is that the former is a macro whereas the latter is a function. Here is a sample program that illustrates the use of these functions.
main( )
{
char ch ;
printf ( "\nPress any key to continue" ) ;
getch( ) ; /* will not echo the character */
printf ( "\nType any character" ) ;
ch = getche( ) ; /* will echo the character typed */
printf ( "\nType any character" ) ;
getchar( ) ; /* will echo character, must be followed by enter key */
printf ( "\nContinue Y/N" ) ;
fgetchar( ) ; /* will echo character, must be followed by enter key */
}
{
char ch ;
printf ( "\nPress any key to continue" ) ;
getch( ) ; /* will not echo the character */
printf ( "\nType any character" ) ;
ch = getche( ) ; /* will echo the character typed */
printf ( "\nType any character" ) ;
getchar( ) ; /* will echo character, must be followed by enter key */
printf ( "\nContinue Y/N" ) ;
fgetchar( ) ; /* will echo character, must be followed by enter key */
}
And here is a sample run of this program...
Press any key to continue
Type any character B
Type any character W
Continue Y/N Y
putch( ) and putchar( ) form the other side of the coin. They print a character on the screen. As far as the working of putch( ) putchar( ) and fputchar( ) is concerned it’s exactly same. The following program illustrates this.
main( )
{
char ch = 'A' ;
putch ( ch ) ;
putchar ( ch ) ;
fputchar ( ch ) ;
putch ( 'Z' ) ;
putchar ( 'Z' ) ;
fputchar ( 'Z' ) ;
}
And here is the output...
AAAZZZ
main( )
{
char ch = 'A' ;
putch ( ch ) ;
putchar ( ch ) ;
fputchar ( ch ) ;
putch ( 'Z' ) ;
putchar ( 'Z' ) ;
fputchar ( 'Z' ) ;
}
And here is the output...
AAAZZZ
The limitation of putch( ), putchar( ) and fputchar( ) is that they can output only one character at a time.
gets( ) and puts( )
gets( ) receives a string from the keyboard. Why is it needed? Because scanf( ) function has some limitations while receiving string of characters, as the following example illustrates...
main( )
{
char name[50] ;
printf ( "\nEnter name " ) ;
scanf ( "%s", name ) ;
printf ( "%s", name ) ;}
{
char name[50] ;
printf ( "\nEnter name " ) ;
scanf ( "%s", name ) ;
printf ( "%s", name ) ;}
And here is the output...
Enter name Jonty Rhodes
Jonty
Surprised? Where did “Rhodes” go? It never got stored in the array name[ ], because the moment the blank was typed after “Jonty” scanf( ) assumed that the name being entered has ended. The result is that there is no way (at least not without a lot of trouble on the programmer’s part) to enter a multi-word string into a single variable (name in this case) using scanf( ). The solution to this problem is to use gets( ) function. As said earlier, it gets a string from the keyboard. It is terminated when an Enter key is hit. Thus, spaces and tabs are perfectly acceptable as part of the input string. More exactly, gets( ) gets a newline (\n) terminated string of characters from the keyboard and replaces the \n with a \0.
The puts( ) function works exactly opposite to gets( ) function. It outputs a string to the screen.
Here is a program which illustrates the usage of these functions:
main( )
{
char footballer[40] ;
puts ( "Enter name" ) ;
gets ( footballer ) ; /* sends base address of array */
puts ( "Happy footballing!" ) ;
puts ( footballer ) ;
}
{
char footballer[40] ;
puts ( "Enter name" ) ;
gets ( footballer ) ; /* sends base address of array */
puts ( "Happy footballing!" ) ;
puts ( footballer ) ;
}
Following is the sample output:
Enter name
Jonty Rhodes
Happy footballing!
Jonty Rhodes
Happy footballing!
Jonty Rhodes
Why did we use two puts( ) functions to print “Happy footballing!” and “Jonty Rhodes”? Because, unlike printf( ), puts( ) can output only one string at a time. If we attempt to print two strings using puts( ), only the first one gets printed. Similarly, unlike scanf( ), gets( ) can be used to read only one string at a time.
No comments:
Post a Comment