Console I/O Functions
The screen and keyboard together are called a console. The basic function of Console I/O is to receive input from keyboard and write output to VDU. Console I/O functions can be further classified into two different categories-Formatted and Unformatted console I/O function.The basic difference between them is that formatted functions allow the input read from the keyboard or the output displayed on the VDU to be formatted as per requirements.For example,if values of average marks and percentage marks are to be displayed on the screen, then the details like where this output would appear on the screen, how many spaces would be present the two values,the number of places after the decimal points, etc can be controlled using formatted functions.The functions available under formatted and unformatted I/O categories are shown in the figure below:
Formatted Console I/O Functions:
As can be seen in the above figure,the functions printf( ),andscanf( )falls under the category of formatted consoleI/O functions.These functions allow us to supply the input in a fixed format and let us obtainoutput in the specified form.Therefore the formatted console I/O function can force the user to receive input in a fixed format and display the output in a fixed format.
Sprint( ) and sscanf( ) Functions:
The sprint ( ) function works similar to the printf( )function except for one small difference.Instead of sending the output to the screen as printf( )does,this function writes the output to an array of characters.The following program illustrates this.
# include <stdio.h>
Void main( )
Int i= 10;
char ch= A
printf (\n%d %c %f,i,ch,a);
sprint (str,%d %c %f,i,ch,a);
In this program,the printf( )prints out the values of I,ch and a on the screen,whereas sprint( ) stores these values in the character array str.Since the string str is present in the memory,what is written into str using sprint( )doesnt get displayed on the screen.Once str has been built,its contents can be displayed on the screen.
Unformatted Console I/O Functions:
These are several standard library functions available under the category-those can deal with a single character and those can deal with a string of characters..Many a times, the scanf( )function has one glaring weakness i.e you need to hit the Enter key before the function can digest what have you 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 hit.getch( ) and getche( ) are the two functions which serve this purpose.These functions return the character that has been most recently typed.Thee in getche( ) function means it echoes(displays) the character that you typed to the screen.getchar( )works similarly and echoes the character that you typed on the screen but unfortunately require Enter key .The difference between getchar( ) and fgetchar( ) is that former is a macro and latter is a function.The prototypes of getch( )and getche( )are present in the header file conio.h whereas the prototypes for getchar( ) and fgetchar( )are present in stdio.h .
Here is the sample program of these functions:
Void main( )
printf(\nPress any key to continue)
getch( );/*will not echo the character*/
printf(\Type any character);
ch=getche( ); /# will echo the character typed*/
printf( \n Type any character ) ;
getchar( );/*will echo character,must be followed by enter key*/
fgetchar( );/* will echo character must be followed by enter key*/
We at Transtutors.com are aware of the complexities of this topic, and therefore our online computer science tutors are available round the clock to help you. We offer a range of services
Online homework help - to help students with the homework related to computer science
Online assignment help to help students with their assignments.
So if you are you in need of computer science Homework Help/ computer science Assignment Help? Our competent pools of computer science professionals are available 24*7 to give you a solution to your problem