4.1 – Formatting input through scanf()

by subbu on September 10, 2013

Exploring scanf()

C language has no native statement to accept the data from keyboard. scanf() is a function defined in stdio.h, generally used to accept any type of data from the keyboard and store into the specified variables.
The scanf() has format string and the list of addresses of variables in which data have to store
If we don’t write and use scanf() properly some crazy things may happen. We must keep some general points in mind while using scanf()

The format specifiers used in format string must match with the variable in order. If the type of input from the keyboard doesn’t match with the format specifier then scanf() stops reading further.

#include<stdio.h>
int main()
{
int qty;
float price;
printf("Quantity:");
scanf("%d",&qty);
printf("Price:");
scanf("%f",&price);
printf("Bill:%f",price*qty);
return 0;
}

Execution 1:
Quantity:25             //reading integer value into int variable
Price:15.75              //reading floating point value into float variable
Bill:393.750000

Execution 2:
Quantity:56.75        //reading floating point value into int type variable
Price:Bill:42.000000 //it results skipping to read second value

#include<stdio.h>
int main()
{
int x;
scanf("%f",&x);/* using %f instead of %d to represent int*/
printf("x=%d",x);
return 0;
}

Output:
78
x=1117519872

#include<stdio.h>
int main()
{
float x;
scanf("%d",&x);/* using %d instead of %f to represent float*/
printf("x=%f",x);
return 0;
}

Output:
34.58
x=0.000000


Input data items must be separated by spaces or a tab and must match the variables receiving the input in the same order.
When searching for a value scanf() ignores spaces, tabs and line boundaries.
The reading will be terminated on reading mismatch data

#include<stdio.h>
int main()
{
int x;
float y;
scanf("%d%f",&x,&y);
printf("----------------\n");
printf("%d\n%f",x,y);
return 0;
}

Execution 1:
5                        //ignoring line boundaries while reading data
12.55
——————
5
12.550000

Execution 2:
567 123.876     //ignoring space while reading data
——————
567
123.875999

Execution 3:
45.67   53         //scanf() reads 45 into a, 67 into y and ignores 53
———————
45
0.670000


Reading fixed width integers:

The field specifier %wd is used to read fixed width integers. Here w is the width of data need to be read and store into a particular variable.
Any unread data in a line is considered as input to the next scanf() call.
Say for example 65345867 is the input to scanf(“%3d%2d”,&x,&y); then first three digits 653 is fed to x, 45 is fed to y and remaining part 867 is skipped from reading.

field width in scanf()

#include<stdio.h>
int main()
{
int x,y;
scanf("%3d%2d",&x,&y);
printf("------------------\n");
printf("%d\n%d",x,y);
return 0;
}

Execution 1:
234                 // reads 3digit integer into x with field width of 3 (%3d)
56                  // reads 2digit integer into y with field width of 2 (%2d)
——————
234
56

Execution 2:
345     78        // reads 3digit integer into x and 78 is input to next scanf() call
——————
345
78

Execution 3:
2654    87     // reads 3digit integer 265 into x and 4 is input to next scanf() call
——————
265
4

Execution 4:
6534           //reads 3digit integer 653 into x and 4 is input to next scanf() call
——————
653
4


Using flag to skip reading in scanf():

An input field can be skipped by placing * in place of field width.

#include<stdio.h>
int main()
{
int x,y;
scanf("%d%*d%d",&x,&y);
printf("----------------\n");
printf("x=%d\ny=%d",x,y);
return 0;
}

Output:
567
45                       //Skipped from reading as field width of second specifier is *
612
——————
x=567
y=612

Previous post:

Next post: