Unformatted Console I/O Functions in C programming language | applied electronics engineering

Latest

Unformatted Console I/O Functions in C programming language

By Applied Electronics - Saturday, March 11, 2017 No Comments
In earlier tutorial we explained that in C programing language we can classify I/O functions into formatted and unformatted input/output functions. There we showed some examples of formatted input output functions, namely- printf and scanf, sprintf and sscanf functions. Here we explain what are Unformatted Console I/O Functions in C programming language are and provide some examples of them.

See earlier part of the tutorial:
- how to display information and read user input in C programming language
- how sprintf and sscanf works

Below the classification of console input/output functions is reshown.



The difference between formatted and unformatted input/output is that using formatted input/output functions like printf and scanf the user can specify how the strings, characters and integers etc are to be displayed on the display. Example includes creating columns and rows, creating spaces between variables to be displayed etc. The unformatted functions does not have the capability to allow users to create such displays.

Again within unformatted I/O functions we can further classify the functions depending upon whether single or multiple character can be read and written. That is those that can deal with a single character and those that can deal with a string of characters.

Unformatted I/O for single character

getch( ), getche( ) and fgetchar( )

First let us look at those which handle one character at a time. But before that, lets look at how the scanf formatted function worked. With scanf the users need to hit the Enter key before the function can take in what they user typed in. Often we want a function that will read a single character at the instant it is typed in without waiting for the user to hit the Enter key.

The getch( ) and getche( ) are two functions which serve this purpose. These are input functions which is similar to scanf but the difference is that as soon as the user types in the input the functions return the character.

The ‘e’ in getche( ) function means echo and it echoes, that is, displays the character that you typed to the screen. Whereas, the getch( ) just returns the character that you typed without echoing it on the screen.

The getchar( ) works similarly and echo’s the character that you typed on the screen, but unfortunately the difference is that it 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.

Below is an example 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 */
}

The output of this program is shown below.

Press any key to continue
Type any character B
Type any character W
Continue Y/N Y

putch( ), putchar( ) and fputchar( )


 Next we look at the unformatted output functions putch( ) and putchar( ). These are used to display characters onto the display. They print a character on the screen. Again the working of putch( ) putchar( ) and fputchar( ) are exactly the same.

The following C program illustrates this.
main( )
{
char ch = 'A' ;
putch ( ch ) ;
putchar ( ch ) ;
fputchar ( ch ) ;
putch ( 'Z' ) ;
putchar ( 'Z' ) ;
fputchar ( 'Z' ) ;
}

The output of this program is below.

AAAZZZ

The limitation of putch( ), putchar( ) and fputchar( ) is that they can output only one character at a time.

Until now we have been talking about the class of unformatted I/O functions that takes and displayes one character at a time. Now we turn to unformatted I/O functions that takes in displays string of characters. These are gets() and puts() functions.

Unformatted I/O for string

gets( ) and puts( )

gets( ) is used to take in string input from the keyboard. It is different and better than scanf() function. That is because the scanf( ) function terminates its execution immediately after you have typed in space between words. That is, consider an example when you want user to enter his/her full name. Once the space key has been entered after the first name, the scanf() functions thinks the input has ended and so there is no way to enter the next last name. That is there is no way to enter strings as input. See the following example C program.

main( )
{
char name[50] ;
printf ( "\nEnter name " ) ;
scanf ( "%s", name ) ;
printf ( "%s", name ) ;
}



The output is,

Enter name Jonty Rhodes
Jonty

You should notice that there is no "Rhodes" at the output. This is because of how scanf works. The scanf executed immediately when you hit the space key. Hence the last name Rhodes was never saved in the array name[ ]. As a result with scanf() there is no way to enter a multi-word string into a single variable.

The solution to this problem is to use gets( ) function. This function is to read in 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 ) ;
}
Following is the sample output:
Enter name
Jonty Rhodes
Happy footballing!
Jonty Rhodes

In the above C program code we used 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.

See Download The C Programming Language PDF free book written by Brain W.Kernighan and Dennis M.Ritchie.

Tags:

No Comment to " Unformatted Console I/O Functions in C programming language "