Contents

Formatted Input and Output allows programmers to perform input and output in a particular fashion.

## Formatting integer input

%wd

Here `%d`

is the conversion specification for integer and `w`

denotes the maximum width of the input data. If the length of the input is more than the width then values are not stored correctly.

Let’s take some examples:

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

In this case variable, `a`

has a width of `2`

and `b`

has a width of `3`

.

The values of `a`

and `b`

can be entered in the following ways:

**Case 1:** When the length of the data entered is less than the field width, then the input values are stored correctly in the given variables.

**Input:** 4 34

In this case, `4`

is stored in `a`

and `34`

is stored in `b`

.

**Case 2:** When the length of the data entered is equal to the field width, then the input values are stored correctly in the given variables.

**Input:** 23 456

In this case, `23`

is stored in `a`

and `456`

is stored in `b`

.

**Case 3:** When the length of the data entered is greater than the field width, then input values are not stored correctly in the given variables.

**Input:** 234 99

Since `a`

has a width of 2, only `23`

is stored in `a`

and `4`

is stored in `b`

, while the rest of the input is ignored.

## Formatting integer output

%wd

In this case, the `w`

denotes the minimum width of the data and `d`

is for integers. If the length of the variable is less than the width then the value is printed right-justified with leading spaces. For e.g:

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

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

If `a = 4`

and `b = 23`

, then the output will be:

**Expected Output:**

1 |
a=•4,b=•23 |

In this case, width specified for the first variable is 2 and the length of the output is only 1 digit(since the number is `4`

), as a result, one leading space is added before `4`

. The space character is represented using a `•`

character. Similarly, the width of the second variable is 3 while the length of the output is only 2 digit (since the number is `23`

), so once again a leading space is added before `23`

.

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

1 |
printf("a=%3d,b=%4d", a, b); |

If `a = 456`

and `b = 2234`

, then

**Expected Output:**

1 |
a=456,b=2234 |

**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.

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

If `a = 1221`

and `b = 19234`

, then

**Expected Output:**

1 |
a=1221,b=19234 |

## Formatting Floating Point Input

%wf

Here `w`

is an integer number specifying the maximum width of the input data including digits before and after decimal points and the decimal itself.

**Case 1:** When the length of the input data is less than the given width, then the values are stored correctly in the given variables.

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

**Input:** 4 1.2

In this case the maximum width of the first variable is 3, while the length of the input is 1, similarly, the width of the second variable is 4, while the length of the input is 3. So the values are stored correctly in the variables. i.e `a = 4`

and `b = 1.2`

.

**Case 2:** When the length of input data is equal to the width, then the values are stored correctly in the variables.

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

**Input:** 1.2 33.1

In this case, the width and length of the input are same, so the values are stored correctly in the variables. i.e `a = 1.2`

and `b = 33.1`

.

**Case 3:** When the length of input data is greater than the width specified then the values are not stored correctly in the variables.

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

**Input:** 5.21 983.71

As the width of the first variable is 3 only `5.2`

is stored in the variable `a`

while `1`

is stored in `b`

, and the rest of the input is ignored.

## Formatting Floating Point Output

%w.nf

The `w`

is the minimum width of output data and `n`

is the digits to be printed after the decimal point. Note that width includes digits before and after the decimal point and the decimal itself.

**Case 1:** When the length of the output data is less than width specified, then the numbers are right justified with the leading spaces.

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

where `a = 3.1`

and `b = 2.4`

**Expected Output:**

1 |
a=••3.1, b=•2.40 |

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 `3.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 `2.4`

.

**Case 2:** When the length of data is equal to the width specified, then the numbers are printed without any leading spaces.

1 |
printf("a=%4.2f, b=%4.2f", a, b); |

where `a = 32.1`

and `b = 45.11`

.

**Expected Output:**

1 |
a=32.10, b=45.11 |

**Case 3:** When the length of data is greater than the width specified, then the numbers are printed without any leading spaces.

1 |
printf("a=%5.2f, b=%4.3f", a, b); |

where `a = 34189.313`

and `b = 415.1411`

.

**Expected Output:**

1 |
a=34189.31, b=415.141 |

## Formatting String Input

%ws

Here `w`

specify the length of the input to be stored in the variable.

1 2 |
char str[20]; scanf("%4s", str) |

**Note:** Strings in C is declared as an array of characters, we will learn more about arrays and string in lesson String Basics in C.

If the input is `earning`

then only `earn`

will be stored in the variable `str`

.

## Formatting String Output

%w.ns

The `w`

is the width of the string. The dot (`.`

) character after `w`

and `n`

are optional. If present only `n`

characters will be displayed and (`w-n`

) leading spaces will be added before the string. On the other hand, if only width of the string(i.e `w`

) is specified and the length of the string is less than the width specified then the output will be right-justified with leading spaces. Otherwise, no leading space is added.

**Case 1:**

1 |
printf("%4s", "codeindepth"); |

**Expected Output:**

1 |
codeindepth |

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:**

1 |
printf("%10s", "code"); |

**Expected Output:**

1 |
•••••••code |

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:**

1 |
printf("%10.3s", "code"); |

**Expected Output:**

1 |
•••••••cod |

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 `"cod"`

will be displayed along with 7 (10-3=7) leading spaces.

**Case 4:**

1 |
printf("%.6s", "codeindepth"); |

**Expected Output:**

1 |
codein |

Here width of the input is not specified but `.6`

indicates that whatever be the length of input string only the first 6 character from the string will be displayed.

In the next chapter, we will learn about Operators and Expressions in C.