"Z Code Writer" is an Online Blog for Programmer & Beginner. Here, You will Find tech, Build your Programming Skills and other Programming Language Like C, C++, HTML, Java Script Java Code, Web Designing and University Of Sialkot (USKT) CGPA / GPA Calculator etc.

Wednesday, 17 July 2019

Field-Width Specifier

Field-Width Specifier

      The number of columns (or spaces) used to display or print a value on the output device (such as display screen) is called field-width. The field-width specifier describes the number of columns that should be used to display/print the value. The place where the value of argument is printed is called its field.
      The general syntax for specifying field-width specifier is as follows:
             %flag  field-width  precision

Where

%                   This sign indicates the beginning of field-width specifier.
field-width     It is used to specify the field width for displaying the output value. Its use is optional.
precision        It is used to specify the value for precision. It is generally used with floating point values. Its use is optional.                                              
      If the output value has less size (or length) than the specifies field width, then blank spaces are places on the left or right. Similarly, if the output value has large length than the specified field width will be extended to fit its value.


Specifying Field-Width for Integers

      The integer format specifier %d or %i is used to display or read integer value. An integer number is written between the symbol % and the format character 'd' or 'i'. This integer number specifies the field-width.
      Suppose value of integer variable 'x' and 'y' are 62 and 716 respectively. To display values of variables 'x' and 'y' with field widths of 5 and 8 respectively, the printf() function is written as;
          printf ( "%5d%8d" ,  x,  y) ;
       The above statement indicates that 5 columns will be used to display the value of variable 'x' on screen. It will take first 5 columns from the current position. Suppose the current position is at column 0. So it will take five columns from 0 to 4. The value is displayed as right justified. The value of variable 'x' is 62, so it will take column 3 and 4 and blank spaces will be inserted in places of columns 0, 1 and 2.
      Similarly, 8 columns will be used to display the value of variable 'y'. It will take 8 columns after first five columns starting from 5 to 12. The output of the above statement is shown in the following figure.


0
1
2
3
4
5
6
7
8
9
10
11
12
                           6       2     ðŸ–µ    🖵    🖵    🖵     ðŸ–µ      7      1       6

      In the above output format, 🖵 indicates a blank space. This character will not be displayed in actual output (instead white space will be displayed or printed).

Specifying Field-Width for Floating-Point Numbers

      In case of floating-point numbers (or real values), the field width as well as the number of decimal places can also be specified. The general syntax for field width of a floating type value is as follows:
               %W . Df

Where

%           This sign indicates the beginning of filed-width specifier.
W           It indicates the total field width including the decimal point and the fractional part. Its use is optional.                 
D            It indicates the number of decimal places (after the decimal point). The value after decimal point is rounded off. By default, 6 digits are displayed to the right of decimal point. Its use is optional.                                       

      The field width should be large enough to display the real value. For example, to display values 2.13, a field width should be at least 4 columns width.
      It should be noted that for real value smaller than zero, the digit '0' is always displayed before the decimal point such as 0.013. Therefore, the total field width should include:


  • one space for digit '0' before decimal point
  • one space for the decimal point
  • one space for the minus sign if the number is negative.
      Suppose a variable 'x' has value 48.557. To display the value of 'x' having filed width of 10 with two decimal places, the statement is written as;
                printf ( "%10 . 2f" , x) ;
      The above statement shows that the total field width is 10 including one place for decimal point and 2 places for digits after decimal point. The number will be displayed right justified in 10 columns. The output of the above statement is as follows:

0
1
2
3
4
5
6
7
8
9
10
11
12
                                                  4        8       .        5        6

No comments:

Post a Comment

Thanks For Visiting Here...!! I will Reply you as soon as possible.