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

C প্রোগ্রামিংয়ের Statements সম্পর্কে আলোচনা

Statements

In the C language, a statement is a complete instruction, ending with a semicolon. In many cases, you can turn an expression into a statement by simply adding a semicolon at the end of the expression.

For instance, the following

C প্রোগ্রামিং এর বিভিন্ন ধরনের Expressions এর আলোচনা

Expressions
An expression is a combination of constants, variables, and operators that are used to denote computations.

For instance, the following:

(2 + 3) * 10

Exercises : Answer these Questions

After Reading and Understanding the Chapter 03 : The Essentials of C Programs, please give the answer of following Questions. if you able to answer these question correctly then please proceed to next chapter which is on Data Types and Names in C programming.

C প্রোগ্রামের Constants ও Variables

Constants and Variables

Constant এর value  কখনোই চেঞ্জ হয় না। অন্যদিকে  variable কে ব্যবহার করা হয় ভিন্ন ভিন্ন ভ্যালু কে দেখানোর জন্য ।