Inputting Integer Numbers

The field specification for reading an integer number is:

% w d

Shows the field specification for reading an integer number


img

For example:

scanf (“%3d %4d “, &a, &b);

If the data entered is 175   3245

then the value 175 will be assigned 324 (because of %3d ) and b will be assigned 5 i.e., the unread part of 3245. The value 175 that is unread will be assigned to the scanf call. This kinds of error may be eliminated if we use the field specifications without the file width specifications. For example:

scanf (“%d %d”, &a, &b);

will read the data 3245 and 175 correctly and assign 3245 to a and 175 to b.

If we enter a floating – point number instead of an integer then the fractional part may be stripped away. Also scanf may skip reading further input.

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

scanf (“%d %d* %d”, &a, &b);

If the data entered is 345 567 123

then it will assign 345 to a, 567 skipped (because of*) and 123 to be.

The data type character d may be preceded by l to read long integers. For example:

scanf (“%ld”, &fact);


Program - Illustration for reading integer numbers
#include <stdio.h>
Void main ()
{
    int a, b, c, p, q;
    printf (“Enter three integer numbers\n”);
    scanf (“%d    %*d   %d”, &a, &b, &c);
    printf (“%d    %d   %d\n”, a, b, c);
    printf (“Enter two four digit numbers\n”);
    scanf (“%2d   %d”, &p, &q);
    printf (“%d   %d\n”, p, q);
    printf (“\nEnter two integers\n”);
    scanf (“%d   %d”, &a, &p);
    printf (“%d   %d\n”, a, p);
}

Output

Enter three integer numbers
6 7 8
6 8 8653
Enter two four digit numbers
1234 5678
12 35
Enter two integers
22 77
5678 22



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext