printf

PRINTF(3)                  Linux Programmer's Manual                 PRINTF(3)



åå
       printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
       vsnprintf - æå®ãããæ¸å¼ã«å¤æãã¦åºåãè¡ã

æ¸å¼
       #include <stdio.h>

       int printf(const char *format, ...);
       int fprintf(FILE *stream, const char *format, ...);
       int sprintf(char *str, const char *format, ...);
       int snprintf(char *str, size_t size, const char *format, ...);

       #include <stdarg.h>

       int vprintf(const char *format, va_list ap);
       int vfprintf(FILE *stream, const char *format, va_list ap);
       int vsprintf(char *str, const char *format, va_list ap);
       int vsnprintf(char *str, size_t size, const char *format, va_list ap);

   glibc åãã®æ©è½æ¤æ»ãã¯ãã®è¦ä»¶ (feature_test_macros(7)  åç§):

       snprintf(), vsnprintf():
           _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE ||
           _POSIX_C_SOURCE >= 200112L;
           or cc -std=c99

説æ
       printf()  é¢æ°ã°ã«ã¼ãã¯ã以ä¸ã§è¿°ã¹ãããã«ã format
       ã«å¾ã£ã¦åºåãçæãããã®ã§ããã printf()  㨠vprintf()  ã¯åºåã stdout
       (æ¨æºåºåã¹ããªã¼ã ) ã«æ¸ãåºãã fprintf()  㨠vfprintf()  ã¯åºåãæå®ãããåºå
       stream ã«æ¸ãåºãã sprintf(), snprintf(), vsprintf(), vsnprintf()
       ã¯åºåãæåå str ã«æ¸ãè¾¼ãã

       snprintf()  㨠vsnprintf()  ã¯æ大㧠size ãã¤ãã str ã«æ¸ãè¾¼ã (size
       ã«ã¯æååãçµç«¯ãã NULL ãã¤ã ('\0') ããå«ã¾ãã)ã

       vprintf(), vfprintf(), vsprintf(), vsnprintf()  ã®åé¢æ°ã¯ãããã printf(),
       fprintf(), sprintf(), snprintf(), ã®åé¢æ°ã¨ç‐
       価ã§ãããå¯å¤æ°å¼ãæ°ã®ä»£ããã« va_list ãå¼ãæ°ã¨ãã¦å¼ã³åºãããç¹ã ããç°ãªãã
       ãããã®é¢æ°ã§ã¯ va_end ãã¯ãã¯å¼ã³åºãããªãã ãããã®é¢æ°ã¯ va_arg
       ãå¼ã³åºãã®ã§ãå¼ã³åºãå¾ã® ap ã®å¤ã¯æªå®ç¾©ã§ããã stdarg(3) ãåç§ã®ãã¨ã

       ãããã® 8 ã¤ã®é¢æ°ã¯ format æååã®å¶å¾¡ã«å¾ã£ã¦åºåãæ¸ãåºãã format æå‐
       åã¯ãããã«ç¶ãå¼ãæ° (ã¾ã㯠stdarg(3)
       ã®å¯å¤é·å¼ãæ°æ©æ§ã使ã£ã¦ã¢ã¯ã»ã¹ã§ããå¼ãæ°)
       ãã©ã®ããã«å¤æãã¦åºåããããæå®ããã

       C99 㨠POSIX.1-2001 ã§ã¯ã sprintf(), snprintf(), vsprintf(),
       vsnprintf()  ã®å¼ã³åºãã§ãç¯å²ãéè¤ãããªãã¸ã§ã¯ãéã§ã³ãã¼ãçºçããå ´åã®
       çµæã¯ä¸å®ã§ããã¨è¦å®ããã¦ãã (ä¾ãã°ãåºååã®æååã¨å¥åããã
       å¼ãæ°ã®ä¸ã¤ãåããããã¡ãåç§ãã¦ããå ´åãªã©ã§ãã)ã ã注æãã®ç¯ãåç§ã

   Return Values
       æåæã«ã¯ãä¸è¨ã®é¢æ°ã¯æ¸ãè¾¼ã¾ããæåæ°ãè¿ã (æååã®æå¾ã示ãããã«ä½¿ç¨ãã NULL
       ãã¤ãã¯æ°ã«å«ã¾ããªã)ã

       snprintf()  㨠vsnprintf()  ã¯ã size ãã¤ããè¶ããæåæ°ãæ¸ãè¾¼ã¾ãªã (size
       ã«ã¯æååãçµç«¯ãã NULL ãã¤ã ('\0') ãå«ã¾ãã)ã
       ãã®å¶éã«ãã£ã¦åºåãåãè©°ããããå ´åã«ã¯ã
       ããååãªã¹ãã¼ã¹ãããã°æ¸ãè¾¼ã¾ããã§ãããæåã®åæ° (æååãçµç«¯ãã NULL ãã¤ããé¤ã)
       ãè¿ãã å¾ã£ã¦ãè¿ãå¤ã size 以ä¸ã ã£ãå ´åãåºåãåãè©°ãããããã¨ãæå³ãã
       (å¾è¿°ã®æ³¨æãåç§ã®ãã¨)ã

       ã¨ã©ã¼ãçºçããå ´åã¯ãè² ã®æ°ãè¿ãã

   ãã©ã¼ãããæååã®ãã©ã¼ããã
       ãã©ã¼ãããæååã¯æåã®åã§ã (ãããããªã) åæã·ããç¶æã§å§ã¾ããåæã·ããç¶æã§çµããã
       ãã©ã¼ãããç¨ã®æåå㯠0 å以ä¸ã®å½ä»¤ (directives) ã«ãã£ã¦æ§æãããã
       å½ä»¤ã«ã¯ãé常æåã¨å¤ææå® (conversion specifications) ãããã é常æå㯠%
       以å¤ã®æåã§ãåºåã¹ããªã¼ã ã«ãã®ã¾ã¾ã³ãã¼ãããã å¤ææå®ã¯ãããããã 0
       å以ä¸ã®å¼ãæ°ãåãã åå¤ææå®ã¯æå % ã§å§ã¾ãã å¤ææå®å (conversion specifier)
       ã§çµããã % ã¨å¤ææå®åã®éã«ã¯ã0 å以ä¸ã® ãã©ã° ã æå° ãã£ã¼ã«ãå¹ ã 精度 ã
       é·ã修飾å ã (ãã®é åºã§) ç½®ããã¨ãã§ããã

       å¼ãæ°ã¯ (åã®æ ¼ä¸ãã®å¾ã¯) å¤ææå®åã表ãåã¨æ£ç¢ºã«å¯¾å¿ããªããã°ãªããªãã
       ããã©ã«ãã§ã¯ã'*' ãå¤ææå®åãåºã¦ããæ¯ã«æ¬¡ã®å¼ãæ°ãè¦æ±ããã
       å¼ãæ°ã¯æå®ãããé åºã§ä½¿ç¨ããã¦ãã (æå®ãããå¼ãæ°ã®åæ°ãä¸ååãªãã¨ã©ã¼ã¨ãªã)ã
       ã¾ããå¼ãæ°ãå¿è¦ãªç®æ㧠'%' ã®ä»£ããã« "%m$"ã '*'ã®ä»£ããã« "*m$"
       ã¨æ¸ããã¨ã§ã æ示çã«ã©ã®å¼ãæ°ã使ç¨ããããæå®ãããã¨ãã§ããã ãã㧠10é²ã®æ´æ° m
       ã¯å¸æã®å¼ãæ°ã®å¼ãæ°ãªã¹ãã§ã®ä½ç½®ã示ã (æåã®å¼ãæ°ã®çªå·ã 1 ã§ãã)ã
       å¾ã£ã¦ã

           printf("%*d", width, num);

       ã¨

           printf("%2$*1$d", width, num);

       ã¯ç価ã§ããã äºçªç®ã®æ¸ãæ¹ã§ã¯åãå¼ãæ°ãç¹°ãè¿ãåç§ãããã¨ãã§ããã C99
       æ¨æºã«ã¯ã Single UNIX Specification ç±æ¥ã® '$'
       ã使ã£ãæ¸ãæ¹ã¯å«ã¾ãã¦ããªãã '$'
       ã使ã£ãã¹ã¿ã¤ã«ã使ãã¨ãå¼ãæ°ãåãå¤æåã³å¹ã¨ç²¾åº¦ã®å¼ãæ°ã
       å¨ã¦ãã®ã¹ã¿ã¤ã«ã§æå®ããªããã°ãªããªããã å¼ãæ°ãæ¶è²»ããªã "%%"
       ãã©ã¼ãããã¨æ··ãã£ã¦ãããããããªãã '$'
       ã§æå®ãããå¼ãæ°ã®çªå·ã«ç©ºãããã£ã¦ã¯ãªããªãã ä¾ãã°ãããå¼ãæ° 1 㨠3
       ãæå®ãããã¨ãå¼ãæ° 2 ããã©ã¼ãããæååã®ã©ãã㧠æå®ãããªããã°ãªããªãã

       æ°å¤å¤æã«ã¯å°æ°ç¹ã 1000 åä½ã®åºåãæåã使ããã®ãããã å®éã«ã©ã®æåã使ããã¯ã‐
       ã±ã¼ã«ã® LC_NUMERIC ã«ããã POSIX ãã±ã¼ã«ã§ã¯å°æ°ç¹ã« '.' ãç¨ãã åºåãæå‐
       ã¯ä½¿ããªãã å¾ã£ã¦ã

           printf("%'.2f", 1234567.89);

       ã¯ã POSIX ãã±ã¼ã«ã§ã¯ "1234567.89" ã nl_NL ãã±ã¼ã«ã§ã¯ "1234567,89"ã
       da_DK ãã±ã¼ã«ã§ã¯ "1.234.567,89" ã¨ãªãã

   ãã©ã°æå
       % æåã®å¾ãã«ã¯ 0 å以ä¸ã®ãã©ã°æåãç¶ãã

       #      å¤ã¯ãå¥ã®å½¢å¼ãã«å¤æãããã o å¤æã®å ´åã(åé æåã 0
              ã«ãªã£ã¦ããªãå ´åã«åé ã« 0 ã追å ãããã¨ã§)  åºåæååã®æåã®æåã 0
              ã«ããã x 㨠X å¤æã®å ´åãæ°å¤ã 0 ã§ãªãã¨ãã«ã¯æåå "0x" (X
              å¤æã®å ´åã«ã¯ "0X") ãåã«ä»ä¸ãããã a, A, e, E, f, F, g, G å¤æã§ã¯ã
              å°æ°ç¹ã«ç¶ãæ°åããªãã¦ãã åºåã«ã¯å¸¸ã«å°æ°ç¹ãå«ã¾ãã
              (é常ã¯ãå°æ°ç¹ã®å¾ã«æ°åãç¶ãå ´åã«ã®ã¿ã å°æ°ç¹ã表示ããã)ã g 㨠G
              å¤æã®å ´åãä»ã®å¤æã¨ã¯ç°ãªããæ«å°¾ã®ã¼ããå¤æçµæããåé¤ãããªãã
              ãã®ä»ã®å¤æã§ã¯ãçµæã¯æªå®ç¾©ã§ããã

       0      å¤ãã¼ãã§åããã d, i, o, u, x, X, a, A, e, E, f, F, g, G
              å¤æã§ã¯ãå¤æããå¤ã®å·¦å´ã空ç½æåã®ä»£ããã«ã¼ãã§åããã 0 㨠-
              ã両æ¹ã¨ãæå®ãããå ´åã¯ã 0 ãã©ã°ã¯ç¡è¦ãããã 精度ãæ°å¤å¤æ (d, i,
              o, u, x, X) ã¨åæã«æå®ãããå ´åã«ã¯ã 0 ãã©ã°ã¯ç¡è¦ãããã
              ãã®ä»ã®å¤æã§ã¯ãåä½ã¯æªå®ç¾©ã§ããã

       -      å¤æå¤ããã£ã¼ã«ãå¢çã§å·¦æãã«ãã (ããã©ã«ãã¯å³æãã§ãã)ã n
              å¤æ以å¤ã§ã¯ãå¤æãããå¤ã¯ å·¦å´ã§ã¯ãªãå³å´ã空ç½æåãã¼ãã§åããããã -
              㨠0 ã®ä¸¡æ¹ãæå®ãããå ´åã«ã¯ã - ãåªåãããã

       ' '    (1åã®åè§ã¹ãã¼ã¹)  符å·ä»ãå¤æã§çæãããæ£ã®æ°åã®åã«ç©ºç½
              (ã¾ãã¯ç©ºæåå) ãç½®ãããã

       +      符å·ä»ãå¤æã«ãã£ã¦åºåãããæ°åã®åã«ã常ã«ç¬¦å· (+ ã -) ãç½®ãããã
              ããã©ã«ãã§ã¯ã符å·ã¯è² ã®æ°åã®å ´åã®ã¿ä»ä¸ãããã + ã¨åè§ã¹ãã¼ã¹ã®
              両æ¹ã使ããã¦ããå ´åã«ã¯ã + ãåªåãããã

       ä¸è¨ã® 5 ã¤ã®ãã©ã°ã¯ C æ¨æºã§å®ç¾©ããã¦ããã SUSv2 ã§ã¯ãããã«ããä¸ã¤ãã©ã°æå‐
       ãè¦å®ããã¦ããã

       '      10é²æ°å¤æ (i, d, u, f, F, g, G)  ã«ããã¦ããã±ã¼ã«æå ±ã«æå®ãããã°
              1000 åä½ã®åºåãæåãåºåããã gcc(1)
              ã®å¤ãã®ãã¼ã¸ã§ã³ã¯ããã®ãªãã·ã§ã³ã解éãããã¨ãã§ããã è‐
              ¦åãåºåãããã¨ã«æ³¨æããã %'F 㯠SUSv2 ã«ã¯å«ã¾ãã¦ããªãã

       glibc 2.2 ã§ã¯ãããã«ä¸ã¤ãã©ã°æåã追å ããã¦ããã

       I      10é²æ´æ°å¤æ (i, d, u)  ã«ããã¦ããã±ã¼ã«ã®ä»£æ¿åºåæ°å‐
              ãããã°ããããç¨ãã¦åºåããã ä¾ãã°ã glibc 2.2.3 以éã§ã¯ããã«ã·ã¢
              ("fa_IR") ãã±ã¼ã«ã§ ã¢ã©ãã¢æ°å (Arabic-Indic digits) ãåºåã§ããã

   ãã£ã¼ã«ãå¹
       æå°ã®ãã£ã¼ã«ãå¹ãæå®ãã 10é²æ°ã®æ°å¤æåå (æååã®æåã®æå㯠ã¼ã‐
       以å¤)ãæ¬é ç®ã¯ãªãã·ã§ã³ã§ããã å¤æãããå¤ã®æåæ°ããã£ã¼ã«ãé·ãããå°ãªãå ´åã
       ãã£ã¼ã«ãã®å·¦å´ãã¹ãã¼ã¹ã§åãã (å·¦æãã®ãã©ã°ãããå ´åã¯å³å´ãåãã)ã
       10é²æ°ã®æååã®ä»£ããã« "*" ã "*m$" (m 㯠10é²æ´æ°) ãæ¸ããã¨ãã§ããã "*" ã¨
       "*m$" ã¯ããããã次ã®å¼ãæ°ã¨ m çªç®ã®å¼ãæ°ããã£ã¼ã«ãå¹ã¨ã㦠使ããã¨ãæå®ãã
       (ãããã®å¼ãæ°ã¯ int åã§ãªããã°ãªããªã)ã ãã£ã¼ã«ãå¹ã«è² ã®æ°ãæå®ãããå ´åã¯ã
       '-' ãã©ã°ã¨æ£ã®æ°ã®ãã£ã¼ã«ãå¹ã¨ãã¦æ±ãããã
       ãã£ã¼ã«ãå¹ãå°ããã£ããæå®ããªãã£ãããã¦ãããã£ã¼ã«ããåãè©°ãããã
       ãã¨ã¯ãªããããå¤æçµæããã£ã¼ã«ãå¹ãããåºãã£ãå ´åã
       ãã£ã¼ã«ãã¯å¤æçµæãå¥ãå¹ã«åºããããã

   精度
       ãªãã·ã§ã³ã§ãã精度ã¯ãããªãªã ('.') ã¨ããã«ç¶ã10é²æ°ã¨ãã å½¢å¼ã§æå®ãã
       (10é²æ°ã¯ãªãã·ã§ã³) ã 10é²æ°ã®æååã®ä»£ããã« "*" ã "*m$" (m 㯠10
       é²æ´æ°)ãæ¸ããã¨ãã§ããã "*" 㨠"*m$" ã¯ããããã次ã®å¼ãæ°ã¨ m
       çªç®ã®å¼ãæ°ã精度ã¨ã㦠使ããã¨ãæå®ãã (ãããã®å¼ãæ°ã¯ int
       åã§ãªããã°ãªããªã)ã 精度ã¨ã㦠'.' ã ããæå®ããããã精度ãè² ã®æ°ã ã£ãå ´åã
       精度ã¯ã¼ãã¨ã¿ãªãããã d, i, o, u, x, X
       å¤æã§ã¯ã表示ãããæå°ã®æ¡æ°ãæå®ããã a, A, e, E, f, F
       å¤æã§ã¯ãå°æ°ç¹ä»¥ä¸ã«è¡¨ç¤ºãããæ°åã®æ¡æ°ãæå®ããã g 㨠G å¤æã§ã¯ãæå¹æ°å‐
       ã®æ大æ¡æ°ãæå®ããã s 㨠S å¤æã§ã¯ãæååããåºåãããæ大æåæ°ãæå®ããã

   é·ã修飾å
       ãæ´æ°å¤æãã¨ã¯ã d, i, o, u, x, X å¤æã®ãã¨ã§ããã

       hh     æ´æ°å¤æã«å¯¾å¿ããå¼ãæ°ã signed char ã unsigned char ã§ã n
              å¤æã«å¯¾å¿ããå¼ãæ°ã signed char ã¸ã®ãã¤ã³ã¿ã§ãããã¨ã示ãã

       h      æ´æ°å¤æã«å¯¾å¿ããå¼ãæ°ã short int ã unsigned short int ã§ã n
              å¤æã«å¯¾å¿ããå¼ãæ°ã short int ã¸ã®ãã¤ã³ã¿ã§ãããã¨ã示ãã

       l      åå¤æã«å¯¾å¿ããå¼ãæ°ãã æ´æ°å¤æã§ã¯ long intã unsigned long intã n
              å¤æã§ã¯ long long int ã¸ã®ãã¤ã³ã¿ã c å¤æã§ã¯ wint_tã s å¤æã§ã¯
              wchar_t ã¸ã®ãã¤ã³ã¿ã§ãããã¨ã示ãã

       ll (ã¨ã«ã¨ã«)
              æ´æ°å¤æã«å¯¾å¿ããå¼ãæ°ã long long int ã unsigned long long int
              ã§ã n å¤æã«å¯¾å¿ããå¼ãæ°ã long int ã¸ã®ãã¤ã³ã¿ã§ãããã¨ã示ãã

       L      a, A, e, E, f, F, g, G å¤æã«å¯¾å¿ããå¼ãæ°ã long double
              ã§ãããã¨ã示ãã (C99 ã§ã¯ %LF ã使ããã¨ãèªãã¦ããããSUSv2
              ã§ã¯èªãããã¦ããªãã)

       q      ("quad"ã 4.4BSD 㨠Linux libc5 ã®ã¿æå¹ã使ã£ã¦ã¯ãªããªãã)  ll
              ã¨åãæå³ã§ããã

       j      æ´æ°å¤æã«å¯¾å¿ããå¼ãæ°ã intmax_t ã uintmax_t ã§ãããã¨ã示ãã

       z      æ´æ°å¤æã«å¯¾å¿ããå¼ãæ°ã size_t ã ssize_t ã§ãããã¨ã示ãã (Linux
              libc5 ã§ã¯ããããæå®ããã®ã« Z ãç¨ããã使ã£ã¦ã¯ãªããªãã)

       t      æ´æ°å¤æã«å¯¾å¿ããå¼ãæ°ã ptrdiff_t ã§ãããã¨ã示ãã

       SUSv2 ã§é·ã修飾åã¨ãã¦ä½¿ç¨ã§ããã®ã¯ã h (hd, hi, ho, hx, hX, hn), l
       (ld, li, lo, lx, lX, ln, lc, ls), L (Le, LE, Lf, Lg, LG)  ã ãã§ããã

   å¤ææå®å
       é©ç¨ãããå¤æã®åãæå®ããæåã å¤ææå®åã¨ãã®æå³ã¯ä»¥ä¸ã®éãã§ããã

       d, i   int å¼ãæ°ã符å·ä»ã 10 é²è¡¨è¨ã«å¤æããã
              精度æå®ãããã°ã精度ã§æå®ããæ¡æ°ã¯å¿ãåºåããããå¤æå¾ã®å¤ã
              æå®ãããæ¡æ°ã«è¶³ããªãå ´åã¯ãå·¦å´ã 0 ã§åããããã ããã©ã«ãã®ç²¾åº¦ã¯ 1
              ã§ããã 0 ã表示ãããã¨ããæã«ãæ示çã«ç²¾åº¦ã¨ã㦠0 ãæå®ããã¦ããã¨ã
              åºåã¯ç©ºæååã¨ãªãã

       o, u, x, X
              unsigned int å¼ãæ°ãã 符å·ãªã8é²æ° (o), 符å·ãªã10é²æ° (u),
              符å·ãªã16é²æ° (x 㨠X)  ã«å¤æããã x å¤æã§ã¯ abcdef ã使ç¨ããã X
              å¤æã§ã¯ ABCDEF ã使ç¨ãããã
              精度æå®ãããã°ã精度ã§æå®ããæ¡æ°ã¯å¿ãåºåããããå¤æå¾ã®å¤ã
              æå®ãããæ¡æ°ã«è¶³ããªãå ´åã¯ãå·¦å´ã 0 ã§åããããã

       e, E   double å¼ãæ°ã丸ã㦠[-]d.ddde±dd ã®å½¢ã«å¤æããã
              å°æ°ç¹ã®åã«ã¯ä¸æ¡ã®æ°åããããå°æ°ç¹ä»¥ä¸ã®æ¡æ°ã¯ç²¾åº¦ã§æå®ãããæ¡æ°
              ã«ãªãã精度ã¯æå®ãããªãã£ãå ´å 6 ã¨ã¿ãªãããã 精度ã 0 ã®å ´åã«ã¯ã
              å°æ°ç¹ä»¥ä¸ã¯è¡¨ç¤ºãããªããE å¤æã§ã¯ãææ°ã表ç¾ããã¨ãã« (e 㧠ã¯ãªã)
              E ã使ããããææ°é¨åã¯å°ãªãã¨ã 2æ¡è¡¨ç¤ºããããã¤ã¾ãã ææ°ã®å¤ã 0
              ã®å ´åã«ã¯ã00 ã¨è¡¨ç¤ºãããã

       f, F   double å¼ãæ°ã丸ã㦠[-]ddd.ddd ã®å½¢ã®10é²è¡¨ç¾ã«å¤æããã
              å°æ°ç¹ã®å¾ã®æ¡æ°ã¯ã精度ã§æå®ãããå¤ã¨ãªãã
              精度ãæå®ããã¦ããªãå ´åã«ã¯ 6 ã¨ãã¦æ±ãããã 精度ã¨ãã¦æ示çã« 0
              ãæå®ãããã¨ãã«ã¯ãå°æ°ç¹ä»¥ä¸ã¯è¡¨ç¤ºãããªãã
              å°æ°ç¹ã表示ããéã«ã¯ãå°æ°ç¹ã®åã«å°ãªãã¨ãä¸æ¡ã¯æ°åã表示ãããã

              (SUSv2 ã§ã¯ãF ã¯è¦å®ããã¦ããããç¡éã NaN ã«é¢ããæåå表ç¾ã
              è¡ã£ã¦ããããã¨ã«ãªã£ã¦ããã
               C99 æ¨æºã§ã¯ãf å¤æã§ã¯ãç¡é㯠"[-]inf" ã "[-]infinity"
              ã¨è¡¨ç¤ºãã NaN ã¯æååã®åé ã« `nan' ãã¤ãã¦è¡¨ç¤ºããããã«è¦å®ããã¦ããã
              F å¤æã®å ´å㯠"[-]INF", "[-]INFINITY", "NAN*" ã¨è¡¨ç¤ºãããã)

       g, G   double å¼ãæ°ã f ã e (G å¤æã®å ´å㯠F ã E)  ã®å½¢å¼ã«å¤æããã
              精度ã¯è¡¨ç¤ºããæ¡æ°ãæå®ããã 精度ãæå®ãããªãå ´åã¯ã6æ¡ã¨ã¿ãªãããã
              精度ã 0 ã®å ´åã¯ã1æ¡ã¨ã¿ãªãããã å¤æãããå¤ã®ææ°ãã -4
              ããå°ãããã精度以ä¸ã®å ´åã«ã e å½¢å¼ã使ç¨ãããã
              å¤æãããçµæã®å°æ°é¨åã®æ«å°¾ã® 0 ã¯åé¤ããããå°æ°ç¹ã表示ãããã®ã¯ã
              å°æ°ç¹ä»¥ä¸ã«æ°åãå°ãªãã¨ãä¸ã¤ããå ´åã«ã ãã§ããã

       a, A   (C99 ã«ã¯ããã SUSv2 ã«ã¯ãªã) a å¤æã§ã¯ã double å¼ãæ°ã (abcdef
              ã®æåã使ã£ã¦) [-]0xh.hhhhp± å½¢å¼ã® 16 é²è¡¨è¨ã«å¤æããã A
              å¤æã§ã¯ãåç½®æåå 0X, æå ABCDEF, ææ°æå P ãç¨ããã å°æ°ç¹ã®åã«ã¯ 1
              æ¡ã® 16 é²æ°ãç½®ãããå°æ°ç¹ã®å¾ãã®æ¡æ°ã¯ 精度ã§æå®
              ãããå¤ã¨ãªããããã©ã«ãã®ç²¾åº¦ã¯ããã®å¤ã 2 é²æ°ã§æ£ç¢ºã«è¡¨ç¾ã§ãã
              å ´åã«ã¯ããã®å¤ãæ£ç¢ºã«è¡¨ç¾ã§ããæ¡æ°ã¨ãªãããã以å¤ã®å ´åã¯ã double
              åã®å¤ãåºå¥ããã®ã«ååãªå¤§ããã¨ãªãã å°æ°ç¹ã®åã®æ°åã¯ã æ‐
              £è¦åããã¦ããªãæ°ã®å ´åã¯ããã¤ã«ãªããåãããªãã æ£è¦åãããæ°ã® å ´åã¯ã 0
              以å¤ã®å¤ã«ãªãããããã¤ã«ãªããã¯åãããªãã

       c      l 修飾åããªããã°ã int å¼ãæ°ã unsigned char
              ã«å¤æãã¦ããã®çµæã«å¯¾å¿ããæåãåºåããã l 修飾åãããã°ã wint_t
              (ã¯ã¤ãæå) å¼ãæ°ãã wcrtomb(3)
              é¢æ°ãåæã·ããç¶æã§å¼ã³åºãã¦ãã«ããã¤ãæååã«å¤æãã å¤æããããã«ããã¤ãæå‐
              åãåºåããã

       s      l 修飾åããªãå ´åã å¼ãæ°ã¯ const char * åã§æååã®éåã¸ã®ãã¤ã³ã¿
              (æååã¸ã®ãã¤ã³ã¿) ã§ãããã¨ã æå¾ããã¦ãããéåä¸ã®æåã¯ãçµç«¯ã® NULL ãã¤ã
              ('\0')  ãåºã¦ããã¾ã§åºåããã (çµç«¯æåã¯åºåãããªã)ã
              精度ãæå®ããã¦ããã¨ãæå®ãããåæ°ä»¥ä¸ã¯åºåãããªãã
              精度ãæå®ãããå ´åã«ã¯ãçµç«¯ãã¤ããåå¨ããå¿è¦ã¯ãªãã
              精度ãæå®ããã¦ããªãã£ããã精度ã®å¤ãéåã®å¤§ãããã大ããå ´åã«ã¯ã
              éåã¯çµç«¯ã® NULL ãã¤ããå«ãã§ããªããã°ãªããªãã

              l 修飾åãæå®ããã¦ããå ´åã å¼ãæ°ã¯ const wchar_t * åã§ã¯ã¤ãæå‐
              ã®éåã¸ã®ãã¤ã³ã¿ã§ãããã¨ãæå¾ããã¦ããã éåä¸ã®ã¯ã¤ãæå㯠(1æåæ¯ã«
              wcrtomb(3)  ãå¼ã³åºãã¦) ãã«ããã¤ãæåã«å¤æããã (æåã®ã¯ã¤ãæå‐
              ã®å¤æã®åã« wcrtomb()  ã®ã·ããç¶æãåæç¶æã«æ»ãã¦ããå¤æã¯è¡ããã)ã
              ãã«ããã¤ãæåã¸ã®å¤æã¯ãæååãçµç«¯ãã NULL ã¯ã¤ãæåã
              åºã¦ããã¾ã§è¡ãããçµç«¯ NULL ã¯ã¤ãæåãå«ãã¦å¤æãããã çµæã®ãã«ããã¤ãæå‐
              åã¯ãçµç«¯ã® NULL ãã¤ããåºã¦ããã¾ã§ åºåããã (çµç«¯ã® NULL
              ãã¤ãã¯åºåãããªã)ã
              精度ãæå®ãããå ´åãæå®ããããã¤ãæ°ä»¥ä¸ã«ã¯åºåãããªãã ä½ãããã«ããã¤ãæå‐
              ã®ä¸é¨åã ããåºåããããã¨ã¯ãªãã
              精度ã¯ããã¤ããæ°ãæå®ãããã®ã§ããããã¯ã¤ãæåãæ°ã
              ãç»é¢ã§ã®ä½ç½®ããæå®ãããã®ã§ã¯ãªããã¨ã«æ³¨æã
              精度ãæå®ããã¦ãã¦ãããã«åºåãéåã®æ«å°¾ã«éããåã«åºåãã¤ãæ°ã
              精度ã®å¤ãè¶ããå ´åã ãã¯ãéå㯠NULL ã¯ã¤ãæåã§çµç«¯ããã¦ããªãã¦ãããã
              ãã以å¤ã®å ´åã¯ãå¿ãéå㯠NULL ã¯ã¤ãæåã§çµç«¯ããã¦ããªããã°ãªããªãã

       C      (C99 ã«ã¯ãªãã SUSv2 ã«ã¯ãã)  lc ã¨åãã使ã£ã¦ã¯ãªããªãã

       S      (C99 ã«ã¯ãªãã SUSv2 ã«ã¯ãã)  ls ã¨åãã使ã£ã¦ã¯ãªããªãã

       p      void * ãã¤ã³ã¿å¼ãæ°ã (%#x ã %#lx ã®ãããª) 16 é²æ°ã§åºåããã

       n      ããã¾ã§ã«åºåãããæåæ°ã int * (ã¾ãã¯é¡ä¼¼ã®å)
              ã®ãã¤ã³ã¿å¼ãæ°ãæãæ´æ°ã«ä¿åããã å¼ãæ°ã®å¤æã¯è¡ãããªãã

       m      (glibc ã§ã®æ¡å¼µ)  strerror(errno)
              ã®åºåã表示ãããå¼ãæ°ã¯å¿è¦ãªãã

       %      '%' æåãåºåãããå¤æãããå¼ãæ°ã¯ç¡ãã å¤ææå®å¨ä½ãæ¸ã㨠"%%" ã¨ãªãã

æºæ
       fprintf(), printf(), sprintf(), vprintf(), vfprintf(), vsprintf()
       é¢æ°ã¯ãC89 㨠C99 ã«æºæ ãã¦ããã snprintf()  㨠vsnprintf()  㯠C99
       ã«æºæ ãã¦ããã

       snprintf()  ã®è¿ãå¤ãè¦ãã¨ã SUSv2 㨠C99 æ¨æºã¯äºãã«çç¾ãã¦ããã SUSv2
       ã§ã¯ã snprintf() ã size=0 ã§å¼ã³åºãããå ´åã 1
       æªæºã®å¤ãä½ãè¿ãå¤ã¨ããããã«è¦å®ãã¦ããã ä¸æ¹ C99 ã§ã¯ããã®ãããªå ´å str ã
       NULL ã¨ããè¿ãå¤ã¨ã㦠(é常éã) åºåãããã¡ãååãªå¤§ããã
       ãã£ãå ´åã«åºåãããã§ãããæåæ°ãè¿ãã

       Linux libc4 ã§ã¯ã 5 ã¤ã® C æ¨æºã®ãã©ã°ã é·ã修飾å h, l, Lãå¤æ c, d, e,
       E, f, F, g, G, i, n, o, p, s, u, x, X ã使ããã ä½ã F 㯠f ã¨å義ã§ããã
       ã¾ãã D, O, and U ã ld, lo, and lu ã¨åããã®ã¨ãã¦ä½¿ãã (ããã¯ã¾ããä»æ§ã§ã
       å¾ã« %D ã®å¯¾å¿ãæã¡åãããæã«æ·±å»ãªãã°ã å¼ãèµ·ããã)ããã±ã¼ã«ä¾å‐
       ã®å°æ°ç¹ã1000 åºåãã NaN ã¨ç¡éã "%m$" 㨠"*m$" ã¯ä½¿ããªãã

       Linux libc5 ã§ã¯ã 5 ã¤ã® C æ¨æºã®ãã©ã°ã¨ ' ãã©ã°ããã±ã¼ã«ã "%m$" ã¨
       "*m$" ã使ããã ã¾ããé·ã修飾å h, l, L, Z, iand q ã使ãããã L 㨠q
       ã¯ä¸¡æ¹ã¨ã long double 㨠long long int ã«å¯¾å¿ãã¦ãã (ããã¯ãã°ã§ãã)ã
       ç¾å¨ã§ã¯å¤æ F, D, O, U ã¯èªèãããªãããå¤ææå m ã追å ããããããã¯
       strerror(errno) ãåºåãããã®ã§ããã

       glibc 2.0 ã§ã¯ãå¤ææå C 㨠S ã追å ãããã

       glibc 2.1 ã§ã¯ãé·ã修飾å hh, j, t, z ã¨å¤ææå a, A ã追å ãããã

       glibc 2.2 ã§ã¯ã C99 ã§è¦å®ãããæå³ã§ã®å¤ææå F 㨠ãã©ã°æå I ã追å ãããã

注æ
       ããã¹ãã buf ã«è¿½å ããã®ã«ã軽çã«ã次ã®ãããªã³ã¼ãã使ã£ã¦ããããã°ã©ã ãããã

           sprintf(buf, "%s some further text", buf);

       ããããªãããæ¨æºè¦æ ¼ã§ã¯ã sprintf(), snprintf(), vsprintf(), vsnprintf()
       ã®å¼ã³åºãã«ããã¦ãã³ãã¼åã¨ã³ãã¼åã®ãããã¡ãéãªã£ã¦ããå ´åã®
       çµæã¯ä¸å®ã§ãããã¨æè¨ããã¦ããã 使ç¨ãã gcc(1)
       ã®ãã¼ã¸ã§ã³ãæå®ããã³ã³ãã¤ã©ã®ãªãã·ã§ã³æ¬¡ç¬¬ã§ã¯ã
       ä¸è¨ã®ãããªå¼ã³åºãã§ãæå¾ããçµæãå¾ããããªãããã¨ãããã

       glibc ã® snprintf()  㨠vsnprintf()  ã®å®è£ã¯ããã¼ã¸ã§ã³ 2.1 以é㯠C99
       æ¨æºã«æºæ ãã¦ããã ä¸è¨ã®éãã®åä½ãããã glibc 2.0.6
       ã¾ã§ã¯ãåºåãåãè©°ããããå ´å㯠-1 ãè¿ãã

ãã°
       sprintf()  㨠vsprintf()  ã¯åæã«ååã«é·ãæå‐
       åé åãããã¨ä»®å®ããã®ã§ãå¼ã³åºãå´ã¯
       å®éã®é åããããµããªãããã«æ³¨æããªããã°ãªããªãã
       ãããããããä¿è¨¼ãããã¨ãä¸å¯è½ãªå ´åãå¤ãã çæãããæååã®é·ãã¯ãã±ã¼ã«ä¾å‐
       ã§ãããäºæ¸¬ãé£ãããã¨ã«æ³¨æã 代ããã« snprintf()  㨠vsnprintf() (ã¾ãã¯
       asprintf(3)  㨠vasprintf(3))  ã使ããã¨ã

       Linux libc4.[45] ã«ã¯ snprintf()  ã¯ãªããã libbsd ãæä¾ããã¦ããã ãã®ä¸ã«ã¯
       sprintf() ã¨ç価㪠(ã¤ã¾ã size å¼ãæ°ãç¡è¦ãã)  snprintf()  ãããã
       ãããã£ã¦ãåæã® libc4 㧠snprintf()  ã使ãã¨ãæ·±å»ãªã»ã‐
       ã¥ãªãã£åé¡ãå¼ãèµ·ãããã¨ãããã

       printf(foo); ã®ãããªã³ã¼ãã¯ãã°ãã°ãã°ãå¼ãèµ·ããã ãªããªã foo ã« % æå‐
       ãå«ã¾ãã¦ããããããªãããã§ããã foo ãä¿¡é ¼ã§ããªãã¦ã¼ã¶ã¼å¥åããä½ããã¦ããå ´åã«ã¯ã
       ãã®ä¸ã« %n ãå«ã¾ãã¦ãããã¨ãããã printf()
       å¼ã³åºãæã«ã¡ã¢ãªã¸ã®æ¸ãè¾¼ã¿ãèµ·ããã ã»ã‐
       ã¥ãªãã£ã¼ãã¼ã«ãä½ããã¨ã«ãªããããããªãã

ä¾
       Pi ã 5 æ¡ã§åºåããã

           #include <math.h>
           #include <stdio.h>
           fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));

       æ¥ä»ã¨æéã "Sunday, July 3, 10:02" ã®å½¢å¼ã§åºåããã (weekday 㨠month
       ã¯æååã¸ã®ãã¤ã³ã¿ã§ãã)

           #include <stdio.h>
           fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
                weekday, month, day, hour, min);

       æ¥ - æ - å¹´ ã®é åºã§è¡¨ç¤ºãè¡ãå½ãå¤ãã
       å¾ã£ã¦ãå½éçã§ã¯æ¸å¼ã§æå®ãããé çªã§ å¼ãæ°ã表示ã§ããªããã°ãªããªãã

           #include <stdio.h>
           fprintf(stdout, format,
                weekday, month, day, hour, min);

       format ã¯ãã±ã¼ã«ã«ä¾åãã¦ãããå¼ãæ°ã®é çªãå¤ãããã¨ãã§ããã format ã

           "%1$s, %3$d. %2$s, %4$d:%5$.2d\n"

       ã§ããã°ã "Sonntag, 3. Juli, 10:02" ã¨ããçµæã«ãªãã

       ååã«å¤§ããªæååé åã確ä¿ãã¦ãããã«ã¡ãã»ã¼ã¸ãæ ¼ç´ããã«ã¯ (glibc 2.0 㨠glibc
       2.1 ã®ä¸¡æ¹ã§æ£ããåä½ããã³ã¼ã):

       If truncation occurs in glibc versions prior to 2.0.6, this is treated
       as an error instead of being handled gracefully.

       #include <stdio.h>
       #include <stdlib.h>
       #include <stdarg.h>

       char *
       make_message(const char *fmt, ...)
       {
           int n;
           int size = 100;     /* Guess we need no more than 100 bytes */
           char *p, *np;
           va_list ap;

           if ((p = malloc(size)) == NULL)
               return NULL;

           while (1) {

               /* Try to print in the allocated space */

               va_start(ap, fmt);
               n = vsnprintf(p, size, fmt, ap);
               va_end(ap);

               /* Check error code */

               if (n < 0)
                   return NULL;

               /* If that worked, return the string */

               if (n < size)
                   return p;

               /* Else try again with more space */

               size = n + 1;       /* Precisely what is needed */


               if ((np = realloc (p, size)) == NULL) {
                   free(p);
                   return NULL;
               } else {
                   p = np;
               }
           }
       }

é¢é£é ç®
       printf(1), asprintf(3), dprintf(3), scanf(3), setlocale(3), wcrtomb(3),
       wprintf(3), locale(5)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



GNU                               2013-03-05                         PRINTF(3)