Formatted Input (scanf) in details with example in C Language


Formatted Input :

Formatted input refers to an input data that has been arranged in a particular format. For example, consider the following data : 15.75 123 John

This line contains three pieces of data, arranged in a particular form. Such data has to be read conforming to the format of its appearance. For example, the first part of the data should be read into a variable float, the second into int, and the third part in to char. this is possible in C using the scanf function. (scan means scan formatted)

Now, we shall see all the options that are available for reading the formatted data with scanf function.

General form of scanf : scanf("control string", arg1, arg2,...argn);

The control string specifies the field format in which the data is to be entered and the arguments arg1, arg2,...argn specify the address of locations where the data is stored. Control string and arguments are separated by commas.

Control string (also known as format string) contains field specifications, which direct the interpretation of input data.

It may include :

  • Field (or format) specifications, consisting of the conversion character %, data type character (or type specifier), and an optional number, specifying the field width.
  • Blanks, tabs or newlines.

Blanks, tabs and newlines are ignored. The data type character indicates the type of data that is to be assigned to the variable associated with the corresponding argument. The field width specifier is optional.

Different ways to read the input data using scanf() :

  1. Inputting Integer Numbers
  2. Inputting Real Numbers
  3. Inputting Character Strings.
  4. Inputting Mixed Data Types.

1. Inputting Integer Numbers :

The field specification for reading an integer number is : %w sd.

The percentage sign (%) indicates that a conversion specification follows. w is an integer number that specifies the field width of the number to be read and d known as data type character, indicates that the number to be read in integer mode.

Consider the following example :

scanf("%ed %5d", &num1, &num2);
input Data1 : 50 31426

The value 50 is assigned to num1 and 31426 to num2.

input Data2 : 31426 50.

The variable num1 will be assigned 31(because of % 2d) and num2 will be assigned 426(unread part of 31426). The value 50 that is unread will be assigned to the first variable in the next scanf call. This kind of errors may be eliminated if we use the field specifications without the field width specifications.

the statement scanf("%d %d", &num1, &num2); will read the data 31426 50 correctly and assign 31426 to num1 and 50 to num2.

Input data items must be separated by spaces, tabs or newlines. Punctuation marks do not count as separators. When the scanf functions searches the input data line for a value to be read, it will always bypass any white space characters.

If we enter a floating point number instead of an integer, the fractional part may be truncated, also scanf may skip reading further input.

When the scanf reads a particular value, reading of the value wil be terminated as soon as the number of characters specified by the field width is reached (if specified) or until a character that is not valid for the value being read is encountered. In the case of integers, valid characters are an optionally signed sequence of digits.

An input field may be skipped by specifying * in the place of field width.

For example, the statement scanf("%d %*d %d", &a, &b)

will assign the data 123 456 789 as follows :

123 to a
456 skipped(because of *)
789 to b

Note 1 : It is legal to use a non - whitespace character between field specifications. However, the scanf expects matching character in the given location.

For example, scanf("%d - %d", &a, &b);

accepts input like 123 - 456 to assign 123 to a and 456 to b.

0 Komentar untuk "Formatted Input (scanf) in details with example in C Language"

Back To Top