INPUT/OUTPUT FUNCTIONS
I/O FUNCTIONS: Reading the data from the input devices and displaying the results on the screen, are the two main tasks of any program. To perform these tasks user friendly ‘c’ has a number of i/p and o/p functions. When a program needs data, it takes the data through i/p functions and send results obtained through the o/p functions. Thus, the I/o functions are the link b/w the user and terminal.
TYPES OF I/O:
(1)Console I/O functions- functions to receive i/p from keyboard
and write O/P to standard O/P unit (usually the screen).
(2)Disk I/O functions - functions to perform I/O operations on a
floppy disk (or) hard disk.
(3) Port I/O functions-
functions to perform I/O operator on various ports.
The main
difference b/w formatted and unformatted I/O is that the
formatted functions allow the I/P read from the keyboard (or)
O/P displayed on O/P device to be formatted as per our
requirements.
Formatted I/O functions:
The formatted I/O functions read and write all type of data
values.
General form of printf is
printf (“format
string”, list of variables);
Format string can contain
-Characters that are simply printed as they are
-Conversion
specifications that begin with a % sign
-Escape sequences
that begin with a\sign.
%d: Decimal integer
%f: Floating point decimal
%lf:
double
%u: Unsigned integer
%i:
Signed integer
%c: Signed character
%s: Character string
The %d and %f that will be
used in printf() are called conversion characters.
We can
provide following optional specifies in the conversion
specifications.
Example program:
#include<stdio.h> #include<conio.h> main() { int weight=63; printf("\n weight is %d kg",weight); } output:- weight is 63 kg
Scanf (): This function allows us to enter data from key board that will be formatted in a certain way.
General form:
scanf (“format string”, list of address of
variables);
Note: we are sending address of variables (address are obtained by using ‘&’ the ‘address of ‘operator) to scanf () function. This is necessary because the variables received from keyboard must be dropped into variables corresponding to these addresses. The values that are supplied through the keyboard must be separated by blanks, tabs (or) newlines.
Do not include these escape sequences in format string.
Unformatted I/O functions:-
The unformatted I/O functions only work with the character data
type. They do not require conversion symbol for identification
of data types because they work only with character datatype.In
case values of other data types are passed to these functions
are treated as the character data type.
So far, for i/p we
have consistently used the scanf () function. However, for some
situations the scanf() function has one glaring weakness…you
need to hit the Enter key before the function can digest what
you have typed. How ever, 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 serves this purpose. These functions
return the character that has been most recently typed. The ‘e’
in getch() 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 ( ):It works similarly and echoes
the character that you typed on the screen, but unfortunately
requires “enter” key to be typed following the character that
you typed.
getch( ) and getche( ):These functions
read any alpha numeric characters from the standard input
device. The character entered is not displayed by getch ( )
function.
#include <stdio.h > #include <conio.h > main( ) { clrscr(); printf("enter two alphabets"); getche(); getch(); }
output: Enter any two alphabets A
Explanation: In the above program even though the two characters are entered, the user can see only one character on the screen. The second character is accepted but not displayed on the console. The getche ( ) accepts and display the character where as getch ( ) accepts but does not display the character.
getchar( ):The simplest all input operations is reading a
character from the standard input (usually keyboard) and writing
it to the standard output unit(usually the screen).reading a
single character can be done by using function getchar(This can
also be done with the help of scanf function. The “getchar”
takes the following form.
Variable-name=getchar ( );
Variable-name is a valid C name that has been declared as “Char
type”. When this statement is encountered the computer waits
until a key is pressed and then assigns this character as a
value to getchar function. Since getchar issued on the right
hand side of an assignment statement, the character value of
getchar is in turn assigned to the variable name on the left.
char ch;
ch=getchar( );
putchar ( ): This
function is used for writing characters one at a time to the
terminal.
It takes the form as shown below:
putchar(variable name);
Where variable name is a type char
variable containing character. This statement displays the
character contained in the variable name at the terminal.
Ex: ch=’a’;
putchar (ch);
will display character ‘a’ on
the screen.
The statement putchar (‘\n’);
would cause
the cursor on the screen to the beginning of the line.