1
0

fprintf 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. .TH FPRINTF 2
  2. .SH NAME
  3. fprintf, printf, sprintf, snprintf, vfprintf, vprintf, vsprintf, vsnprintf \- print formatted output
  4. .SH SYNOPSIS
  5. .B #include <stdio.h>
  6. .PP
  7. .B
  8. int fprintf(FILE *f, char *format, ...)
  9. .PP
  10. .B
  11. int printf(char *format, ...)
  12. .PP
  13. .B
  14. int sprintf(char *s, char *format, ...)
  15. .PP
  16. .B
  17. int snprintf(char *s, int n, char *format, ...)
  18. .PP
  19. .B
  20. int vfprintf(FILE *f, char *format, va_list args)
  21. .PP
  22. .B
  23. int vprintf(char *format, va_list args)
  24. .PP
  25. .B
  26. int vsprintf(char *s, char *format, va_list args)
  27. .PP
  28. .B
  29. int vsnprintf(char *s, int n, char *format, va_list args)
  30. .SH DESCRIPTION
  31. .I Fprintf
  32. places output on the named output stream
  33. .I f
  34. (see
  35. .IR fopen (2)).
  36. .I Printf
  37. places output on the standard output stream
  38. .IR stdout .
  39. .I Sprintf
  40. places output
  41. followed by the null character
  42. .RB ( \e0 )
  43. in consecutive bytes starting at
  44. .IR s ;
  45. it is the user's responsibility to ensure that
  46. enough storage is available.
  47. .I Snprintf
  48. is like
  49. .I sprintf
  50. but writes at most
  51. .I n
  52. bytes (including the null character)
  53. into
  54. .IR s .
  55. .IR Vfprintf ,
  56. .IR vprintf ,
  57. .IR vsnprintf ,
  58. and
  59. .I vsprintf
  60. are the same, except the
  61. .I args
  62. argument is the argument list of the
  63. calling function, and the effect is as if the calling function's
  64. argument list from that point on is passed to the
  65. .I printf
  66. routines.
  67. .PP
  68. Each function returns the number of characters
  69. transmitted (not including the
  70. .B \e0
  71. in the case of
  72. .IR sprintf
  73. and friends),
  74. or
  75. a negative value if an output error was encountered.
  76. .PP
  77. These functions
  78. convert, format, and print their
  79. trailing arguments
  80. under control of a
  81. .IR format
  82. string.
  83. The
  84. .I format
  85. contains two types of objects:
  86. plain characters, which are simply copied to the
  87. output stream,
  88. and conversion specifications,
  89. each of which results in fetching of
  90. zero or more
  91. arguments.
  92. The results are undefined if there are arguments of the
  93. wrong type or too few
  94. arguments for the format.
  95. If the format is exhausted while
  96. arguments remain, the excess
  97. are ignored.
  98. .PP
  99. Each conversion specification is introduced by
  100. the character
  101. .BR % .
  102. After the
  103. .BR % ,
  104. the following
  105. appear in sequence:
  106. .PP
  107. .RS
  108. Zero or more
  109. .IR flags ,
  110. which modify the meaning of
  111. the conversion specification.
  112. .PP
  113. An optional decimal digit string specifying a minimum
  114. .IR "field width" .
  115. If the converted value has fewer characters
  116. than the field width, it will be padded with spaces on the
  117. left (or right, if the left adjustment, described later, has been given)
  118. to the field width.
  119. .PP
  120. An optional
  121. .I precision\^
  122. that gives
  123. the minimum number of digits to appear for the
  124. .BR d ,
  125. .BR i ,
  126. .BR o ,
  127. .BR u ,
  128. .BR x ,
  129. and
  130. .B X
  131. conversions,
  132. the number of digits to appear after the
  133. decimal point for the
  134. .BR e ,
  135. .BR E ,
  136. and
  137. .B f
  138. conversions,
  139. the maximum number of significant digits
  140. for the
  141. .B g
  142. and
  143. .B G
  144. conversions,
  145. or the maximum number of characters
  146. to be written from a string in
  147. .B s
  148. conversion.
  149. The precision takes the form of a period
  150. .RB ( \&. )
  151. followed by an optional decimal integer;
  152. if the integer is omitted, it is treated as zero.
  153. .PP
  154. An optional
  155. .B h
  156. specifying that a following
  157. .BR d ,
  158. .BR i ,
  159. .BR o ,
  160. .BR u ,
  161. .BR x
  162. or
  163. .BR X
  164. conversion specifier applies to a
  165. .B short
  166. .B int
  167. or
  168. .B unsigned
  169. .B short
  170. argument (the argument will have been promoted according to the integral
  171. promotions, and its value shall be converted to
  172. .B short
  173. or
  174. .B unsigned
  175. .B short
  176. before printing);
  177. an optional
  178. .B h
  179. specifying that a following
  180. .B n
  181. conversion specifier applies to a pointer to a
  182. .B short
  183. argument;
  184. an optional
  185. .B l
  186. (ell) specifying that a following
  187. .BR d ,
  188. .BR i ,
  189. .BR o ,
  190. .BR u ,
  191. .BR x ,
  192. or
  193. .B X
  194. conversion character applies to a
  195. .B long
  196. or
  197. .B unsigned
  198. .B long
  199. argument;
  200. an optional
  201. .B l
  202. specifying that a following
  203. .B n
  204. conversion specifier applies to a pointer to a
  205. .B long
  206. .B int
  207. argument;
  208. or an optional
  209. .B L
  210. specifying that a following
  211. .BR e ,
  212. .BR E ,
  213. .BR f ,
  214. .BR g ,
  215. or
  216. .B G
  217. conversion specifier applies to a
  218. .B long double
  219. argument.
  220. If an
  221. .BR h ,
  222. .BR l ,
  223. or
  224. .B L
  225. appears with any other conversion specifier, the behavior is undefined.
  226. .PP
  227. A character that indicates the type of
  228. conversion to be applied.
  229. .RE
  230. .PP
  231. A field width or precision, or both, may be
  232. indicated by an asterisk
  233. .RB ( * )
  234. instead of a digit string.
  235. In this case, an
  236. .B int
  237. .I arg\^
  238. supplies
  239. the field width or precision.
  240. The arguments specifying field width or precision, or both,
  241. shall appear (in that order) before the argument (if any) to be converted.
  242. A negative field width argument is taken as a
  243. .B -
  244. flag followed by a positive field width.
  245. A negative precision is taken as if it were missing.
  246. .PP
  247. The flag characters and their meanings are:
  248. .PD 0
  249. .TP 10
  250. .B -
  251. The result of the conversion is left-justified within the field.
  252. .TP
  253. .B +
  254. The result of a signed
  255. conversion always begins with a sign
  256. .RB ( +
  257. or
  258. .BR - ).
  259. .TP
  260. blank
  261. If the first character of a signed conversion is not a sign,
  262. or a signed conversion results in no characters,
  263. a blank
  264. is prefixed to the result.
  265. This implies that if the blank and
  266. .B +
  267. flags both appear, the blank flag is ignored.
  268. .TP
  269. .B #
  270. The result is to be converted
  271. to an ``alternate form.''
  272. For
  273. .B o
  274. conversion, it increases the precision to force
  275. the first digit of the result to be a zero.
  276. For
  277. .B x
  278. or
  279. .B X
  280. conversion, a non-zero result has
  281. .B 0x
  282. or
  283. .B 0X
  284. prefixed to it.
  285. For
  286. .BR e ,
  287. .BR E ,
  288. .BR f ,
  289. .BR g ,
  290. and
  291. .B G
  292. conversions, the result always contains a decimal point,
  293. even if no digits follow the point (normally, a decimal point
  294. appears in the result of these conversions only if a digit
  295. follows it).
  296. For
  297. .B g
  298. and
  299. .B G
  300. conversions, trailing zeros are
  301. .I not\^
  302. be removed from the result
  303. as they normally are.
  304. For other conversions, the behavior is undefined.
  305. .TP
  306. .B 0
  307. For
  308. .BR d ,
  309. .BR i ,
  310. .BR o ,
  311. .BR u ,
  312. .BR x ,
  313. .BR X ,
  314. .BR e ,
  315. .BR E ,
  316. .BR f ,
  317. .BR g ,
  318. and
  319. .B G
  320. conversions, leading zeros (following any indication of sign or base)
  321. are used to pad the field width; no space padding is performed.
  322. If the
  323. .B 0
  324. and
  325. .B -
  326. flags both appear, the
  327. .B 0
  328. flag will be ignored.
  329. For
  330. .BR d ,
  331. .BR i ,
  332. .BR o ,
  333. .BR u ,
  334. .BR x ,
  335. and
  336. .B X
  337. conversions, if a precision is specified, the
  338. .B 0
  339. flag will be ignored.
  340. For other conversions, the behavior is undefined.
  341. .PD
  342. .PP
  343. The conversion characters
  344. and their meanings are:
  345. .PP
  346. .PD 0
  347. .TP 10
  348. \fLd\fP,\fLo\fP,\fLu\fP,\fLx\fP,\fLX\fP
  349. The integer
  350. .I arg\^
  351. is converted to signed decimal
  352. .RB ( d
  353. or
  354. .BR i ),
  355. unsigned octal
  356. .RB ( o ),
  357. unsigned decimal
  358. .RB ( u ),
  359. or unsigned hexadecimal notation
  360. .RB ( x
  361. or
  362. .BR X );
  363. the letters
  364. .B abcdef
  365. are used for
  366. .B x
  367. conversion and the letters
  368. .B ABCDEF
  369. for
  370. .B X
  371. conversion.
  372. The precision specifies the minimum number of digits
  373. to appear; if the value being converted can be represented
  374. in fewer digits, it is expanded with leading zeros.
  375. The default precision is 1.
  376. The result of converting a zero value with a precision
  377. of zero is no characters.
  378. .TP
  379. .BR f
  380. The
  381. .B double
  382. argument is converted to decimal notation
  383. in the style
  384. [\-]\fIddd\fL.\fIddd\fR,
  385. where the number of digits after the decimal point
  386. is equal to the precision specification.
  387. If the precision
  388. is missing,
  389. it is taken as 6;
  390. if the precision is explicitly
  391. .LR 0 ,
  392. no decimal point appears.
  393. .TP
  394. .BR e , E
  395. The
  396. .B double
  397. argument is converted in the style
  398. [\-]\fId\fL.\fIddd\fLe\fR±\fIdd\fR,
  399. where there is one digit before the decimal point and
  400. the number of digits after it is equal to the
  401. precision;
  402. when the precision is missing, it is taken as 6;
  403. if the precision is zero, no decimal point appears.
  404. The
  405. .B E
  406. format code produces a number with
  407. .B E
  408. instead of
  409. .B e
  410. introducing the exponent.
  411. The exponent always contains at least two digits.
  412. .TP
  413. .BR g , G
  414. The
  415. .B double
  416. argument is printed in style
  417. .BR f
  418. or
  419. .BR e
  420. (or in style
  421. .B E
  422. in the case of a
  423. .B G
  424. conversion specifier),
  425. with the precision specifying the number of significant digits.
  426. If an explicit precision is zero, it is taken as 1.
  427. The style used depends on the value converted:
  428. style
  429. .B e
  430. is used only if the exponent resulting from
  431. the conversion is less than \-4
  432. or greater than or equal to the precision.
  433. Trailing zeros are removed from the fractional portion of the result;
  434. a decimal point appears only if it is followed by a digit.
  435. .TP
  436. .B c
  437. The
  438. .B int
  439. argument is converted to an
  440. .B unsigned
  441. .BR char ,
  442. and the resulting character is written.
  443. .TP
  444. .B s
  445. The
  446. argument is taken to be a string (character pointer)
  447. and characters from the string are printed until
  448. a null character
  449. .RB ( \e0 )
  450. is encountered or
  451. the number of characters indicated by the precision
  452. specification is reached.
  453. If the precision is missing, it is taken to be infinite, so
  454. all characters up to the first null character are printed.
  455. A
  456. zero
  457. value for
  458. the argument yields undefined results.
  459. .TP
  460. .B P
  461. The
  462. .B void*
  463. argument is printed in an implementation-defined way (for Plan 9:
  464. the address as hexadecimal number).
  465. .TP
  466. .B n
  467. The argument shall be a pointer to an integer into which is
  468. .I written
  469. the number of characters written to the output stream so far by
  470. this call to
  471. .IR fprintf .
  472. No argument is converted.
  473. .TP
  474. .B %
  475. Print a
  476. .BR % ;
  477. no argument is converted.
  478. .PD
  479. .PP
  480. If a conversion specification is invalid, the behavior is undefined.
  481. .PP
  482. If any argument is, or points to, a union or an aggregate
  483. (except for an array of character type using
  484. .B %s
  485. conversion, or a pointer cast to be a pointer to
  486. .B void
  487. using
  488. .B %P
  489. conversion), the behavior is undefined.
  490. .PP
  491. In no case does a nonexistent or small field width cause truncation
  492. of a field; if the result of a conversion is wider than the field width,
  493. the field is expanded to contain the conversion result.
  494. .SH SOURCE
  495. .B /sys/src/libstdio
  496. .SH SEE ALSO
  497. .IR fopen (2),
  498. .IR fscanf (2),
  499. .IR print (2)
  500. .SH BUGS
  501. There is no way to print a wide character (rune); use
  502. .IR print (2)
  503. or
  504. .IR bio (2).