Pages

Saturday, July 30, 2016

Precision specifier

The printf precision specifiers set the maximum number of characters (or minimum number of integer digits) to print. A printf precision specification always begins with a period (.) to separate it from any preceding width specifier. Like [width], precision is specified in one of two ways:
  • directly, through a decimal digit string
  • indirectly, through an asterisk (*)

If you use an "*" for the precision specifier, the next argument in the call (treated as an int) specifies the precision. If you use "*" for the width or the precision, or for both, the width argument must immediately follow the specifiers, followed by the precision argument, then the argument for the data to be converted.

.precDescription
(none) Precision set to default:
  • 1 for d, i, o, u, x, X types
  • 6 for e, E, f types
  • All significant digits for g, G types
  • Print to first null character for s types
  • No effect on c types
.0 For d, i, o, u, x types, precision set to default;
for e, E, f types, no decimal point is printed.
.n n characters or n decimal places are printed. If the output value has more than n characters, the output might be truncated or rounded. (Whether this happens depends on the type character.)
* The argument list supplies the precision specifier, which must precede the actual argument being formatted.

No numeric characters will be output for a field (i.e., the field will be blank) if the following conditions are all met:
  • you specify an explicit precision of 0
  • the format specifier for the field is one of the integer formats (d, i, o, u, or x)
  • the value to be printed is 0

How [.prec] affects conversion

Type CharEffect of [.prec] (.n) on Conversion
d i o u x X Specifies that at least n digits are printed.
  • If input argument has less than n digits, output value is left-padded with zeros.
  • If input argument has more than n digits, the output value is not truncated.
e E fSpecifies that n characters are printed after the decimal point, and the last digit printed is rounded.
g GSpecifies that at most n significant digits are printed.
cHas no effect on the output.
sSpecifies that no more than n characters are printed.

Examples
CodeOutput
printf("%.0d",0);
printf("%.0d",1);1
printf("%.0d",123);123
printf("%.0f",12.3456);12
printf("%.5d",12);00012
printf("%.1f",12.3456);12.3
printf("%.3f",12.3456);12.346
printf("%.3s","Education");Edu

Width Specifier

The printf width specifier sets the minimum width for an output value. Width is specified in one of two ways:
  • directly, through a decimal digit string
  • indirectly, through an asterisk (*)
If you use an "*" for the width specifier, the next argument in the call (which must be an int) specifies the minimum output field width.

Nonexistent or small field widths do NOT cause truncation of a field. If the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.

WidthDescription
n At least n characters are printed. If the output less than n characters, the output is padded with blanks (right-padded if "-" flag is given, left-padded otherwise).
0n At least n characters are printed. If the output value has less than n characters, it is filled on the left with zeros.
*The argument list supplies the width specifier, which must precede the actual argument being formatted.

Examples
CodeOutput
printf("%5d",10);   10
printf("%05",10);00010
printf("%8s","Hello");   Hello
printf("%08s","Hello");   Hello (doesn't pad 0 for string)
printf("%*s",0,"Hello");Hello
printf("%*s",8,"Hello");   Hello (width given by argument)

Size Modifier

These modifiers determine how printf functions interpret the next input argument, arg[f].

ModifierType of argarg is interpreted as ...
F Pointer (p, s and n) A far pointer
N A near pointer (NOTE: N can't be used with any conversion in huge model.)
h d i o u x X A short int
l d i o u x X A long int
e E f g G A double
L e E f g G A long double

These modifiers affect how all the printf functions interpret the data type of the corresponding input argument arg.

Both F and N reinterpret the input variable arg. Normally, the arg for a %p, %s, or %n conversion is a pointer of the default size for the memory model.

h, l, and L override the default size of the numeric data input arguments. Neither h nor l affects character (c, s) or pointer (p, n) types.

Flag Characters

The printf flag characters specify output justification, numeric signs, decimal points, trailing zeros, and octal and hex prefixes. The flag characters can appear in any order and combination.

FlagDescription
- Left-justifies the result, pads on the right with blanks. If not given, right-justifies result, pads on left with zeros or blanks.
+ Signed conversion results always begin with a plus (+) or minus (-) sign.
blank If value is non-negative, the output begins with a blank instead of a plus; negative values still begin with a minus.
# Specifies octal and hexadecimal format.
Plus (+) takes precedence over blank ( ) if both are given.

Examples
CodeOutput
printf("%5d",10);   10
printf("%-5d test",10);10    test
printf("%10s world","Hello");     Hello world
printf("%-10s world","Hello");Hello      world
printf("%+d",10);+10
printf("%+d",-10);-10
printf("% d",10); 10
printf("% d",10);-10
printf("%o",255);377
printf("%#o",255);0377
printf("%x",255);ff
printf("%#x",255);0xff

Type Characters

The information in this table is based on the assumption that no flag characters, width specifiers, precision specifiers, or input-size modifiers were included in the format specifier. NOTE: Certain conventions accompany some of these format specifiers.

Type Char Expected Input Format of output
Numerics
d i Integer Signed decimal integer
o Integer Unsigned octal integer
u Integer Unsigned decimal integer
x Integer Unsigned hexadecimal int (with a, b, c, d, e, f)
X Integer Unsigned hexadecimal int (with A, B, C, D, E, F)
f Floating point Signed value of the form [-]dddd.dddd
e Floating point Signed value of the form [-]d.dddd or e[+/-]ddd
g Floating point Signed value in either e or f form, based on given value and precision. Trailing zeros and the decimal point are printed if necessary
E Floating point Same as e; with E for exponent
G Floating point Same as g; with E for exponent if e format used
Characters
c Character Single character
s String pointer Prints characters until a null-terminator is pressed or precision is reached
% None Prints the % character
Pointers
n Pointer to int Stores (in the location pointed to by the input argument) a count of the chars written so far
p Pointer Prints the input argument as a pointer; format depends on which memory model was used. It will be either XXXX:YYYY or YYYY (offset only)

Infinite floating-point numbers are printed as +INF and -INF. An IEEE Not-A-Number is printed as +NAN or -NAN.

Examples
CodeOutput
printf("%d",123);123
printf("%i",123);123
printf("%o",123);173
printf("%u",54321);54321
printf("%x",123);7b
printf("%X",123);7B
printf("%f",12345.678);12345.678000
printf("%e",12345.678);1.234568e+04
printf("%e",0.0001234567);1.234567e-04
printf("%E",12345.678);1.234568E+04
printf("%E",0.0001234567);1.234567E-04
printf("%g",12345.678);12345.7
printf("%g",1234567.8);1.23457e+06
printf("%G",1234567.8);1.23457E+06
printf("%g",0.001234567);0.00123457
printf("%g",0.00001234567);1.23457e-05
printf("%G",0.00001234567);1.23457E-05
printf("%c",'A');A
printf("%s","test");test
printf("%%");%
int a; printf("%p",&a);FFF8 (address may vary)