printfcopied from: https://cplusplus.com/reference/cstdio/printf/
syntax: %[flags][width][.precision][length]specifier
| specifier | Output | Example |
|---|---|---|
%d%i |
Signed decimal integer | 392 |
%u |
Unsigned decimal integer | 7235 |
%o |
Unsigned octal | 610 |
%x%X |
Unsigned hexadecimal integer | 7fa |
%f%F |
Decimal floating point | 392.65 |
%e%E |
Scientific notation (mantissa/exponent) | 3.9265e+23.9265E+2 |
%g%G |
Use the shortest representation: %e vs %f |
392.65 |
%a%A |
Hexadecimal floating point | -0xc.90fep-2 |
%c |
Character | a |
%s |
String of characters | sample |
%p |
Pointer address | b8000000 |
%n |
Nothing printed. The corresponding argument must be a pointer to a signed int.The number of characters written so far is stored in the pointed location. |
|
%% |
A % followed by another % character will write a single % to the stream. |
% |
| example code | example output |
|---|---|
printf("Characters: %c %c \n", 'a', 65); |
Characters: a A |
printf("Decimals: %d %ld\n", 1977, 650000L); |
Decimals: 1977 650000 |
printf("Preceding with blanks: %10d \n", 1977); |
Preceding with blanks: 1977 |
printf("Preceding with zeros: %010d \n", 1977); |
Preceding with zeros: 0000001977 |
printf("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100); |
Some different radices: 100 64 144 0x64 0144 |
printf("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416); |
floats: 3.14 +3e+000 3.141600E+000 |
printf("Width trick: %*d \n", 5, 10); |
Width trick: 10 |
printf("%s \n", "A string"); |
A string |
| flags | description |
|---|---|
- |
Left-justify within the given field width; Right justification is the default (see width sub-specifier). |
+ |
Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign. |
| (space) | If no sign is going to be written, a blank space is inserted before the value. |
# |
Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.Used with a, A, e, E, f, F, g, G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.
|
0 |
Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier). |
| width | description |
|---|---|
| (number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger. |
* |
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
| .precision | description |
|---|---|
.number |
For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written.
If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer.
A precision of 0 means that no character is written for the value 0.For a, A, e, E, f, F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).For g and G specifiers: This is the maximum number of significant digits to be printed.For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.If the period is specified without an explicit value for precision, 0 is assumed. |
.* |
The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
strftimecopied from: https://cplusplus.com/reference/ctime/strftime/
| Field type | specifier | Replaced by | Example |
|---|---|---|---|
| year | %C |
Year divided by 100 and truncated to integer (00-99) |
20 |
%g |
Week-based year, last two digits (00-99) |
01 |
|
%G |
Week-based year | 2001 |
|
%y |
Year, last two digits (00-99) |
01 |
|
%Y |
Year | 2001 |
|
| month | %b |
Abbreviated month name | Aug |
%B |
Full month name * | August |
|
%h |
Abbreviated month name (same as %b) |
Aug |
|
%m |
Month as a decimal number (01-12) |
08 |
|
| week | %U |
Week number with the first Sunday as the first day of week one (00-53) |
33 |
%V |
ISO 8601 week number (01-53) |
34 |
|
%W |
Week number with the first Monday as the first day of week one (00-53) |
34 |
|
| day | %d |
Day of the month, zero-padded (01-31) |
23 |
%e |
Day of the month, space-padded ( 1-31) |
23 |
|
%j |
Day of the year (001-366) |
235 |
|
%a |
Abbreviated weekday name | Thu |
|
%A |
Full weekday name | Thursday |
|
%u |
ISO 8601 weekday as number with Monday as 1 (1-7) |
4 |
|
%w |
Weekday as a decimal number with Sunday as 0 (0-6) |
4 |
|
| period | %p |
AM or PM designation | PM |
| hour | %H |
Hour in 24h format (00-23) |
14 |
%I |
Hour in 12h format (01-12) |
02 |
|
%r |
12-hour clock time | 02:55:02 pm |
|
%R |
24-hour HH:MM time, equivalent to %H:%M |
14:55 |
|
| minute | %M |
Minute (00-59) |
55 |
| second | %S |
Second (00-61) |
02 |
| time zone | %z |
ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100) If timezone cannot be determined, no characters |
+100 |
%Z |
Timezone name or abbreviation If timezone cannot be determined, no characters |
CDT |
|
| shortcut | %c |
Date and time representation | Thu Aug 23 14:55:02 2001 |
%D |
Short MM/DD/YY date, equivalent to %m/%d/%y |
08/23/01 |
|
%F |
Short YYYY-MM-DD date, equivalent to %Y-%m-%d |
2001-08-23 |
|
%T |
ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S |
14:55:02 |
|
%x |
Date representation | 08/23/01 |
|
%X |
Time representation | 14:55:02 |
|
| misc | %n |
New-line character ('\n') |
|
%t |
Horizontal-tab character ('\t') |
||
%% |
A % sign |
% |
|
E |
Uses the locale's alternative representation. Applied to %Ec %EC %Ex %EX %Ey %EY |
||
O |
Uses the locale's alternative numeric symbols. Applied to %Od %Oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow %OW %Oy |
copied from: https://unicode-org.github.io/icu/userguide/format_parse/datetime/
| Format Pattern | Result |
|---|---|
"yyyy.MM.dd G 'at' HH:mm:ss zzz" |
"1996.07.10 AD at 15:08:56 PDT" |
"EEE, MMM d, ''yy" |
"Wed, July 10, ‘96" |
"h:mm a" |
"12:08 PM" |
"hh 'o''clock' a, zzzz" |
"12 o’clock PM, Pacific Daylight Time" |
"K:mm a, z" |
"0:00 PM, PST" |
"yyyyy.MMMM.dd GGG hh:mm aaa" |
"01996.July.10 AD 12:08 PM" |
| Field type | Symbol | Meaning | Pattern | Example Output |
|---|---|---|---|---|
| era | G |
era designator | G, GG, or GGGGGGGGGGGG |
AD Anno Domini A |
| year | y |
year | yyy or yyyy |
96 1996 |
Y |
year of “Week of Year” | Y |
1997 | |
u |
extended year | u |
4601 | |
U |
cyclic year name, as in Chinese lunar calendar | U |
甲子 | |
r |
related Gregorian year | r |
1996 | |
| quarter | Q |
quarter | QQQQQQQQQQQQQQQ |
2 02 Q2 2nd quarter 2 |
q |
stand-alone quarter | qqqqqqqqqqqqqqq |
2 02 Q2 2nd quarter 2 |
|
| month | M |
month in year | MMMMMMMMMMMMMMM |
9 09 Sep September S |
L |
stand-alone month in year | LLLLLLLLLLLLLLL |
9 09 Sep September S |
|
| week | w |
week of year | www |
27 27 |
W |
week of month | W |
2 | |
| day | d |
day in month | ddd |
2 02 |
D |
day of year | D |
189 | |
F |
day of week in month | F |
2 (2nd Wed in July) | |
g |
modified julian day | g |
2451334 | |
E |
day of week | E, EE, or EEEEEEEEEEEEEEEEEE |
Tue Tuesday T Tu |
|
e |
local day of week example: if Monday is 1st day, Tuesday is 2nd ) |
e or eeeeeeeeeeeeeeeeeeee |
2 Tue Tuesday T Tu |
|
c |
stand-alone local day of week | c or cccccccccccccccccccc |
2 Tue Tuesday T Tu |
|
| period | a |
am/pm marker | a |
pm |
| hour | h |
hour in am/pm (1~12) | hhh |
7 07 |
H |
hour in day (0~23) | HHH |
0 00 |
|
k |
hour in day (1~24) | kkk |
24 24 |
|
K |
hour in am/pm (0~11) | KKK |
0 00 |
|
| minute | m |
minute in hour | mmm |
4 04 |
| second | s |
second in minute | sss |
5 05 |
S |
fractional second - truncates (like other time fields) to the count of letters when formatting. Appends zeros if more than 3 letters specified. Truncates at three significant digits when parsing. |
SSSSSSSSSS |
2 23 235 2350 |
|
A |
milliseconds in day | A |
61201235 | |
| time zone | z |
Time Zone: specific non-location | z, zz, or zzzzzzz |
PDT Pacific Daylight Time |
Z |
Time Zone: ISO8601 basic hms / RFC 822 Time Zone: long localized GMT (=OOOO) TIme Zone: ISO8601 extended hms (=XXXXX) |
Z, ZZ, or ZZZZZZZZZZZZ |
-0800 GMT-08:00 -08:00, -07:52:58, Z |
|
O |
Time Zone: short localized GMT Time Zone: long localized GMT (=ZZZZ) |
OOOOO |
GMT-8 GMT-08:00 |
|
v |
Time Zone: generic non-location (falls back first to VVVV) |
vvvvv |
PT Pacific Time or Los Angeles Time |
|
V |
Time Zone: short time zone ID Time Zone: long time zone ID Time Zone: time zone exemplar city Time Zone: generic location (falls back to OOOO) |
VVVVVVVVVV |
uslax America/Los_Angeles Los Angeles Los Angeles Time |
|
X |
Time Zone: ISO8601 basic hm, with Z for 0 Time Zone: ISO8601 basic hm, with Z Time Zone: ISO8601 extended hm, with Z Time Zone: ISO8601 basic hms, with Z Time Zone: ISO8601 extended hms, with Z |
XXXXXXXXXXXXXXX |
-08, +0530, Z -0800, Z -08:00, Z -0800, -075258, Z -08:00, -07:52:58, Z |
|
x |
Time Zone: ISO8601 basic hm, without Z for 0 Time Zone: ISO8601 basic hm, without Z Time Zone: ISO8601 extended hm, without Z Time Zone: ISO8601 basic hms, without Z Time Zone: ISO8601 extended hms, without Z |
xxxxxxxxxxxxxxx |
-08, +0530 -0800 -08:00 -0800, -075258 -08:00, -07:52:58 |
|
| misc | ' |
escape for text | ' |
(nothing) |
' ' |
two single quotes produce one | ' ' |
’ |