Thursday, October 17, 2013

Field width specifiers


Now, look at the scanf() function we used in our program.
scanf(“%3d%c%2d”,&num1,&point,&num2);

Here, 2 and 3 before the conversion character %d are called field width specifier. The general format for field width specifier for integer is:

%wd

Where, w is the width specifier and d is the conversion character for integer. Consider the following section of code:

..
printf(“\nEnter any three digit number:”);
scanf(“%3d”,&num);

Here, if you supply a number 12345, then the scanf() function will accept only first three digits from the inputted digits i.e. 123 and the remaining digits i.e. 45 remains unread. If your program contains another scanf(), then unread part is used as an input of the scanf(). To avoid confusion, consider the following program.

Program 29:
#include <stdio.h>
#include <conio.h>
void main()
{
    int num1,num2;
    clrscr();
    printf(“Enter any two digit number:”);
    scanf(“%2d”,&num1);
    printf(“Enter any three digit number:”);
    scanf(“%3d”,&num2);
    printf(“\nThe first number is=%d”,num1);
    printf(“\nThe second number is=%d”,num2);
    getch();
}

What happens if you enter 6 digit number, instead of 2 digits here?

#-------------------------------------------------------------------------------#
 Enter any two digit number:123456
 Enter any three digit number:
 The first number is=12
 The second number is=345
#-------------------------------------------------------------------------------#
Our inputted six digit number is 123456. The compiler assigns first two digits i.e. 12 to num1. The unread part of the number i.e. 3456 is then passed to the next scanf(). The next scanf() will receive only three digit. So, 345 is assigned to the num2 and the unread part of 3456 i.e. 6 remains unread.

Conclusion is that, if your specified field width is 3 then, it will receive maximum three digits. So, use field width specifier only when it is necessary otherwise it causes the unexpected behavior of the program.

You can use %wc character specifier to read in the next n characters, including white space character. So, to read 5 characters from the keyboard, you can use %5c format specifier. The following code illustrates this:

Program 30:
#include <stdio.h>
#include <conio.h>
void main()
{
    char str[6];
    clrscr();
    printf(“Enter five characters without any space:”);
    scanf(“%5c”,&str);
    str[5]=’\0’; /* String terminator character.*/
    printf(“\nYour entered characters are:%s”,str);
    getch();
}

#-------------------------------------------------------------------------------#
 Enter five characters without any space:Trish
 Your entered characters are:Trish
#-------------------------------------------------------------------------------#
Here, str is a character array which can hold maximum 5 characters. Likewise, the %wf format is used to read in the float value of specified field width.
Program 31:
#include <stdio.h>
#include <conio.h>
void main()
{
    float f;
    clrscr();
    printf(“Enter any three digit float:”);
    scanf(“%3f”,&f);
    printf(“\nYour entered float is=%f”,f);
    getch();
}

#-------------------------------------------------------------------------------#
 Enter any three digit float:123
 Your entered float is:123.000000
#-------------------------------------------------------------------------------#
Warning: If you want to receive floating point number from the keyboard say 123.45, then don’t use %3.2d format specifier, it won’t work. Such field width specifier can be used with the printf() statement only to control the output stream.

If you want to receive comma separated list of numbers, then you can use the scanf() like this:
printf(“Enter three numbers separated by commas:”);
scanf(“%d,%d,%d”,&num1,&num2,&num3);

If you want to receive input like this say 50%, then you can do so by using the following statement.
printf(“Enter percentage marks (e.g. 80%):”);
scanf(“%d%”,&marks);

You can skip or ignore the input items by using “*” before format specifier as follows.

Program 32:
#include <stdio.h>
#include <conio.h>
void main()
{
    int i1,i2;
    float f1,f2;
    clrscr();
    printf(“Enter two integers:”);
    scanf(“%d%*d”,&i1,&i2);
    printf(“Enter two floats:”);
    scanf(“%f%*f”,&f1,&f2);

    printf(“\nYour entered ..”);
    printf(“\n%d\n%f”,i1,f1);
    getch();
}

#-------------------------------------------------------------------------------#
 Enter two integers:100 200
 Enter two floats:3.15 2.71
 You entered..
 100
 3.150000
#-------------------------------------------------------------------------------#
Like %wc format specifier, you can use %ws format specifier to receive specified number of characters from the keyboard.

Program 33:
#include <stdio.h>
#include <conio.h>
void main()
{
    char name[30];
    clrscr();
    printf(“Enter name:”);
    scanf(“%2s”,name);
   
    printf(“\nYour name is %s”,name);
    getch();
}

#-------------------------------------------------------------------------------#
  Enter name:Sheena
  Your name is Sh

#-------------------------------------------------------------------------------#

No comments:

Post a Comment