Testing.

Tuesday, 4 September 2018

C Turorial : Buffer meaning in C progamming language.


  • Temporary storage area is called as buffer.
  • In implementation when we are passing more than required number of input values then automatically remaining values are stored into standard input buffer and these values automatically will pass to next input functionality.    
Example:
         int main()
         {
              int value1, value2;
              clrscr();
              printf ("\n Enter Value-1: ");
              scanf("%d", &value1);
              printf ("\n Enter Value-2");
              scanf("%d", &value2);
              printf("\n Sum of Value1+Value2 = %d", value1+value2);
              getch();
              return 0;
         }
Output:
         Enter Value-1: 10  20
         Enter Value-2: 
         Sum of Value1+Value2 = 30
  • In implementation when we require to remove temporary data form standard input buffer then go for flushall() or fflush().
flushall():-
  • flushall() is predefined function which is declared in <stdio.h>, by using this function we can remove the data from standard input buffer.
  • When we are working with flushall() it doesn't return any value and doesn't take any parameter.
Syntax:-
      void flushall(void);

fflush():-
  • fflush() is predefined function declared in <stdio.h>, by using this function we can clear standard input buffer data.
  • fflush() require one argument of type FILE* and doesn't returns any value.
Syntax:-
      void fflush(FILE*stream)

Example: 
          #include<stdio.h>
          #include<conio.h>
          int main()
         {
              int value1, value2;
              clrscr();
              printf ("\n Enter Value-1: ");
              scanf("%d", &value1);
              printf ("\n Enter Value-2");
              flushall(); //fflush(stdin);
              scanf("%d", &value2);
              printf("\n Sum of Value1+Value2 = %d", value1+value2);
              getch();
              return 0;
         }
Output:
         Enter Value-1: 10  20  30  //here only 10 will be accepted by compiler 20 and 30 will be  
         Enter Value-2: 30              // cleared by flushall()
         Sum of Value1+Value2 = 40

1 comment: