Modifiers for Format Commands in printf()
The format commands may have modifiers, to suitably modify the basic conversion specifications.
The following are valid modifiers acceptable in the printf()statement. If more than one modifier
is used, then they must be in the same order as given below.
‘-’ Modifier
The data item will be left-justified within its field; the item will be printed beginning from the leftmost
position of its field.
Field Width Modifier
They can be used with type float, doubleor chararray (string). The field width modifier, which
is an integer, defines the minimum field width for the data item. Data items for smaller width will be
output right-justified within the field. Larger data items will be printed by using as many extra positions as required. e.g. %10fis the format command for a type float data item with minimum field
width 10.
Precision Modifier
This modifier can be used with type float, doubleor chararray (string). The modifier is written as .mwhere mis an integer. If used with data type floator double, the digit string indicates
the maximum number of digits to be printed to the right of the decimal. When used with a string, it
indicates the maximum number of characters to be printed.
If the fractional part of a type floator double data item exceeds the precision modifier, then the
number will be rounded. If a string length exceeds the specified length, then the string will be truncated (cut of at the end). Padding with zeroes occurs for numbers when the actual number of digits
in a data item is less than that specified by the modifier. Similarly blanks are padded for strings. e.g.
%10.3f is the format command for a type float data item, with minimum field width 10 and 3 places
after the decimal.
‘0’ Modifier
The default padding in a field is done with spaces. If the user wishes to pad a field with zeroes, this
modifier must be used.
‘1’ Modifier
This modifier can be used to display integers as long int or a double precision argument. The corresponding format code is %ld.
‘h’ Modifier
This modifier is used to display short integers. The corresponding format code is %hd.
‘*’ Modifier
This modifier is used if the user does not want to specify the field width in advance, but wants the
program to specify it. But along with this modifier an argument is required which tells what the field
width should be.
Let us now see how these modifiers work. First we see their effect when used with integer data
items.
Với sự đa dạng và phong phú về mẫu mã và giá cả, choxeonline.vn hứa hẹn sẽ là địa chỉ tin cậy cho những ai đang tìm kiếm chiếc xe ô tô ưng ý. Từ những mẫu xe hạng sang đến những chiếc xe gia đình...
Khám Phá Đa Dạng Sản Phẩm Xe Ô Tô...