February 15, 2018

Format string of printf

Format string of printf

❑ Format string is a string containing one or more format specifiers.

❑ Format specifier has the following syntax.

%[Parameter][Flag][Width][Precision][Length][Type]




❑ Parameter field
CharacterDescription
n$Select a value(direct parameter access).
  * 1$ exists in esp+4 on x86.
  * 2$ → esp+8


❑ Flag field
CharacterDescription
+prepend + or - sign.
-Left-align.
0If "Width" field is used, empty space is filled with zeros.
(Space)Prepend "space character" to the positive number.
Prepend "-" to the negative number.
#Prepend 0 to the octal number.
Prepend 0x to the hexadecimal number.


❑ Width field
CharacterDescription
n or *Specify the minimum number of characters to print.


❑ Precision field
CharacterDescription
.n or .*- Decimal type : Same function as Width field.
- Float type : Specify the number of digits after the decimal point.
- String type : Specify the number of characters.


❑ Length field
CharacterDescription
hData type size down(int → short).
hhData type size down(int → char).
lData type size up(int → long).
llData type size up(int → long long).
LData type size up(double → long double).
zData type change(int → size_t).
jData type change(int → intmax_t).
tData type change(int → ptrdiff_t).


❑ Type field
CharacterDescription
d, iInt type value.
uUnsigned int type value.
f, FDouble type value with decimal point.
e, EDouble type value with exponent.
g, GEfficient double type value.
c, CChar type value.
a ,ADouble type value with hexadecimal.
pUnsigned int type address with hexadecimal.
x, XUnsigned int type value with hexadecimal.
oUnsigned int type value with octal.
s- String type value.
- Read string type value from char *.
nWrite the printed size so far into the following 4 Bytes address.
  * Examples of use(Link)
hnWrite the printed size so far into the following 2 Bytes address.
hhnWrite the printed size so far into the following 1 Bytes address.
  * If the size output so far causes an overflow in the memory space, the counted value from the beginning is input.
  * e.g. If 0x101 is input to the space of 0xFF size, 0x2 is stored.
  * 지금까지 출력된 사이즈가 메모리 공간에서 오버플로우를 발생시키면, 처음부터 다시 카운팅 된 값이 입력된다.
  * e.g. 0xFF 크기의 공간에 0x101이 입력되면 0x2가 저장된다.