April 14, 2018

Formatted Input and Output in C


   Formatting means representation of data with different settings as per the requirement of the user. The various settings that can be done are number format, field width, decimal points etc.

Formats for different specifications are given below:


Format for Integer Input

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

Let's take some examples:

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

In this case variable, a has width of 2 and b has a width of 3.

a and b can be entered in the following ways:

Case 1: When input data length is less than or equal to the given field width, the input values are unaltered and stored in given variables.

Input : 3  234

Result : 3 is stored in a and 234 is stored in b.


Case 2: When input data length is more than the given field width, the input values are altered and stored in the variables.

Input : 325  2349

Result : 32 is stored in a and 5 is stored in b and the rest of input is ignored.


Format for Integer Output

%wd
Here 'd' is the conversion specification character for integer value and 'w' is an integer number specifying the minimum field width of output data. If the length of the variable is less than the specified field width, the variable is right justified (i.e all the text aligned to the right-hand side) with leading blanks.

Let's take some examples:

printf("a=%2d,b=%3d", a, b);

Case 1: When the length of variable is less than the width specifier.

If a = 3 and b = 16, then

Expected Output:

 a   =   3   ,   b   =   1   6 

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

Case 2: When the length of the variable is equal to the width specified no leading space is added.

If a = 32 and b = 126, then

Expected Output:

 a   =   3   2   ,   b   =   1   2   6 

Case 3: When the length of the variable is more than the width specified then the output is printed correctly despite the length of the variable.

If a = 321 and b = 1263, then

Expected Output:

 a   =   3   2   1   ,   b   =   1   2   6   3 


Format for Floating Point Numeric Input

%wf
Here 'f' is the conversion specification character for floating point value and 'w' is an integer number specifying the maximum field width of the input data (including the digits before and after decimal and the decimal itself).

Let's take some examples:

scanf("%3f%4f", &a, &b);

In this case variable, a has width of 3 and b has a width of 4.

a and b can be entered in the following ways:

Case 1: When input data length is less than or equal to the given width, the input values are unaltered and stored in given variables.

Input : 3  5.62


Result : 3.0 is stored in a and 5.62 is stored in b.


Case 2: When input data length is more than the given width, the input values are altered and stored in the variables.

Input : 3.23  42.465

Result : 3.2 is stored in a and 3.00 is stored in b and the rest of input is ignored.



Format for Floating Point Numeric Output

%w.nf
Here w is the integer number specifying the total width of the input data and n is the number of digits to be printed after decimal point. By default 6 digits are printed after the decimal.

Let's take some examples:

printf("a=%5.1f,b=%5.2f", a, b);

If the total length of the variable is less than the specified width w, then the value is right justified with leading blanks. If the number of digits after decimal is more than n, the digits are rounded off.

Case 1:

If a = 4.1 and b = 3.6, then

Expected Output:

 a  =  4   .   1   ,   b  = 3  .  6  0 

In this case width of the variable, a is 5 and length of output data is 3, that's why two leading spaces are added before 4.1. Similarly, the width of the variable b is 5 and length of output data is 3 , but since the number of digits to be printed after the decimal point is 2 only one leading space is added before 3.6 .

Case 2:

If a = 436.2 and b = 34.62, then

Expected Output:

a =  4   3   6   .   2   ,  b =  3   4   .  6  2 

In this case the length of data is equal to the width specified, that's why the numbers are printed without any leading spaces.

Case 3:

If a = 436.231 and b = 4.875948, then

Expected Output:

 a  = 4  3   6   .   2   ,   b  = 4  .  8  8 

In this case the number of digits after decimal is more than n, the digits are rounded off.


Format for String Input

%ws
Here w specifies the total number of characters that will be stored in the string.

Let's take some examples:

char str[20];
scanf("%7s", str);


If the input is Programming then only first seven characters of this input will be stored in the string, so the characters in the string will be:

'P','r','o','g','r','a','m','\0'

The null character('\0') is automatically stored at the end.


Format for String Output

%w.ns
Here w is the specified field width. Decimal point and n are optional. If present then n specifies that only first n characters of the string will be displayed and (w-n) leading blanks are displayed before string.

Case 1:
printf("%3s", "JustCoding");

Expected Output:

 J   u   s   t   C   o   d   i   n   g 

Here width of the string is less than the length of the input, so the string will be printed with no leading spaces.

Case 2:
printf("%10s", "Just");

Expected Output:

 J   u   s   t 

Here width of the string is 10 and the length of the string is 4, so the string will be printed with 6 leading spaces.

Case 3:
printf("%.4s", "JustCoding");

Expected Output:

  J     u     s     t  

Here width of the input is not specified but .4 indicates that whatever be the length of input string only the first 4 character from the string will be displayed.

Case 4:
printf("%10.3s", "Just");

Expected Output:

 J   u   s 

Here width of the output is 10 but .3 indicates that only 3 characters will be displayed. The length of the string is 4, so only "Jus" will be displayed along with 7 (10-3=7) leading spaces.








No comments:

Post a Comment