Console I/O function

Console I/O refers to the operation that occur at the keyboard and the screen of your computer.
Console I/O function can also be classified in two parts:
1. Formatted console I/O:
Formatted I/O function accepts or present in a particular format. The example of formatted console I/O function is printf( ) and scanf( ).
printf
It is highly desirable that the output are presented in such a way that they are understandable and are in a form easy to use.
The printf() statement provides certain features through which the screen output is effectively controlled.
The general form of printf() function is:
printf(“Control String “,arg1,arg2….);
Control string may contain:
1. character that are simply printed as they are.
2. Conversion specification that begin with a sign.
3. Escape sequence that begin with \ sign.
Given below is a list of conversion charcter that can be used with printf() function:
scanf
scanf allows formatted reading of data from the keyboard. Like printf it has a control string, followed by the list of items to be read.
However scanf wants to know the address of the items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by the & sign.
Character strings are an exception to this. Since a string is already a character pointer, we give the names of string variables unmodified by a leading &.
Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalents.
2. Unformatted console I/O:
This function cannot control the format of reading and writing the data. some of the example of this function are getch( ),getche( ),getchar( ),gets( ),putchar( ),putch( )and puts( ).
getchar
getchar returns the next character of keyboard input as an int.
If there is an error then EOF (end of file) is returned instead. It is therefore useful to compare this value against EOF before using it.
If the return value is stored in a char, it will never be equal to EOF, so error conditions will not be handled correctly.
As an example, here is a program to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control – d.
Output of the program:
putchar
putchar puts its character argument on the standard output (usually the screen).
The following example program converts any typed input into capital letters.
To do this it applies the function toupper from the character conversion library ctype.h to each character in turn.
An example program using putchar.
Output of the program:
gets
gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines. When all input is finished, NULLas defined in stdio.h is returned
puts
puts writes a string to the output, and follows it with a newline character.
An example program which uses gets and puts to double space typed input
Out put of the program
Note: putchar, printf and puts can be freely used together. So can getchar, scanf and gets.