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:
Flags | Description |
‘ | (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) |
0 | Add 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 modifier | Description |
hh | Output the corresponding parameters as signed or unsigned char |
h | Output the corresponding parameters as signed or unsigned short |
l | Output the corresponding parameters as signed or unsigned long |
ll | Output the corresponding parameters as signed or unsigned long long |
j | intmax_t or uintmax_t |
z | size_t |
t | ptrdiff_t |
L | long double |
convtype
The optional arguments to lenmodifier are:
Conversion type | Description |
d、i | signed decimal |
o | unsigned octal |
u | unsigned decimal |
x、X | unsigned hexadecimal |
f、F | double precision floating point numbers |
e、E | exponential format double precision floating-point numbers |
g、G | interpreted as f, F, e or E based on the converted values |
a、A | hexadecimal exponential format double precision floating point numbers |
c | character (wide character with length modifier L) |
s | string (wide character with length modifier L) |
p | pointer to void p |
n | 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) |
S | wide 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.