1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
|
'\" et
.TH FPRINTF "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.SH NAME
dprintf,
fprintf,
printf,
snprintf,
sprintf
\(em print formatted output
.SH SYNOPSIS
.LP
.nf
#include <stdio.h>
.P
int dprintf(int \fIfildes\fP, const char *restrict \fIformat\fP, ...);
int fprintf(FILE *restrict \fIstream\fP, const char *restrict \fIformat\fP, ...);
int printf(const char *restrict \fIformat\fP, ...);
int snprintf(char *restrict \fIs\fP, size_t \fIn\fP,
const char *restrict \fIformat\fP, ...);
int sprintf(char *restrict \fIs\fP, const char *restrict \fIformat\fP, ...);
.fi
.SH DESCRIPTION
Excluding
\fIdprintf\fR():
The functionality described on this reference page is aligned with the
ISO\ C standard. Any conflict between the requirements described here and the
ISO\ C standard is unintentional. This volume of POSIX.1\(hy2008 defers to the ISO\ C standard.
.P
The
\fIfprintf\fR()
function shall place output on the named output
.IR stream .
The
\fIprintf\fR()
function shall place output on the standard output stream
.IR stdout .
The
\fIsprintf\fR()
function shall place output followed by the null byte,
.BR '\e0' ,
in consecutive bytes starting at *\fIs\fP; it is the user's
responsibility to ensure that enough space is available.
.P
The
\fIdprintf\fR()
function shall be equivalent to the
\fIfprintf\fR()
function, except that
\fIdprintf\fR()
shall write output to the file associated with the file descriptor
specified by the
.IR fildes
argument rather than place output on a stream.
.P
The
\fIsnprintf\fR()
function shall be equivalent to
\fIsprintf\fR(),
with the addition of the
.IR n
argument which states the size of the buffer referred to by
.IR s .
If
.IR n
is zero, nothing shall be written and
.IR s
may be a null pointer. Otherwise, output bytes beyond the
.IR n \(hy1st
shall be discarded instead of being written to the array, and a null
byte is written at the end of the bytes actually written into the
array.
.P
If copying takes place between objects that overlap as a result of a
call to
\fIsprintf\fR()
or
\fIsnprintf\fR(),
the results are undefined.
.P
Each of these functions converts, formats, and prints its arguments
under control of the
.IR format .
The
.IR format
is a character string, beginning and ending in its initial shift state,
if any. The
.IR format
is composed of zero or more directives:
.IR "ordinary characters" ,
which are simply copied to the output stream, and
.IR "conversion specifications" ,
each of which shall result in the fetching of zero or more arguments.
The results are undefined if there are insufficient arguments for the
.IR format .
If the
.IR format
is exhausted while arguments remain, the excess arguments shall be
evaluated but are otherwise ignored.
.P
Conversions can be applied to the
.IR n th
argument after the
.IR format
in the argument list, rather than to the next unused argument. In this
case, the conversion specifier character
.BR %
(see below) is replaced by the sequence \fR"%\fIn\fR$"\fR, where
.IR n
is a decimal integer in the range [1,{NL_ARGMAX}],
giving the position of the argument in the argument list. This feature
provides for the definition of format strings that select arguments in
an order appropriate to specific languages (see the EXAMPLES section).
.P
The
.IR format
can contain either numbered argument conversion specifications (that
is, \fR"%\fIn\fR$"\fR and \fR"*\fIm\fR$"\fR), or unnumbered argument
conversion specifications (that is,
.BR %
and
.BR * ),
but not both. The only exception to this is that
.BR %%
can be mixed with the \fR"%\fIn\fR$"\fR form. The results of mixing
numbered and unnumbered argument specifications in a
.IR format
string are undefined. When numbered argument specifications are used,
specifying the
.IR N th
argument requires that all the leading arguments, from the first to the
(\fIN\(mi1\fP)th, are specified in the format string.
.P
In format strings containing the \fR"%\fIn\fR$"\fR form of conversion
specification, numbered arguments in the argument list can be
referenced from the format string as many times as required.
.P
In format strings containing the
.BR %
form of conversion specification, each conversion specification uses
the first unused argument in the argument list.
.P
All forms of the
\fIfprintf\fR()
functions allow for the insertion of a language-dependent radix
character in the output string. The radix character is defined in the
current locale (category
.IR LC_NUMERIC ).
In the POSIX locale, or in a locale where the radix character is not
defined, the radix character shall default to a
<period>
(\c
.BR '.' ).
.P
Each conversion specification is introduced by the
.BR '%'
character
or by the character sequence \fR"%\fIn\fR$"\fR,
after which the following appear in sequence:
.IP " *" 4
Zero or more
.IR flags
(in any order), which modify the meaning of the conversion
specification.
.IP " *" 4
An optional minimum
.IR "field width" .
If the converted value has fewer bytes than the field width, it shall
be padded with
<space>
characters by default on the left; it shall be padded on the right if
the left-adjustment flag (\c
.BR '\(mi' ),
described below, is given to the field width. The field width takes the
form of an
<asterisk>
(\c
.BR '*' ),
described below, or a decimal integer.
.IP " *" 4
An optional
.IR precision
that gives the minimum number of digits to appear for the
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
and
.BR X
conversion specifiers; the number of digits to appear after the radix
character for the
.BR a ,
.BR A ,
.BR e ,
.BR E ,
.BR f ,
and
.BR F
conversion specifiers; the maximum number of significant digits for the
.BR g
and
.BR G
conversion specifiers; or the maximum number of bytes to be printed
from a string in the
.BR s
and
.BR S
conversion specifiers. The precision takes the form of a
<period>
(\c
.BR '.' )
followed either by an
<asterisk>
(\c
.BR '*' ),
described below, or an optional decimal digit string, where a null
digit string is treated as zero. If a precision appears with any other
conversion specifier, the behavior is undefined.
.IP " *" 4
An optional length modifier that specifies the size of the argument.
.IP " *" 4
A
.IR "conversion specifier"
character that indicates the type of conversion to be applied.
.P
A field width, or precision, or both, may be indicated by an
<asterisk>
(\c
.BR '*' ).
In this case an argument of type
.BR int
supplies the field width or precision. Applications shall ensure that
arguments specifying field width, or precision, or both appear in that
order before the argument, if any, to be converted. A negative field
width is taken as a
.BR '\(mi'
flag followed by a positive field width. A negative precision is taken
as if the precision were omitted.
In
.IR format
strings containing the \fR"%\fIn\fR$"\fR form of a
conversion specification, a field width or precision may be indicated
by the sequence \fR"*\fIm\fR$"\fR, where
.IR m
is a decimal integer in the range [1,{NL_ARGMAX}] giving the position
in the argument list (after the
.IR format
argument) of an integer argument containing the field width or
precision, for example:
.sp
.RS 4
.nf
\fB
printf("%1$d:%2$.*3$d:%4$.*3$d\en", hour, min, precision, sec);
.fi \fR
.P
.RE
.P
The flag characters and their meanings are:
.IP "\fR'\fR" 8
(The
<apostrophe>.)
The integer portion of the result of a decimal conversion (\c
.BR %i ,
.BR %d ,
.BR %u ,
.BR %f ,
.BR %F ,
.BR %g ,
or
.BR %G )
shall be formatted with thousands' grouping characters. For other
conversions the behavior is undefined. The non-monetary grouping
character is used.
.IP "\fR\(mi\fR" 8
The result of the conversion shall be left-justified within the field.
The conversion is right-justified if this flag is not specified.
.IP "\fR+\fR" 8
The result of a signed conversion shall always begin with a sign (\c
.BR '+'
or
.BR '\(mi' ).
The conversion shall begin with a sign only when a negative value is
converted if this flag is not specified.
.IP <space> 8
If the first character of a signed conversion is not a sign or if a
signed conversion results in no characters, a
<space>
shall be prefixed to the result. This means that if the
<space>
and
.BR '+'
flags both appear, the
<space>
flag shall be ignored.
.IP "\fR#\fR" 8
Specifies that the value is to be converted to an alternative
form. For
.BR o
conversion, it increases the precision (if necessary) to force the
first digit of the result to be zero. For
.BR x
or
.BR X
conversion specifiers, a non-zero result shall have 0x (or 0X) prefixed
to it. For
.BR a ,
.BR A ,
.BR e ,
.BR E ,
.BR f ,
.BR F ,
.BR g ,
and
.BR G
conversion specifiers, the result shall always contain a radix
character, even if no digits follow the radix character. Without this
flag, a radix character appears in the result of these conversions only
if a digit follows it. For
.BR g
and
.BR G
conversion specifiers, trailing zeros shall
.IR not
be removed from the result as they normally are. For other conversion
specifiers, the behavior is undefined.
.IP "\fR0\fR" 8
For
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
.BR X ,
.BR a ,
.BR A ,
.BR e ,
.BR E ,
.BR f ,
.BR F ,
.BR g ,
and
.BR G
conversion specifiers, leading zeros (following any indication of sign
or base) are used to pad to the field width rather than performing
space padding, except when converting an infinity or NaN. If the
.BR '0'
and
.BR '\(mi'
flags both appear, the
.BR '0'
flag is ignored. For
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
and
.BR X
conversion specifiers, if a precision is specified, the
.BR '0'
flag shall be ignored.
If the
.BR '0'
and
<apostrophe>
flags both appear, the grouping characters are inserted before zero
padding. For other conversions, the behavior is undefined.
.P
The length modifiers and their meanings are:
.IP "\fRhh\fR" 8
Specifies that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.BR X
conversion specifier applies to a
.BR "signed char"
or
.BR "unsigned char"
argument (the argument will have been promoted according to the integer
promotions, but its value shall be converted to
.BR "signed char"
or
.BR "unsigned char"
before printing); or that a following
.BR n
conversion specifier applies to a pointer to a
.BR "signed char"
argument.
.IP "\fRh\fR" 8
Specifies that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.BR X
conversion specifier applies to a
.BR "short"
or
.BR "unsigned short"
argument (the argument will have been promoted according to the integer
promotions, but its value shall be converted to
.BR "short"
or
.BR "unsigned short"
before printing); or that a following
.BR n
conversion specifier applies to a pointer to a
.BR "short"
argument.
.IP "\fRl\fR\ (ell)" 8
Specifies that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.BR X
conversion specifier applies to a
.BR "long"
or
.BR "unsigned long"
argument; that a following
.BR n
conversion specifier applies to a pointer to a
.BR "long"
argument; that a following
.BR c
conversion specifier applies to a
.BR wint_t
argument; that a following
.BR s
conversion specifier applies to a pointer to a
.BR wchar_t
argument; or has no effect on a following
.BR a ,
.BR A ,
.BR e ,
.BR E ,
.BR f ,
.BR F ,
.BR g ,
or
.BR G
conversion specifier.
.IP "\fRll\fR\ (ell-ell)" 8
.br
Specifies that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.BR X
conversion specifier applies to a
.BR "long long"
or
.BR "unsigned long long"
argument; or that a following
.BR n
conversion specifier applies to a pointer to a
.BR "long long"
argument.
.IP "\fRj\fR" 8
Specifies that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.BR X
conversion specifier applies to an
.BR intmax_t
or
.BR uintmax_t
argument; or that a following
.BR n
conversion specifier applies to a pointer to an
.BR intmax_t
argument.
.IP "\fRz\fR" 8
Specifies that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.BR X
conversion specifier applies to a
.BR size_t
or the corresponding signed integer type argument; or that a following
.BR n
conversion specifier applies to a pointer to a signed integer type
corresponding to a
.BR size_t
argument.
.IP "\fRt\fR" 8
Specifies that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.BR X
conversion specifier applies to a
.BR ptrdiff_t
or the corresponding
.BR unsigned
type argument; or that a following
.BR n
conversion specifier applies to a pointer to a
.BR ptrdiff_t
argument.
.IP "\fRL\fR" 8
Specifies that a following
.BR a ,
.BR A ,
.BR e ,
.BR E ,
.BR f ,
.BR F ,
.BR g ,
or
.BR G
conversion specifier applies to a
.BR "long double"
argument.
.P
If a length modifier appears with any conversion specifier other than
as specified above, the behavior is undefined.
.P
The conversion specifiers and their meanings are:
.IP "\fRd\fR,\ \fRi\fR" 8
The
.BR int
argument shall be converted to a signed decimal in the style
\fR"[\(mi]\fIdddd\fR"\fR. The precision specifies the minimum number of
digits to appear; if the value being converted can be represented in
fewer digits, it shall be expanded with leading zeros. The default
precision is 1. The result of converting zero with an explicit
precision of zero shall be no characters.
.IP "\fRo\fP" 8
The
.BR unsigned
argument shall be converted to unsigned octal format in the style
\fR"\fIdddd\fR"\fR. The precision specifies the minimum number of
digits to appear; if the value being converted can be represented in
fewer digits, it shall be expanded with leading zeros. The default
precision is 1. The result of converting zero with an explicit
precision of zero shall be no characters.
.IP "\fRu\fP" 8
The
.BR unsigned
argument shall be converted to unsigned decimal format in the style
\fR"\fIdddd\fR"\fR. The precision specifies the minimum number of
digits to appear; if the value being converted can be represented in
fewer digits, it shall be expanded with leading zeros. The default
precision is 1. The result of converting zero with an explicit
precision of zero shall be no characters.
.IP "\fRx\fP" 8
The
.BR unsigned
argument shall be converted to unsigned hexadecimal format in the
style \fR"\fIdddd\fR"\fR; the letters
.BR \(dqabcdef\(dq
are used. The precision specifies the minimum number of digits to
appear; if the value being converted can be represented in fewer
digits, it shall be expanded with leading zeros. The default precision
is 1. The result of converting zero with an explicit precision of zero
shall be no characters.
.IP "\fRX\fP" 8
Equivalent to the
.BR x
conversion specifier, except that letters
.BR \(dqABCDEF\(dq
are used instead of
.BR \(dqabcdef\(dq .
.IP "\fRf\fR,\ \fRF\fR" 8
The
.BR double
argument shall be converted to decimal notation in the style
\fR"[\(mi]\fIddd\fR.\fIddd\fR"\fR, where the number of digits after the
radix character is equal to the precision specification. If the
precision is missing, it shall be taken as 6; if the precision is
explicitly zero and no
.BR '#'
flag is present, no radix character shall appear. If a radix character
appears, at least one digit appears before it. The low-order digit
shall be rounded in an implementation-defined manner.
.RS 8
.P
A
.BR double
argument representing an infinity shall be converted in one of the
styles
.BR \(dq[\(mi]inf\(dq
or
.BR \(dq[\(mi]infinity\(dq ;
which style is implementation-defined. A
.BR double
argument representing a NaN shall be converted in one of the styles
\fR"[\(mi]nan(\fIn-char-sequence\fR)"\fR or
.BR \(dq[\(mi]nan\(dq ;
which style, and the meaning of any
.IR n-char-sequence ,
is implementation-defined. The
.BR F
conversion specifier produces
.BR \(dqINF\(dq ,
.BR \(dqINFINITY\(dq ,
or
.BR \(dqNAN\(dq
instead of
.BR \(dqinf\(dq ,
.BR \(dqinfinity\(dq ,
or
.BR \(dqnan\(dq ,
respectively.
.RE
.IP "\fRe\fR,\ \fRE\fR" 8
The
.BR double
argument shall be converted in the style
\fR"[\(mi]\fId\fR.\fIddd\fRe\(+-\fIdd\fR"\fR, where there is one digit
before the radix character (which is non-zero if the argument is
non-zero) and the number of digits after it is equal to the precision;
if the precision is missing, it shall be taken as 6; if the precision
is zero and no
.BR '#'
flag is present, no radix character shall appear. The low-order digit
shall be rounded in an implementation-defined manner. The
.BR E
conversion specifier shall produce a number with
.BR 'E'
instead of
.BR 'e'
introducing the exponent. The exponent shall always contain at least
two digits. If the value is zero, the exponent shall be zero.
.RS 8
.P
A
.BR double
argument representing an infinity or NaN shall be converted in
the style of an
.BR f
or
.BR F
conversion specifier.
.RE
.IP "\fRg\fR,\ \fRG\fR" 8
The
.BR double
argument representing a floating-point number shall be converted in the
style
.BR f
or
.BR e
(or in the style
.BR F
or
.BR E
in the case of a
.BR G
conversion specifier), depending on the value converted and the precision.
Let
.BR P
equal the precision if non-zero, 6 if the precision is omitted, or 1 if the
precision is zero. Then, if a conversion with style
.BR E
would have an exponent of
.IR X :
.RS 8
.IP -- 4
If
.BR P >\c
.IR X \(>=\(mi4,
the conversion shall be with style
.BR f
(or
.BR F )
and precision
.BR P \(mi(\c
.IR X +1).
.IP -- 4
Otherwise, the conversion shall be with style
.BR e
(or
.BR E )
and precision
.BR P \(mi1.
.P
Finally, unless the
.BR '#'
flag is used, any trailing zeros shall be removed from the fractional
portion of the result and the decimal-point character shall be removed
if there is no fractional portion remaining.
.P
A
.BR double
argument representing an infinity or NaN shall be converted in the
style of an
.BR f
or
.BR F
conversion specifier.
.RE
.IP "\fRa\fR,\ \fRA\fR" 8
A
.BR double
argument representing a floating-point number shall be converted in the
style \fR"[\(mi]0x\fIh\fR.\fIhhhh\fRp\(+-\fId\fR"\fR, where there is
one hexadecimal digit (which shall be non-zero if the argument is a
normalized floating-point number and is otherwise unspecified) before
the decimal-point character and the number of hexadecimal digits after
it is equal to the precision; if the precision is missing and FLT_RADIX
is a power of 2, then the precision shall be sufficient for an exact
representation of the value; if the precision is missing and FLT_RADIX
is not a power of 2, then the precision shall be sufficient to
distinguish values of type
.BR double ,
except that trailing zeros may be omitted; if the precision is zero and
the
.BR '#'
flag is not specified, no decimal-point character shall appear. The
letters
.BR \(dqabcdef\(dq
shall be used for
.BR a
conversion and the letters
.BR \(dqABCDEF\(dq
for
.BR A
conversion. The
.BR A
conversion specifier produces a number with
.BR 'X'
and
.BR 'P'
instead of
.BR 'x'
and
.BR 'p' .
The exponent shall always contain at least one digit, and only as many
more digits as necessary to represent the decimal exponent of 2. If the
value is zero, the exponent shall be zero.
.RS 8
.P
A
.BR double
argument representing an infinity or NaN shall be converted in the
style of an
.BR f
or
.BR F
conversion specifier.
.RE
.IP "\fRc\fP" 8
The
.BR int
argument shall be converted to an
.BR "unsigned char" ,
and the resulting byte shall be written.
.RS 8
.P
If an
.BR l
(ell) qualifier is present, the
.BR wint_t
argument shall be converted as if by an
.BR ls
conversion specification with no precision and an argument that points
to a two-element array of type
.BR wchar_t ,
the first element of which contains the
.BR wint_t
argument to the
.BR ls
conversion specification and the second element contains a null wide
character.
.RE
.IP "\fRs\fP" 8
The argument shall be a pointer to an array of
.BR char .
Bytes from the array shall be written up to (but not including) any
terminating null byte. If the precision is specified, no more than that
many bytes shall be written. If the precision is not specified or is
greater than the size of the array, the application shall ensure that
the array contains a null byte.
.RS 8
.P
If an
.BR l
(ell) qualifier is present, the argument shall be a pointer to an array
of type
.BR wchar_t .
Wide characters from the array shall be converted to characters (each
as if by a call to the
\fIwcrtomb\fR()
function, with the conversion state described by an
.BR mbstate_t
object initialized to zero before the first wide character is
converted) up to and including a terminating null wide character. The
resulting characters shall be written up to (but not including) the
terminating null character (byte). If no precision is specified, the
application shall ensure that the array contains a null wide character.
If a precision is specified, no more than that many characters (bytes)
shall be written (including shift sequences, if any), and the array
shall contain a null wide character if, to equal the character sequence
length given by the precision, the function would need to access a wide
character one past the end of the array. In no case shall a partial
character be written.
.RE
.IP "\fRp\fP" 8
The argument shall be a pointer to
.BR void .
The value of the pointer is converted to a sequence of printable
characters, in an implementation-defined manner.
.IP "\fRn\fP" 8
The argument shall be a pointer to an integer into which is written the
number of bytes written to the output so far by this call to one of the
\fIfprintf\fR()
functions. No argument is converted.
.IP "\fRC\fP" 8
Equivalent to
.BR lc .
.IP "\fRS\fP" 8
Equivalent to
.BR ls .
.IP "\fR%\fR" 8
Print a
.BR '%'
character; no argument is converted. The complete conversion
specification shall be
.BR %% .
.P
If a conversion specification does not match one of the above forms,
the behavior is undefined. If any argument is not the correct type for
the corresponding conversion specification, the behavior is undefined.
.P
In no case shall a nonexistent or small field width cause truncation of
a field; if the result of a conversion is wider than the field width,
the field shall be expanded to contain the conversion result.
Characters generated by
\fIfprintf\fR()
and
\fIprintf\fR()
are printed as if
\fIfputc\fR()
had been called.
.P
For the
.BR a
and
.BR A
conversion specifiers, if FLT_RADIX is a power of 2, the value shall be
correctly rounded to a hexadecimal floating number with the given
precision.
.P
For
.BR a
and
.BR A
conversions, if FLT_RADIX is not a power of 2 and the result is not
exactly representable in the given precision, the result should be one
of the two adjacent numbers in hexadecimal floating style with the
given precision, with the extra stipulation that the error should have
a correct sign for the current rounding direction.
.P
For the
.BR e ,
.BR E ,
.BR f ,
.BR F ,
.BR g ,
and
.BR G
conversion specifiers, if the number of significant decimal digits is
at most DECIMAL_DIG, then the result should be correctly rounded. If
the number of significant decimal digits is more than DECIMAL_DIG but
the source value is exactly representable with DECIMAL_DIG digits, then
the result should be an exact representation with trailing zeros.
Otherwise, the source value is bounded by two adjacent decimal strings
.IR L
<
.IR U ,
both having DECIMAL_DIG significant digits; the value of the resultant
decimal string
.IR D
should satisfy
.IR L
<=
.IR D
<=
.IR U ,
with the extra stipulation that the error should have a correct sign
for the current rounding direction.
.P
The last data modification and last file status change timestamps
of the file shall be marked for update:
.IP " 1." 4
Between the call to a successful execution of
\fIfprintf\fR()
or
\fIprintf\fR()
and the next successful completion of a call to
\fIfflush\fR()
or
\fIfclose\fR()
on the same stream or a call to
\fIexit\fR()
or
\fIabort\fR()
.IP " 2." 4
Upon successful completion of a call to
\fIdprintf\fR()
.SH "RETURN VALUE"
Upon successful completion, the
\fIdprintf\fR(),
\fIfprintf\fR(),
and
\fIprintf\fR()
functions shall return the number of bytes transmitted.
.P
Upon successful completion, the
\fIsprintf\fR()
function shall return the number of bytes written to
.IR s ,
excluding the terminating null byte.
.P
Upon successful completion, the
\fIsnprintf\fR()
function shall return the number of bytes that would be written to
.IR s
had
.IR n
been sufficiently large excluding the terminating null byte.
.P
If an output error was encountered, these functions shall return a
negative value
and set
.IR errno
to indicate the error.
.P
If the value of
.IR n
is zero on a call to
\fIsnprintf\fR(),
nothing shall be written, the number of bytes that would have been
written had
.IR n
been sufficiently large excluding the terminating null shall be
returned, and
.IR s
may be a null pointer.
.SH ERRORS
For the conditions under which
\fIdprintf\fR(),
\fIfprintf\fR(),
and
\fIprintf\fR()
fail and may fail, refer to
.IR "\fIfputc\fR\^(\|)"
or
.IR "\fIfputwc\fR\^(\|)".
.P
In addition, all forms of
\fIfprintf\fR()
shall fail if:
.TP
.BR EILSEQ
A wide-character code that does not correspond to a valid character
has been detected.
.TP
.BR EOVERFLOW
The value to be returned is greater than
{INT_MAX}.
.P
In addition, all forms of
\fIfprintf\fR()
may fail if:
.TP
.BR EINVAL
There are insufficient arguments.
.P
The
\fIdprintf\fR()
function may fail if:
.TP
.BR EBADF
The
.IR fildes
argument is not a valid file descriptor.
.P
The
\fIdprintf\fR(),
\fIfprintf\fR(),
and
\fIprintf\fR()
functions may fail if:
.TP
.BR ENOMEM
Insufficient storage space is available.
.P
The
\fIsnprintf\fR()
function shall fail if:
.TP
.BR EOVERFLOW
The value of
.IR n
is greater than
{INT_MAX}.
.LP
.IR "The following sections are informative."
.SH "EXAMPLES"
.SS "Printing Language-Independent Date and Time"
.P
The following statement can be used to print date and time using a
language-independent format:
.sp
.RS 4
.nf
\fB
printf(format, weekday, month, day, hour, min);
.fi \fR
.P
.RE
.P
For American usage,
.IR format
could be a pointer to the following string:
.sp
.RS 4
.nf
\fB
"%s, %s %d, %d:%.2d\en"
.fi \fR
.P
.RE
.P
This example would produce the following message:
.sp
.RS 4
.nf
\fB
Sunday, July 3, 10:02
.fi \fR
.P
.RE
.P
For German usage,
.IR format
could be a pointer to the following string:
.sp
.RS 4
.nf
\fB
"%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
.fi \fR
.P
.RE
.P
This definition of
.IR format
would produce the following message:
.sp
.RS 4
.nf
\fB
Sonntag, 3. Juli, 10:02
.fi \fR
.P
.RE
.SS "Printing File Information"
.P
The following example prints information about the type, permissions,
and number of links of a specific file in a directory.
.P
The first two calls to
\fIprintf\fR()
use data decoded from a previous
\fIstat\fR()
call. The user-defined
\fIstrperm\fR()
function shall return a string similar to the one at the beginning of the
output for the following command:
.sp
.RS 4
.nf
\fB
ls \(mil
.fi \fR
.P
.RE
.P
The next call to
\fIprintf\fR()
outputs the owner's name if it is found using
\fIgetpwuid\fR();
the
\fIgetpwuid\fR()
function shall return a
.BR passwd
structure from which the name of the user is extracted. If the user
name is not found, the program instead prints out the numeric value of
the user ID.
.P
The next call prints out the group name if it is found using
\fIgetgrgid\fR();
\fIgetgrgid\fR()
is very similar to
\fIgetpwuid\fR()
except that it shall return group information based on the group number.
Once again, if the group is not found, the program prints the numeric
value of the group for the entry.
.P
The final call to
\fIprintf\fR()
prints the size of the file.
.sp
.RS 4
.nf
\fB
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
.P
char *strperm (mode_t);
\&...
struct stat statbuf;
struct passwd *pwd;
struct group *grp;
\&...
printf("%10.10s", strperm (statbuf.st_mode));
printf("%4d", statbuf.st_nlink);
.P
if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
printf(" %\(mi8.8s", pwd->pw_name);
else
printf(" %\(mi8ld", (long) statbuf.st_uid);
.P
if ((grp = getgrgid(statbuf.st_gid)) != NULL)
printf(" %\(mi8.8s", grp->gr_name);
else
printf(" %\(mi8ld", (long) statbuf.st_gid);
.P
printf("%9jd", (intmax_t) statbuf.st_size);
\&...
.fi \fR
.P
.RE
.SS "Printing a Localized Date String"
.P
The following example gets a localized date string. The
\fInl_langinfo\fR()
function shall return the localized date string, which specifies the
order and layout of the date. The
\fIstrftime\fR()
function takes this information and, using the
.BR tm
structure for values, places the date and time information into
.IR datestring .
The
\fIprintf\fR()
function then outputs
.IR datestring
and the name of the entry.
.sp
.RS 4
.nf
\fB
#include <stdio.h>
#include <time.h>
#include <langinfo.h>
\&...
struct dirent *dp;
struct tm *tm;
char datestring[256];
\&...
strftime(datestring, sizeof(datestring), nl_langinfo (D_T_FMT), tm);
.P
printf(" %s %s\en", datestring, dp->d_name);
\&...
.fi \fR
.P
.RE
.SS "Printing Error Information"
.P
The following example uses
\fIfprintf\fR()
to write error information to standard error.
.P
In the first group of calls, the program tries to open the password
lock file named
.BR LOCKFILE .
If the file already exists, this is an error, as indicated by the
O_EXCL flag on the
\fIopen\fR()
function. If the call fails, the program assumes that someone else is
updating the password file, and the program exits.
.P
The next group of calls saves a new password file as the current
password file by creating a link between
.BR LOCKFILE
and the new password file
.BR PASSWDFILE .
.sp
.RS 4
.nf
\fB
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
.P
#define LOCKFILE "/etc/ptmp"
#define PASSWDFILE "/etc/passwd"
\&...
int pfd;
\&...
if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == \(mi1)
{
fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\en");
exit(1);
}
\&...
if (link(LOCKFILE,PASSWDFILE) == -1) {
fprintf(stderr, "Link error: %s\en", strerror(errno));
exit(1);
}
\&...
.fi \fR
.P
.RE
.SS "Printing Usage Information"
.P
The following example checks to make sure the program has the necessary
arguments, and uses
\fIfprintf\fR()
to print usage information if the expected number of arguments is not
present.
.sp
.RS 4
.nf
\fB
#include <stdio.h>
#include <stdlib.h>
\&...
char *Options = "hdbtl";
\&...
if (argc < 2) {
fprintf(stderr, "Usage: %s -%s <file\en", argv[0], Options); exit(1);
}
\&...
.fi \fR
.P
.RE
.SS "Formatting a Decimal String"
.P
The following example prints a key and data pair on
.IR stdout .
Note use of the
<asterisk>
(\c
.BR '*' )
in the format string; this ensures the correct number of decimal places
for the element based on the number of elements requested.
.sp
.RS 4
.nf
\fB
#include <stdio.h>
\&...
long i;
char *keystr;
int elementlen, len;
\&...
while (len < elementlen) {
\&...
printf("%s Element%0*ld\en", keystr, elementlen, i);
\&...
}
.fi \fR
.P
.RE
.SS "Creating a Pathname"
.P
The following example creates a pathname using information from a previous
\fIgetpwnam\fR()
function that returned the password database entry of the user.
.sp
.RS 4
.nf
\fB
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
\&...
char *pathname;
struct passwd *pw;
size_t len;
\&...
// digits required for pid_t is number of bits times
// log2(10) = approx 10/33
len = strlen(pw->pw_dir) + 1 + 1+(sizeof(pid_t)*80+32)/33 +
sizeof ".out";
pathname = malloc(len);
if (pathname != NULL)
{
snprintf(pathname, len, "%s/%jd.out", pw->pw_dir,
(intmax_t)getpid());
...
}
.fi \fR
.P
.RE
.SS "Reporting an Event"
.P
The following example loops until an event has timed out. The
\fIpause\fR()
function waits forever unless it receives a signal. The
\fIfprintf\fR()
statement should never occur due to the possible return values of
\fIpause\fR().
.sp
.RS 4
.nf
\fB
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
\&...
while (!event_complete) {
\&...
if (pause() != \(mi1 || errno != EINTR)
fprintf(stderr, "pause: unknown error: %s\en", strerror(errno));
}
\&...
.fi \fR
.P
.RE
.SS "Printing Monetary Information"
.P
The following example uses
\fIstrfmon\fR()
to convert a number and store it as a formatted monetary string named
.IR convbuf .
If the first number is printed, the program prints the format and the
description; otherwise, it just prints the number.
.sp
.RS 4
.nf
\fB
#include <monetary.h>
#include <stdio.h>
\&...
struct tblfmt {
char *format;
char *description;
};
.P
struct tblfmt table[] = {
{ "%n", "default formatting" },
{ "%11n", "right align within an 11 character field" },
{ "%#5n", "aligned columns for values up to 99\|999" },
{ "%=*#5n", "specify a fill character" },
{ "%=0#5n", "fill characters do not use grouping" },
{ "%^#5n", "disable the grouping separator" },
{ "%^#5.0n", "round off to whole units" },
{ "%^#5.4n", "increase the precision" },
{ "%(#5n", "use an alternative pos/neg style" },
{ "%!(#5n", "disable the currency symbol" },
};
\&...
float input[3];
int i, j;
char convbuf[100];
\&...
strfmon(convbuf, sizeof(convbuf), table[i].format, input[j]);
.P
if (j == 0) {
printf("%s\t%s\t%s\en", table[i].format,
convbuf, table[i].description);
}
else {
printf("\t%s\en", convbuf);
}
\&...
.fi \fR
.P
.RE
.SS "Printing Wide Characters"
.P
The following example prints a series of wide characters. Suppose that
.BR \(dqL`@`\(dq
expands to three bytes:
.sp
.RS 4
.nf
\fB
wchar_t wz [3] = L"@@"; // Zero-terminated
wchar_t wn [3] = L"@@@"; // Unterminated
.P
fprintf (stdout,"%ls", wz); // Outputs 6 bytes
fprintf (stdout,"%ls", wn); // Undefined because wn has no terminator
fprintf (stdout,"%4ls", wz); // Outputs 3 bytes
fprintf (stdout,"%4ls", wn); // Outputs 3 bytes; no terminator needed
fprintf (stdout,"%9ls", wz); // Outputs 6 bytes
fprintf (stdout,"%9ls", wn); // Outputs 9 bytes; no terminator needed
fprintf (stdout,"%10ls", wz); // Outputs 6 bytes
fprintf (stdout,"%10ls", wn); // Undefined because wn has no terminator
.fi \fR
.P
.RE
.P
In the last line of the example, after processing three characters,
nine bytes have been output. The fourth character must then be examined
to determine whether it converts to one byte or more. If it converts
to more than one byte, the output is only nine bytes. Since there is
no fourth character in the array, the behavior is undefined.
.SH "APPLICATION USAGE"
If the application calling
\fIfprintf\fR()
has any objects of type
.BR wint_t
or
.BR wchar_t ,
it must also include the
.IR <wchar.h>
header to have these objects defined.
.SH RATIONALE
None.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "Section 2.5" ", " "Standard I/O Streams",
.IR "\fIfputc\fR\^(\|)",
.IR "\fIfscanf\fR\^(\|)",
.IR "\fIsetlocale\fR\^(\|)",
.IR "\fIstrfmon\fR\^(\|)",
.IR "\fIwcrtomb\fR\^(\|)"
.P
The Base Definitions volume of POSIX.1\(hy2008,
.IR "Chapter 7" ", " "Locale",
.IR "\fB<stdio.h>\fP",
.IR "\fB<wchar.h>\fP"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.unix.org/online.html .
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
|