Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Detection of errors in scanf input

Posted By: George Evans     Category: C Programming     Views: 5076

program illustrates the testing for correctness of reading of data by scanf function.

Code for Detection of errors in scanf input in C Programming

   main()                                                      
   {                                                           
       int a;                                                  
       float b;                                                
       char c;                                               
       printf("Enter values of a, b and c\n");                 
       if (scanf("%d %f %c", &a, &b, &c) == 3)                 
          printf("a = %d  b = %f   c = %c\n" , a, b, c);       
       else                                                    
          printf("Error in input.\n");                         
   }                                                           
Output      Enter values of a, b and c                                  
   12  3.45  A                                                 
   a = 12  b = 3.450000   c = A                                
   Enter values of a, b and c                                  
   23  78  9                                                   
   a = 23  b = 78.000000   c = 9                               
   Enter values of a, b and c                                  
   8  A  5.25                                                  
   Error in input.                                             
   Enter values of a, b and c                                  
   Y  12  67                                                   
   Error in input.                                             
   Enter values of a, b and c                                  
   15.75  23  X                                                
   a = 15  b = 0.750000   c = 2                                                                                    
  
Share: 


Didn't find what you were looking for? Find more on Detection of errors in scanf input Or get search suggestion and latest updates.

George Evans
George Evans author of Detection of errors in scanf input is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!