C-Input/ Output

 Input/Output in 'C'

Most languages have facilities for input and output, built into the langauge. The 'C' language does not have built in statement of I/O. It has function to achieve the I/O. The standard I/O functions (scanf ( ), printf ( ) ) are normally performed by using the "stdio.h" header file. It has "conio.h" header file to perform console I/O functions (gets1), puts ( ). getchar, etc.

Standard I/O functions

1. Scanf ( ). This function allows to enter data from key board that will be formatted in a certain way. The general form of scanf ( ) statement is 
scanf ("formatted string". List of addresses of variables); 
For example -- scanf ("%d%f,"&a, &b);

Here & denotes the address of operator, this is necessary because the values received from keyboard must be dropped into variables corresponding to these addresses.
%d and %f are format specifiers which work as conversion characters. Format specifiers used in 'C' are as follows.

Format Specifier Data type
% d,% i  Signed integer 
%u          Unsigned integer 
%ld, %li Long integer
%lu         unsigned long integer 
%hd, %hi Short integer
% hu unsigned short integer
%f         float
%lf, %le, %lg        double
%Lf, %Le, %Lg        Character 
%c        Character 
%s        string, group of characters

printf ( ) -This information writes formatted data to screen. This function allows to supply the input in a fixed format and to obtain the output in the specified form. The printf ( ) function interprets the contents of the formats string.

printf ("formatted string", variables);

Example 1.
printf ("Average=%d percentage=%f", avg.per);
Here the % d and %f are conversion characters.
They tell printf ( ) to print the value of avg as an integer and per as a float .

Example 2
printf ("Hello students How are you");
Here , t are escape sequences. Escape sequences used in 'C' are 

Escape Sequences Function
a                          beep (alert)
                          back space 
                        New line
                        Horizontal tab
\                         back slash 
,                         Single quote
"                         Double quote
V                        Vertical tab
?                        Question mark
000                        Code specified in octal
xhh                        Code specified in hexa decimal

Console I/O Functions

getch ( ) Reads a character from keyboard
getche ( ) Reads a character from keyboard and echoes it
getchar ( ) Reads a character from keyboard (macro version )
gets ( ) Reads a line from Keyboard
putch ( ) Writes a character to the screen 
putchar ( ) Writes a character to the screen (macro version)
puts ( ) Writes a line
scanf ( ) Reads formatted data from keyboard
printf ( ) Writes formatted data to screen