Saturday 7 June 2014

Standard Input Output Functions



Standard Input Output Functions

The functions which are used for data input and output are called standard input output functions.
    These functions are divided into two categories :
   1.   Formatted
   2 .   Unformatted

Formatted console I/O functions
         These functions are used to input data from a standard input device as well as output is sent to a standard output device and is formatted as per the requirements of the user i.e. spacing between two values, place where we want the data on the screen, no. of decimal places in an output etc. In these functions scanf ( ) function is used for input and printf ( ) is used for output.
More about scanf ( ) and printf ( ) is given below:

scanf  ( ) function  
The scanf function is used to accept input data from the standard device i.e. keyboard in a fixed format. This function falls under the category of formatted console I/O functions. Its syntax is given below:
 
scanf (“format string”,arguments);


Format String
         Format string contains the format specifies which begin with character “%” and another character selected from the following table depending upon which type of data user wants to input, e.g. %d for signed decimal integer etc.
Format specifier
         Input types
       Data type
%d or %i
Short signed Integer
        Integer
%u
Short Unsigned Integer
        Integer
%Id
Long Signed Integer
        Integer
%Iu
Long Unsigned Integer
        Integer
%x
Unsigned Hexadecimal Integer
        Integer
%o
Unsigned Octal Integer
        Integer




Format specifier
        Input types
         Data type
%f
Single precision float
    Float
%If
Double Precision float
    Float

Format specifier
      Input types
       Data type
%c
Signed character
    Character
%c
Unsigned Character
    Character

Format specifier
     Input types
      Data type
%s
  String
   String

Arguments
 The Arguments tells or specify where the input data is to be stored while receiving it from the standard input device. There must be an argument for each input data, the extra arguments are ignored and if there are too few arguments, the results are unpredictable. The arguments to the scanf function are pointers. So argument must be preceded by an ampersand (&) i.e. address of operator and entered data from keyboard is stored at an address in the memory.
    Input data must be supplied strictly according to the specified format string otherwise it will show strange results.

E.g. scanf (“%d %f %c”, &a, &b, &c);

      In this example there are three format strings %d for integer we can see that in the table given above %f for float and %c for character and the corresponding arguments a, b  and c and these arguments are preceded by & operator.
Printf ( ) function
This function is used to output data from the computer onto a standard output device in a fixed format. It is also a formatted I/O function. Its general form is
Printf (“Format string”, arguments);
   Format String In printf function format string can contain
a)   Characters that are simply printed as they are
b)Format specifier that begin with %sign as shown in the table given above
c)   Escape sequences that begin with a \ sign.

Source window
Output Window


Here in this blog we only discuss formatted console I/O functions.
In next blog we will discuss Unformatted I/O functions.

No comments:

Post a Comment