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

Using the Precision Specifier
You can put a period (.) and an integer right after the minimum field width specifier. The combination of the period (.) and the integer makes up a precision specifier. The precision specifier is another important specifier you can use to determine the number of decimal places for floating-point numbers, or to specify the maximum field width (or length) for integers or strings. (Strings in C are introduced in Hour 13, "Manipulating Strings.")

 

For instance, with %10.3f, the minimum field width length is specified as 10 characters long, and the number of decimal places is set to 3. (Remember, the default number of decimal places is 6.) For integers, %3.8d indicates that the minimum field width is 3, and the maximum field width is 8.

 

Listing 5.8 gives an example of left- or right-justifying output by using precision specifiers.

 

TYPE
Listing 5.8. Using precision specifiers.

 

1:  /* 05L08.c: Using precision specifiers */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int int_num;
7:     double flt_num;
8:
9:     int_num = 123;
10:    flt_num = 123.456789;
11:    printf("Default integer format:    %d\n", int_num);
12:    printf("With precision specifier:  %2.8d\n", int_num);
13:    printf("Default float format:      %f\n", flt_num);
14:    printf("With precision specifier:  %-10.2f\n", flt_num);
15:    return 0;
16: }

 

 

OUTPUT
After running the executable file 05L08.exe on my machine, I get the following output on the screen:

 

   C:\app> 05L08

    Default integer format:    123

    With precision specifier:  00000123

    Default float format:      123.456789

    With precision specifier:  123.46

    C:\app>

 

ANALYSIS
The program in Listing 5.8 declares one integer variable, int_num, in line 6, and one floating-point number, flt_num, in line 7. Lines 9 and 10 assign 123 and 123.456789 to int_num and flt_num, respectively.

 

In line 11, the default integer format is specified for the integer variable, int_num, while the statement in line 12 specifies the integer format with a precision specifier that indicates that the maximum field width is 8 characters long. Therefore, you see that five zeros are padded prior to the integer 123 in the second line of the output.

 

For the floating-point variable, flt_num, line 13 prints out the floating-point value in the default format, and line 14 reduces the decimal places to two by putting the precision specifier .2 within the format specifier %-10.2f. Note here that the left-justification is also specified by the minus sign (-) in the floating-point format specifier.

 

The floating-point number 123.46 in the fourth line of the output is produced by the statement in line 14 with the precision specifier for two decimal places. Therefore, 123.456789 rounded to two decimal places becomes 123.46.

Related Items

Adding More Expressions into for

Adding More Expressions into for

The C language allows you to put more expressions into the three expression fields in the for statement. Expressions in a single expression field are separated by commas.

The Null Statement

The Null Statement

Looping Under the for Statement

Looping Under the for Statement

The general form of the for statement is

for (expression1; expression2; expression3) {
   statement1;
   statement2;
   .
   .
   .
}

Using Nested Loops

Using Nested Loops

You can put a loop inside another one to make nested loops. The computer will run the inner loop first before it resumes the looping for the outer loop.

Listing 7.7 is an example of how nested loops work.

 

The do-while Loop

The do-while Loop

You may note that in the for and while statements, the expressions are set at the top of the loop. However, in this section, you're going to see another statement used for looping,