Formatted Input and Output in C

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 is used to denote 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:

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 length of the data entered is less than the field width, then 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 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

As variable a has 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, output w denote minimum width of the data and d is for integers. If the length of the variable is less than the width then the variable is right justified (i.e all the text aligned to the right-hand side) with leading spaces. For e.g:

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

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

If a = 4 and b = 23, then

Expected Output:

a=•4,b=•23

In this case, width specified for the first variable is 2 and the length of the output is only 1  (i.e 4) digit, as a result, one leading space is added before 4 denoted by 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.

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

If a = 456 and b = 2234, then

Expected Output:

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.

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

If a = 1221 and b = 19234, then

Expected Output:

a=121,b=1234

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.

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 values are stored correctly in the variables.

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 values are not stored correctly in the variables.

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.

So a = 5.2 and b = 1.

Formatting floating point output

%w.nf

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 leading spaces.

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

where a = 3.1  and b = 2.4

Expected Output:

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.

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

where a = 32.1 and b = 45.11.

Expected Output:

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.

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

where a = 34189.313 and b = 415.1411.

Expected Output:

a=34189.31, b=415.141

Formatting string input

%ws

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

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 later chapters.

If the input is earning then only earn will be stored in the variable str.

Formatting string output

%w.ns

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:

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

Expected Output:

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:

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

Expected Output:

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

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

Expected Output:

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

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

Expected Output:

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.