Formatted Input (scanf) in details with example in C Languge - Part 2

Inputting Real Numbers

Unlike integer numbers, the field width of real numbers is not to be specified and therefore scanf reads real numbers using the simple specification %f for both the notations, namely, decimal point notation and exponential notation.

 For example, Scanf("%f %f %f", &x, &y, &z); 
 	  with the input data 475.89 43.21E-1 678 

will assign the value 475.89 to x, 4.321 to y and 678.0 to z.

The input field may be separated by any arbitrary blank spaces.

If the number to be read is of double type then the specification should be % if instead of simple %f. A number may be skipped using % * f specification.

Inputting Character Strings

We have already seen how a single character can be read from the terminal using the getchar() function. The same can be achieved using the scanf function also. In additional, a scanf() function can input strings with more than one character. Following are the specifications for reading character strings.

 %ws or %wc 

The corresponding argument should be a pointer to a character array. However, %c may be used to read a single character when the argument is a pointer to a char variable.

Note : Note that the specification %s terminates reading at the encounter of a blank space. Some versions of scanf support the following conversion specifications for strings.

 % [ characters ] 
% [ ^ characters ]

The specification %[charactes] means that only the characters specified within the brackets are permissible in the input string. If the input string contains any other character, the string will be terminated at the first encounter of such a character. The specification %[^characters] does exactly the reverse. That is, the characters 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 characters.

Reading Blank Spaces :

We have earlier seen that %s specifier can not be used to read strings with blank spaces. But, this can be done with the help of %[] specification. Blank spaces may be included with the brackets, thus enabling the scanf to read strings with spaces. Remember that the lowercases and uppercases letters are distinct.

Inputting Mixed Data Types

It is possible to use one scanf statement to input a data line containing mixed mode data. In such cases, care should be taken to ensure that the input data items match control specifications in order an type. When an attempt is made to read an item that does not match the type expected, the scanf unction does not read any further and immediately returns the values read.

 The statement scanf("%d %c %f %s", &count, &code, &ratio, name); 

will read that data 15 p 1.575 coffee correctly and assign the values to the variables in the order in which they appear. Some systems accept integers in the place of real numbers and vice versa, and the input data is converted to the type specified in the control string.

Note : A space before the %c specification in the format string in necessary to skip the white space before p.

Detection of Errors in Input

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

 For example : scanf("%f %f %s", &a, &b, name); 

will return the value 3 if the following data is typed in : 20 150.25 motor
and will return the value 1 if the following line is entered 20 motor 150.25

This is because the function would encounter a string when it was expecting a floating-point value, and would therefore terminate its scan after reading the first value.

Rules for scanf() function :

  • Each variable to be read must have a filed specification.
  • For each field specification, there must be a variable address of proper type.
  • Any non- whitespace character used in the format string must have a matching character in the user input.
  • Never end the format string with whitespace. It is a fatal error !
  • The scanf reads until :
    1. A white space character is found in a numeric specification, or
    2. The maximum number of characters have been read or
    3. An error is detected, or
    4. The end of file is reached

Previous : Formatted Output printf

Post a Comment

0 Comments