Printf format specifiers in C

From Notes_Wiki

Home > C programming language > Printf format specifiers in C

The size specification

l d,i,o,u,x,X The letter l with this formats means long or unsigned long.
l n The letter l with the n format means long *.
l c Used with the c (character) format, it means the character string is in wide character format.
l all others No effect, is ignored
ll d,i,o,u,x,X The letters ll mean long long or unsigned long long.
ll n With this format, ll means long long *.
h d,i,o,u,x,X With this formats, h indicates short or unsigned short.
h n Means short *.
hh d,i,o,u,x,X Means char or unsigned char.
hh n Means char * or unsigned char *.
L A, a, E, e, F, f, G, g, Means that the argument is a long double. Notice that the l modifier has no effect with those formats. It is uppercase L.
j d, i, o, u, x, X Means the argument is of type intmax_t, i.e. the biggest integer type that an implementation offers. In the case of lcc-win32 this is long long.
q f,g,e Means the argument is of type qfloat (350 bits precision). This is an extension of lcc-win32.
t d, i, o, u, x, X Means the argument is ptrdiff_t, under lcc-win32 int.
z d, i, o, u, x, X Means the argument is size_t, in lcc-win32 unsigned int.



The conversions

d, i Signed integer conversion is used and the argument is by default of type int. If the h modifier is present, the argument should be a short, if the ll modifier is present, the argument is a long long.
u Unsigned decimal conversion. Argument should be of type unsigned int (default), unsigned short (h modifier) or unsigned long long (ll

modifier).

o Unsigned octal conversion is done. Same argument as the u format.
x, X Unsigned hexadecimal conversion. If x is used, the letters will be in lower case, if X is used, they will be in uppercase. If the # modifier is present, the number will be prefixed with 0x.
c The argument is printed as a character or a wide character if the l modifier is present.
s The argument is printed as a string, and should be a pointer to byte sized characters (default) or wide characters if the l modifier is present. If no precision is given all characters are used until the zero byte is found. Otherwise, the maximum number of characters used is the given precision.
p The argument is a pointer and is printed in pointer format. Under lcc-win32 this is the same as the unsigned format (#u).
n The argument is a pointer to int (default), pointer to short (h modifier) or pointer to long long (ll modifier). Contrary to all other conversions, this conversion writes the number of characters written so far in the address pointed by its argument.
e, E Signed decimal floating point conversion..Argument is of type double (default), or long double (with the L modifier) or qfloat (with the q modifier). The result will be displayed in scientific notation with a floating point part, the letter ‘e’ (for the e format) or the letter E (for the E format), then the exponent. If the precision is zero, no digits appear after the decimal point, and no point is shown. If the # flag is given, the point will be printed.
f, F Signed decimal floating point conversion. Argument is of type double (default), or long double (with the L modifier). If the argument is the special value infinite, inf will be printed. If the argument is the special value NAN the letters nan are written.
g, G This is the same as the above but with a more compact representation. Arguments should be floating point. Trailing zeroes after the decimal point are removed, and if the number is an integer the point disappears. If the # flag is present, this stripping of zeroes is not performed. The scientific notation (as in format e) is used if the exponent falls below -4 or is greater than the precision, that defaults to 6.
% This can be used to insert percent(%) sign in output by using %%



Home > C programming language > Printf format specifiers in C