Submitted by tushar pramanick on Tue, 03/05/2013 - 15:28

Adding the Minimum Field Width
The C language allows you to add an integer between the percent sign (%) and the letter in a format specifier. The integer is called the minimum field width specifier because it specifies the minimum field width and ensures that the output reaches the minimum width. For example, in %10f, 10 is a minimum field width specifier that ensures that the output is at least 10 character spaces wide.

 

The example in Listing 5.6 shows how to use the minimum field width specifier.

 

TYPE
Listing 5.6. Specifying the minimum field width.

 

1:  /* 05L06.c: Specifying minimum field width */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int num1, num2;
7:
8:     num1 = 12;
9:     num2 = 12345;
10:    printf("%d\n", num1);
11:    printf("%d\n", num2);
12:    printf("%5d\n", num1);
13:    printf("%05d\n", num1);
14:    printf("%2d\n", num2);
15:    return 0;
16: }



OUTPUT
The following is the result I obtain by running the executable file 05L06.exe:

 

C:\app> 05L06

    12

    12345

       12

    00012

    12345

    C:\app>

 

ANALYSIS
In Listing 5.6, two integer variables, num1 and num2, are declared in line 6, and assigned 12 and 12345, respectively, in lines 8 and 9.

 

Without using any minimum field width specifiers, lines 10 and 11 print out the two integers by calling the printf() function. You can see in the output section that the output made by the statements in line 10 is 12, which takes two character spaces, while the output, 12345, from line 11 takes five character spaces.

 

In line 12, a minimum field width, 5, is specified by %5d. The output from line 12 therefore takes five character spaces, with three blank spaces plus two character spaces of 12. (See the third output line in the output section.)

 

The %05d in printf(), shown in line 13, indicates that the minimum field width is 5, and zeros are used to pad the spaces. Therefore, you see the output made by the execution of the statement in line 13 is

 

00012

 

The %2d in line 14 sets the minimum field width to 2, but you still see the full-size output of 12345 from line 14. This means that when the minimum field width is shorter than the width of the output, the latter is taken, and the output is still printed in full.

 

Related Items

Using the Precision Specifier

Using the Precision Specifier

Aligning Output

Aligning Output
As you might have noticed in the previous section, all output is right-justified. In other words, by default, all output is placed on the right edge of the field, as long as the field width is longer than the width of the output.

 

Converting to Hex Numbers

Converting to Hex Numbers

Revisiting the printf() Function

Revisiting the printf() Function

The printf() function is the first C library function you used in this book to print out messages on the screen. printf() is a very important function in C, so it's worth it to spend more time on it.

 

Another Function for Writing: putchar()

Another Function for Writing: putchar()