Arithmetic and Data Types - The effect of precision specification on integer output

Chapter chap4 section 7

The "%d" conversion which has been described earlier as well as including a field-width specification may also include a precision specification similar to that used with the floating point specifications. With a "%d" specification the precision specifies the minimum number of output digits to appear. This may include leading zero padding. A precision specification of zero will result in a totally blank output field when displaying the value 0, this is sometimes useful when printing large tables. The following program illustrates the effect of precision on integer output.

main()
{
        int     x=5,y=0,z=25;
        /* output always has at least 2 non-blank digits */
        printf(" x = %4.2d y = %4.2d z = %4.2d\n",x,y,z);
        /* complete suppression of zero value */
        printf(" x = %4.0d y = %4.0d z = %4.0d\n",x,y,z);
}

producing the output
 x =   05 y =   00 z =   25
 x =    5 y =      z =   25


Unsigned Integer Data Types