An Article to Introduce Detailed Printf Usage

Background:

One day, I wanted to print an unsigned char itself in the printf function in my code, but I didn’t know what is the format, I was searching the anwser on the internet and got many answers like “%c”, “%u” etc. which are not the correct one.

I will give the correct answer and give a summarization about printf function.

Correct Answer

“%hhu”

#include <stdio.h>
int main()
{
	unsigned char ch = 23;
	printf("ch = %hhu\n", ch);
	return 0;
}

Output:

Interesting, ah?

Summarization

I got the answer from APUE book and now I am going to introduce the printf function in details.

Below is the protoype of printf function.

%[flags][fldwidth][precision][lenmodifier]convtype

flags

The optional arguments to flags are:

FlagsDescription
(apostrophe) a character that groups integers into thousands
Left-align the output within the field
+Always display signs with signed conversions
(Space)If the first character is not a plus or minus sign, precede it with a space
#Specify another conversion form (for example, prefix 0x for hexadecimal format)
0Add a leading 0 instead of a space for padding

fldwidth

FidWidth specifies the minimum field width. If the number of characters in the parameter is less than the width, the extra characters are padded with Spaces. The field width is either a non-negative decimal number or a (*).

precision

precision specifies the minimum field width for integer parameter, the minimum width of a decimal point for floating-point number parameter and the maxnum width for string parameter.

precision is a dot (.) followed by an optional non-negative decimal number or an asterisk (*).

The fldwidth and precision fields can both be *.In this case, the value of width or precision is specified by an integer parameter, which precedes the converted parameter.

I know it is not easy to understand, so I give the below example.

#include <stdio.h>
int main()
{
	double a = 223.42233;
	printf("a = %*.*f\n", 10, 3, a);
	return 0;
}

Output:

lenmodifier

The optional arguments to lenmodifier are:

Length modifierDescription
hhOutput the corresponding parameters as signed or unsigned char
hOutput the corresponding parameters as signed or unsigned short
lOutput the corresponding parameters as signed or unsigned long
llOutput the corresponding parameters as signed or unsigned long long
jintmax_t or uintmax_t
zsize_t
tptrdiff_t
Llong double

convtype

The optional arguments to lenmodifier are:

Conversion typeDescription
d、isigned decimal
ounsigned octal
uunsigned decimal
x、X unsigned hexadecimal
f、Fdouble precision floating point numbers
e、Eexponential format double precision floating-point numbers
g、G interpreted as f, F, e or E based on the converted values
a、Ahexadecimal exponential format double precision floating point numbers
ccharacter (wide character with length modifier L)
sstring (wide character with length modifier L)
ppointer to void p
So far, the number of characters in this printf call will be written to the signed int to which the pointer points
a % character
C wide character (XSI extension, equivalent to lc)
Swide string (XSI extension, equivalent to ls)

I think this article have introduce the printf function clearly and hope it is useful for you.

Have a good day and any feedbacks are welcome.

Share this article to your social media