Input/Output Functions

Standard In/Output Function  :-
                                                            In almost every programming problem user need to enter some data to the program & want to get some specific result  from the program . In programming , the instruction used to perform the function of Data Input from standard input unit such as Keyboard and to get Output on standard Output unit such as Monitor are called Standard Input Output Functions .
In " C " language , these Input Output function can be categorise into two categories :-

1). Formated Input Output Function             2). Unformated Input Output Function

Let's try to explain them in details .


1). Formated Input Output Functions :-
                                                                   These function are used to Input data from a standard Input unit such as Keyboard and to get the result on standard output unit such as Monitor . As name suggest , these function follow a basic fix format .
There are two Input Output functions .
     1.    printf()                                                   2.   scanf()          
         ( Formated Output Function )                    ( Formated Input Function )

-----> 1.  printf() Function :-
                                             This is a Formated output function used to get printed output on screen . It follow a fixed format . Its general form is 
printf(" format string " , List of variables ) ;

format string ----> It can contain 
Characters , that are simply printed as they are written 
         eg.  printf(" Hello  How are you ?  ") ;
Conversion or format specifier that begin with  %  sign  
Escape sequencer begin with  \  sign 

Format specifiers :-
                                Format specifiers begin with  %  sign and are used to tell the compiler to print the value store in variable and also tell the data type format .

           

     Integer         

                 %d

Short Signed

                 %u

Short Unsigned

                 %ld

Long Signed

                 %lu

Long unsigned

                 %x

Unsigned Hexadecimal

                 %o

Unsigned Octal

   Float or Real

                 %f

Float

                 %lf

Double

     Character

                 %c

Signed Character

                 %c

Unsigned Character

       String

                 %s

String

We can also use optional format specifiers in the format string

   dd

Digits specifying field width .

eg. %8d print the value in 8 column field and is right justified ie. It insert blanks at the left .


  .

It separate the number of places after the decimal point .

eg. %8.3f print 8 column output

                                   3 digit after decimal point

                                   4 digit before decimal point

                                   one digit for decimal

  -

Minus sign is used for left justification of the output .

eg. % -8d print 8 column output and is left justification ie. blank are inserted on right side .

Escape  Sequencer :-
                                               The Format Specifier may also contain the escape sequencers starting with  \  sign  and are used to break the sequence of Output .
The table show different type of escape sequencer .

\n

New line

\t

Tab

\b

Back Space

\r

Carriage Return

\f

Form Feed

\a

Alert <CPU produce Beep>

\’

Single Quotes

\”

Double Quotes

\\

Back Slash

-----> scanf()  Function :-
                                             This is a Formated Input function used to enter data to the program . As printf()  , it also follow a specific format .

     scanf(" Format string " , List of Addresses of variables ) ;

       Format string -----> It contain format specifiers starting with  % sign , which tell the compiler the datatype of the Input .The following table show different format specifers

           

     Integer         

                 %d

Short Signed

                 %u

Short Unsigned

                 %ld

Long Signed

                 %lu

Long unsigned

                 %x

Unsigned Hexadecimal

                 %o

Unsigned Octal

   Float or Real

                 %f

Float

                 %lf

Double

     Character

                 %c

Signed Character

                 %c

Unsigned Character

       String

                 %s

String

List of addresses of variable  contain the memory addresses of variable where the Input is stored . The memory addresses of variable can be given from two methods 
----> First we put  & sign before the variable which give address of variable.
          eg.     scanf(" %d ",&num );
            In this example , &num will provide the address of variable num and the value enter is save variable num .
----> Secondly , we use Pointer variable , such as
           eg.    main()
                     {
                        int num ;
                        int *pt ;
                        pt = & num ;      /* Assining address of  num  to the pointer variable  pt */
                        scanf(" %d ", pt ) ;       /*  Puting pointer variable in the address list  */
                     }
In upper example , we can see pointer variable  pt  stores the address of variable num and that address is provided in address list .

We will discuss Pointers later in detail .


2). Unformated Input Output function :-
                                                            These are console Input Output Library function which deal with one character at a time and string function for Array of characters ( String ) .
The following table show different type of character and string Input Output function .

     Data type

   Function

Input / Output

 

  Character

getchar()

Input

getche()

Input

getch()

Input

putchar()

Output

putch()

Output

   String

gets()

Input

puts()

Output

1). getchar( ) function :-
                                       This is a un-formatted console Input function which is used to enter or Input One character at a time froma standard Input device eg. Keyboard . In this the entered character is echoed( display ) on the screen and the use need to press Enter key .
eg.        char alphabet ;
             alphabet = getchar() ;
/* In this , firstly we press a key which we want to enter (as say 'A') and we need to press Enter key to move to the next instruction */

let's see a demo how we use it in Array of characters.

main()                                                                                                                                                                          
{
   int i ;
   char name[ 10 ] ;
   printf(" Enter name \n ") ;
   for( i=0 ; i <= 10 ; i++ )
   {
    name[ i ] = getchar() ;
   }
}

Output .

Enter name
H    < press enter > 
A    < press enter >
R    < press enter >
S    < press enter >
I     < press enter >
M    < press enter >
R    < press enter >
A    < press enter >
N    < press enter >
_    < press enter >


2). getche() function :-
                                       This also comes in the unformatted console Input function which is used to enter ( input ) One character at a time . In this , the entered character is echoed / displayed on the screen but the user not need to press Enter key to submit the character . As the key is press , it is accepted from the program .
eg.   char alphabet ;
         alphabet = getche() ;
  /* In this , user only press the character key (say 'A') and is accepted */
let's see a demo how we use it in Array of characters.

main()
{
   int i ;
   char name[ 10 ] ;
   printf(" Enter name \n ") ;
   for( i=0 ; i <= 10 ; i++ )
   {
    name[ i ] = getche() ;
   }
}

3). getch() function :-
                                     This is also unformated console Input function which is used to enter one character at a time . In thi , the user need neither to press Enter key nor the character is echoed on screen . It is used where the user not want to show the Input . 
eg.     char alphabet ;
          alphabet = getch() ;
    /* In this , we need only to press the character key and it is immediately accepted */  
let's see a demo how we use it in Array of characters.

main()
{
   int i ;
   char name[ 10 ] ;
   printf(" Enter name \n ") ;
   for( i=0 ; i <= 10 ; i++ )
   {
    name[ i ] = getch() ;
   }
}
Output .

Enter name
HARSIMRAN_   <  This is your Input , It is not shown on the screen , It is only for your understanding >

4). putchar() function :-
                                       This function is unformatted console Character Output function which is use to print one character (at a time) on the screen . 
eg.  main()
       {
          char ch ;
          printf(" Enter a key ") ; 
          ch = getch() ;
          printf(" \n You have enter character ") ;
          putchar( ch ) ;
       }

5). putch() function :-
                                     This function is unformatted console Character Output function which is use to print one character (at a time) on the screen . 
eg.  main()
       {
          char ch ;
          printf(" Enter a key ") ; 
          ch = getch() ;
          printf(" \n You have enter character ") ;
          putch( ch ) ;
       }

    gets() function :-
                                This function is unformatted String Input function . This is used to enter a string ( Array of character ) from standard Input device (such as Keyboard) . The length of entering string is limitedfrom declaring string length (array) . With this function , the user can enter multiple word in the same time . Let's take example to describe this point .
Example :-  If we want to enter the NAME of a person , we use String for this purpose . As trying this with scanf() function , we write it as

main()
{
  char name[ 15 ] ;
  clrscr() ;
  printf(" Enter name of 15 Characters ") ;
  scanf(" %s " , name );           /* We NOT use & operator for String (Array of characters)*/    
  getch() ;
}
Output :-
Enter name of 15 Characters HARSIMRAN SINGH

In above example , we use scanf() function to enter characters in string . BUT there is a limitation of  scanf() , that it can only except characters till the user press Enter key or Space-bar key .
so if we try to Enter " HARSIMRAN SINGH " 
it only take " HARSIMRAN " as Input and it Ignore the input after space is given . For this reason we use unformatted string Input function ie. gets() function .
Now we write the above example with gets() .
main()
{
  char name[ 15 ] ;
  clrscr() ;
  printf(" Enter name of 15 Characters ") ;
  gets( name ) ;           /*  Unformatted String Input function  */

Output .
Enter name of 15 Characters HARSIMRAN SINGH 
This take " HARSIMRAN SINGH " as Input .


  puts() function :-
                              This is an Unformatted String Output function . This is used to Print the Content of string on the standard Output device such as Screen . 
let's discuss this function with the help of suitable example .
main()
{
  char name[ 15 ] ;
  clrscr() ;
  puts(" Enter name of 15 Characters ") ;
  gets( name ) ;                                             /*  String Input  */
  puts(" The name Entered is ") ;  
  puts(" name ") ;                                         /*  String Output  */
  getch() ;
}
Output .
Enter name of 15 Characters HARSIMRAN SINGH
The name Entered is HARSIMRAN SINGH

Who's online

We have 34 guests and one member online

About Us

Harsimran Singh

Hello

This website is a special gift for Programming Lovers , which want to learn basic Programming Languages and want to develop there skills Read More....

Our work

Find Us

This email address is being protected from spambots. You need JavaScript enabled to view it.

Harsimran Singh(Facebook)

Harsimran Singh(about.me)

Harsimran Singh(G+)