Saturday 6 January 2018

Reading Mixed Data types | c language tutorial for beginners |how to study c programming language


Reading Mixed Data types
Reading Mixed Data types

It is possible to use one scanf statement to input a data line containing mixed mode data.In such cases,core should be exercised to ensure that the input data items match the control specifications in order and type.
      The statement         
      scanf("%d%c%f%s",&count,&code,&ratio,&name);
      will read the data
         14    s      14.345    john


Detection of errors in input: When a scanf function completes reading its list,it returns the value of items that are successfully read. This value can be used to test whether any errors occurred in reading the input


Example: scanf("%d%f%s",&a,&b,&name);
         will return the value 3 if the following data is typed in:      
           20    150.35   
cycle will return the value 1 if the following line is typed in: 
           20    cycle      150.35 

This is because the function would encounter a string when it was expecting a floating point value and would therefore terminates its scan after reading the first value.Some versions of scanf support the following conversion specifications for strings.%[characters] and %[^characters] The specification %[character] means that only the characters specified within brackets are permissible in the input string.If the input string contains only other character,the string will be terminated at the first encounter of such a character. The specification of%[^characters] does exactly the reverse. The character specified after the circumflex(^) are not permitted in the input string.The reading of the string will be terminated at the encounter of one of these character %s specifier cannot be used to read string with blank spaces. But,this can be done with the help of %[] specification. Blank spaces may be included within the brackets,this enabling the scanf to strings with spaces 


0 comments:

Post a Comment