Input and Output – Part IV (Formatted I/O – Integer Data)

We have already known what conversion specifiers, how to read input data from the user and how to write an output data to the console. Though we can perform read and write operations without any particular format just by following the particular syntax, we can follow format specifications through which we can obtain the better presentation of the result. Formatted input and output mean that data is entered and displayed in a particular format. We have a different format for different specifications. Now it’s time to know about them. Let’s get started with the integer data.

Formatted Input Output in C (Integer Data)

Format for integer input

%wd

Here, ‘d’ is the conversion specification character for an integer value and ‘w’ is an integer number specifying the maximum field width of input data. If the length of the input is more than this maximum field width then the values are not stored correctly.

For example

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

  • When input data length is less than the given field width, then the input values are unaltered and stored in given variables.

Input-

7                40

Result-

7 is stored in a and 40 is stored in b.

  • When input data length is equal to the given field width, then the input values are unaltered and stored in given variables.

Input-

38              590

Result-

38 is stored in a and 590 is stored in b.

  • When input data length is more than the given field width, then the input values are altered and stored in the variable as-

Input-

389            4580

Result-

38 is stored in a and 9 is stored in b and the rest of input is ignored.

C program to demonstrate above example is


#include<stdio.h>

#include<conio.h>

int main(){

int a,b,c,d,e,f;

printf("Enter six integers value (a,b,c,d,e,f) :\n");

scanf("%2d%3d%2d%3d%2d%3d",&a,&b,&c,&d,&e,&f);   //reading integer data in particular format

printf("Stored integer values for variables are\na = %d\nb = %d\nc = %d\nd = %d\ne = %d\nf = %d\n",a,b,c,d,e,f);

getch();

return 0;

}

Its output will be as:

Output for above program (Formatted Integer Input)

Format for integer output

%wd

Here w is the integer number specifying the minimum field width of the output data. If the length of the variable is less than the specified field width, then the variable is right justified with leading blanks.

For example-

printf(“a=%3d, b=%4d”,a,b);

  • When the length of variable is less than the width specifier.

Input-

89              0

Output-

A = 8 9 , b = 0

 

The width specifier of first data is 3 while there are only 2 digits in it, so there is one leading blank. The width specifier of second data is 4 while there is only 1 digit, so there are 3 leading blanks.

  • When the length of the variable is equal to the width specifier.

Input-

374            2052

Output-

a = 3 7 4 , b = 2 0 5 2
  • When the length of the variable is more than the width specifier, then also the output is printed correctly.

Input-

3702          20523

Output-

a = 3 7 0 2 , b = 2 0 5 2 3

C program to demonstrate above example is:


#include<stdio.h>

#include<conio.h>

int main(){

int a=89,b=0,c=374,d=2052,e=3702,f=20523;

printf("Printed integer values for variables with format specifications are\na = %3d\nb = %4d\nc = %3d\nd = %4d\ne = %3d\nf = %4d\n",a,b,c,d,e,f);

getch();

return 0;

}

Its output will be as:

Output for above program (Formatted Integer Output)

Leave a comment