vprintf

å称
     printf, fprintf, sprintf, snprintf, asprintf, vprintf, vfprintf,
     vsprintf, vsnprintf, vasprintf — æ¸å¼ã«å¤æãã¦åºå

ã©ã¤ãã©ãª
     Standard C Library (libc, -lc)

æ¸å¼
     <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, ...)  int
     asprintf(char **ret, const char *format, ...)  <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) int vasprintf(char **ret, const char *format, va_list ap)

解説
     printf() ãã¡ããªã®é¢æ°ã¯ã以ä¸ã§èª¬æãã format ã«å¾ã£ã¦åºåãè¡ãã¾ãã printf()
     㨠vprintf() ã¯ãæ¨æºåºå stdout ã«åºåãè¡ãã¾ãã fprintf() 㨠vfprintf()
     ã¯ãæå®ãããåºå stream ã«åºåãè¡ãã¾ãã sprintf(), snprintf(), vsprintf(),
     vsnprintf() ã¯ããã£ã©ã¯ã¿æåå str ã«åºåãè¡ãã¾ãã asprintf() 㨠vasprintf()
     ã¯ã malloc(3) ã§æ°ããæååãåçã«å²ãå½ã¦ã¾ãã

     ãããã®é¢æ°ã¯ã format æååã«ããå¶å¾¡ã«å¾ã£ã¦åºåãè¡ãã¾ãã ãã®æååã¯ããã®å¾ã®å¼æ°
     (ã¾ã㯠stdarg(3) ã®å¯å¤é·å¼æ°æ©è½ã§ã¢ã¯ã»ã¹ã§ããå¼æ°)
     ãåºåç¨ã«å¤æããæ¹æ³ãæå®ãã¾ãã

     ãã®é¢æ°ã¯ãåºåãããæåæ° (æååã¸ã®åºåãçµäºãããæå¾ã® ‘\0’ ã¯å«ã¾ãªã) ãè¿ãã¾ãã
     snprintf() 㨠vsnprintf() ã®å ´åã®ã¿ã size ã®å¶éãç¡ãã£ãã¨ããã
     åºåãããã§ãããæåæ° (åæ§ã«æååã®æå¾ã® ‘\0’ ã¯å«ã¾ãªã) ãè¿ãã¾ãã

     asprintf() 㨠vasprintf() ã¯ãæ´å½¢ãããæå‐
     åãæ ¼ç´ããã®ã«ååãªå¤§ããã®ãããã¡ãæããã¤ã³ã¿ã *ret ã«è¨å®ãã¾ãã
     å²ãå½ã¦ãããé åãä¸è¦ã«ãªã£ãå ´åã¯ããã®ãã¤ã³ã¿ã free(3)
     ã«æ¸¡ãã¦è§£æ¾ãã¦ãã ããã ååãªé åãå²ãå½ã¦ãããªãå ´åã asprintf() ã¨
     vasprintf() 㯠-1 ãæ»ãã ret ã NULL ãã¤ã³ã¿ã«è¨å®ãã¾ãã

     snprintf() 㨠vsnprintf() ã¯ãæ大㧠size-1 æåã ãåºåæååã«æ¸ãè¾¼ã¿ã¾ã (
     size çªç®ã®æåã¯çµç«¯ã® ‘\0’ ã«ãªãã¾ã)ã æ»ãå¤ã size
     å¼æ°ä»¥ä¸ã§ããå ´åã¯ãæååãæ ¼ç´ããã«ã¯çããããããã åºåãããæå‐
     ã®ä¸é¨ãç ´æ£ããããã¨ã«ãªãã¾ãã

     sprintf() 㨠vsprintf() ã¯ã size ãç¡éã§ããã¨ä»®å®ãã¾ãã

     æ´å½¢æååã¯ã0 以ä¸ã®å½ä»¤ããæ§æããã¦ãã¾ãã
     ãã®å½ä»¤ã«ã¯ãåºåã¹ããªã¼ã ã«å¤æ´ãããã«ã³ãã¼ããã é常æå (% 以å¤)ãããã³ 0
     以ä¸ã®å¾ç¶ã®å¼æ°ãåãåºãå¤ææå®ãããã¾ãã ããããã®å¤ææå®ã¯ã % æå‐
     ããå§ã¾ãã¾ããå¼æ°ã¯ã (åæ¡å¼µã®å¾ã«) å¤ææ示åã«é©åã«å¯¾å¿ããå¿è¦ãããã¾ãã %
     ã®å¾ã«ã¯ã以ä¸ãé çªã«ç¾ãã¾ãã

     å¾ã« $ ãç¶ã 10 é²æ°æååããæ§æããã
         次ã«ã¢ã¯ã»ã¹ããå¼æ°ãæå®ããä»»æã®ãã£ã¼ã«ãã
         ãã®ãã£ã¼ã«ããæå®ããªãã¨ãæå¾ã«ã¢ã¯ã»ã¹ããã
         å¼æ°ã«ç¶ãå¼æ°ã使ç¨ããã¾ããå¼æ°ã«ã¯ 1 ããå§ã¾ãçªå·ãä»ãã¾ãã æ¸å¼æå®æååã§ã
         ã¢ã¯ã»ã¹ã§ããªãå¼æ°ãã¢ã¯ã»ã¹ã§ããå¼æ°ã«ç¹å¨ããå ´åã çµæã¯ä¸å®ã«ãªãã¾ãã

     0 å以ä¸ã®ä»¥ä¸ã®ãã©ã°

         -   å¤ã “代æ¿å½¢å¼” ã«å¤æãããã¨ãæå®ãã # æåã c, d, i, n, p, s, ããã³ u
             å¤æã®å ´åããã®ãªãã·ã§ã³ã¯å¹æãçºæ®ãã¾ããã o
             å¤æã®å ´åã¯ãæ°å¤ã®ç²¾åº¦ãä¸ããã åºåæååã®æåã®æåã 0 ã«ãªãã¾ã
             (æ確ãªç²¾åº¦ã® 0 㧠0 ãåºåãããå ´åãé¤ã)ã x å¤æ㨠X å¤æã®å ´åã¯ã0
             以å¤ã®çµæã®åã«æåå ‘0x’ (X å¤æã®å ´å㯠‘0X’) ãä»ãã¾ãã e, E, f, g,
             ããã³ G å¤æã®å ´åã¯ãå°æ°ç¹ä»¥ä¸ããªãã¦ãå°æ°ç¹ãçµæã«å¸¸ã«å«ã¾ãã¾ã
             (é常ã®å ´åãå°æ°ç¹ä»¥ä¸ãããå ´åã«ãããã å¤æçµæã«å°æ°ç¹ãä»ãã¾ã)ã g
             ããã³ G å¤æã®å ´åã¯ãå¾ç¶ã® 0 ãé常ã®å ´åã®ããã«çµæããåé¤ããã¾ããã

         -   0 (ã¼ã) æåã®ããã£ã³ã°ãæå®ããã n å¤æãé¤ããã¹ã¦ã®å¤æã§ã¯ã
             å¤æå¤ã®å·¦ã«ç©ºç½ã§ã¯ãªã 0 ãä»ãã¾ããæ°å¤å¤æ (d, i, o, u, i, x,
             ããã³ X) ã§ç²¾åº¦ãæå®ããã¦ããå ´åã 0 ãã©ã°ã¯ç¡è¦ããã¾ãã

         -   è² ã®ãã£ã¼ã«ãå¹ã示ã -
             ãã©ã°ã¯å¤æãããå¤ããã£ã¼ã«ãå¢çã®å·¦ã§æããããäºã示ãã¾ãã n
             å¤æ以å¤ã§ã¯ãå¤æå¤ã®å·¦ã«ç©ºç½ã 0 ãä»ãã®ã§ã¯ãªãã
             å¤æå¤ã®å³ã«ç©ºç½ãä»ãã¾ãã - 㨠0 ã両æ¹ã¨ãæå®ããå ´å㯠0
             ãç¡å¹ã«ãªãã¾ãã

         -   空ç½ã符å·ä»ãå¤æ (d, e, E, f, g, G, ããã³ i) ã§ä½æãããæ‐
             £ã®æ°å¤ã®åã«ç©ºç½ãæ®ãã¾ãã

         -   + æåã符å·ä»ãå¤æã§ä½æãããæ°å¤ã®åã«å¸¸ã«ç¬¦å·ãä»ãã¾ãã +
             ã¨ç©ºç½ã両æ¹ã¨ãæå®ããå ´åã¯ç©ºç½ãç¡å¹ã«ãªãã¾ãã

     ä»»æã® 10 é²æ°æååãæä½ãã£ã¼ã«ãå¹ãæå®ãã¾ãã å¤æå¤ã®æå‐
         æ°ããã£ã¼ã«ãã®å¹ããå°ãªãå ´åã¯ãå·¦ã«ç©ºç½ãä»ãã¦
         (å·¦æããã©ã°ãæå®ããå ´åã¯å³ã«ç©ºç½ãä»ãã¦) ãã£ã¼ã«ãã®å¹ã«åããããã¾ãã

     ããªãªã . ã®æ¬¡ã«ä»»æã®æ°åæååãç¶ãå½¢å¼ã®ç²¾åº¦ã æ°åæå‐
         åãçç¥ããå ´åã精度㯠0 ã«ãªãã¾ãã d, i, o, u, x, ããã³ X
         å¤æã§ã¯ããã®ç²¾åº¦ã®æä½æ¡æ°ãåºåããã¾ãã e, E, ããã³ f
         å¤æã§ã¯ãå°æ°ç¹ä»¥ä¸ã«ãã®ç²¾åº¦ã®æ¡æ°ãåºåããã¾ãã g ããã³ G
         å¤æã§ã¯ããã®ç²¾åº¦ã®æ大æå¹æ¡æ°ãåºåããã¾ãã s å¤æã§ã¯ããã®ç²¾åº¦ã®æ大æå‐
         æ°ãæååããåºåããã¾ãã

     ãªãã·ã§ã³æå h ãå¾ã® d, i, o, u, x, ããã³ X å¤æã short int å¼æ°ã
         unsigned short int å¼æ°ã«å¯¾å¿ãããã¨ãã¾ãã¯å¾ã® n å¤æã short int
         å¼æ°ã®ãã¤ã³ã¿ã«å¯¾å¿ãããã¨ãæå®ãã¾ãã

     ãªãã·ã§ã³æå l (å°æåã® L)ãå¾ã® d, i, o, u, x, ããã³ X å¤æã long int
         å¼æ°ã unsigned long int å¼æ°ã®ãã¤ã³ã¿ã«é©ç¨ããããã¨ãã¾ãã¯å¾ã® n å¤æã
         long int å¼æ°ã®ãã¤ã³ã¿ã«å¯¾å¿ãããã¨ãæå®ãã¾ãã

     ãªãã·ã§ã³æå ll (å°æåã® L ã 2 ã¤)ãå¾ã® d, i, o, u, x, ããã³ X å¤æã long
         long int å¼æ°ã unsigned long long int å¼æ°ã«å¯¾å¿ãããã¨ãã¾ãã¯å¾ã® n
         å¤æã long long int å¼æ°ã®ãã¤ã³ã¿ã«å¯¾å¿ãããã¨ãæå®ãã¾ãã

     ãªãã·ã§ã³æå q, ãå¾ã® d, i, o, u, x, ããã³ X å¤æã quad int å¼æ°ã
         unsigned quad int å¼æ°ã«å¯¾å¿ãããã¨ãã¾ãã¯å¾ã® n å¤æã quad int
         å¼æ°ã®ãã¤ã³ã¿ã«å¯¾å¿ãããã¨ãæå®ãã¾ãã

     ãªãã·ã§ã³æå L ãå¾ã® e, E, f, g, ããã³ G å¤æã long double
         å¼æ°ã«å¯¾å¿ãããã¨ãæå®ãã¾ãã

     é©ç¨ããå¤æã®åãæå®ããæåã

     ãã£ã¼ã«ãå¹ã精度ãã¾ãã¯ãã®ä¸¡æ¹ã¯ãã¢ã¹ã¿ãªã¹ã¯ ‘*’ ãã¾ãã¯æ°åæååã®ä»£ããã«
     1 ã¤ä»¥ä¸ã® 10 é²æ°ã¨ ‘$’ ãç¶ãã¢ã¹ã¿ãªã¹ã¯ã§æå®ã§ãã¾ãããã®å ´åã int
     å¼æ°ã¯ãã£ã¼ã«ãå¹ã精度ãæä¾ãã¾ãã è² ã®ãã£ã¼ã«ãå¹ã¯ã æ‐
     £ã®ãã£ã¼ã«ãå¹ãç¶ãå·¦æããã©ã°ã¨ãã¦æ±ããã¾ãã
     è² ã®ç²¾åº¦ã¯ãæ¬ è½ãã¦ãããã®ã¨ãã¦æ±ããã¾ãã 1 ã¤ã®æ¸å¼å½ä»¤ã«ä½ç½®å¼æ° (nn$)
     ã¨ä½ç½®ä»¥å¤ã®å¼æ°ãæ··å¨ãã¦ããå ´åã çµæã¯æªå®ç¾©ã«ãªãã¾ãã

     å¤ææ示åã¨ãã®æå³ã¯æ¬¡ã®ã¨ããã§ãã

     diouxX  int å¼æ° (ã¾ãã¯é©åãªå¯å¤å¼æ°) ãã符å·ä»ã 10 é² (d 㨠i) ã符å·ãªã
             8 é² (o) ã符å·ãªã 10 é² (u) ã符å·ãªã 16 é² (x 㨠X) ã«å¤æããã¾ãã
             x å¤æã«ã¯æå abcdef ã X å¤æã«ã¯æå ABCDEF ã使ç¨ããã¾ãã
             精度ã¯ãåºåããæä½æ¡æ°ãæå®ãã¾ãã å¤æå¤ã§å°ãªãæ¡ããå¿è¦ãªãå ´åã¯ã
             左㫠0 ãä»ãã¾ãã

     DOU     long int å¼æ°ãã符å·ä»ã 10 é²ã符å·ãªã 8 é²ã符å·ãªã 10 é²ã«ã
             ããããã®å½¢å¼ã ld, lo, ããã³ lu ã§ãããã®ããã«å¤æããã¾ãã ãã®å¤ææå‐
             ã«ã¯åé¡ãããã®ã§ãæçµçã«ã¯åºåããã¾ããã

     eE      double å¼æ°ã丸ãããã [-]d.ddde±dd ã®ã¹ã¿ã¤ã«ã«å¤æããã¾ãã
             å°æ°ç¹ä»¥ä¸ã¯ 1 æ¡ã§ãå°æ°ç¹ä»¥ä¸ã®æ¡æ°ã¯ç²¾åº¦ã¨çãããªãã¾ãã
             精度ãæå®ããã¦ããªãå ´å㯠6 ãä»®å®ããã¾ãã 精度ã 0
             ã§ããå ´åãå°æ°ç¹ã¯åºåããã¾ããã E å¤æã§ã¯ãæå E (e ã§ã¯ãªã)
             ã使ç¨ããã¦ææ°ãå°å¥ããã¾ãã ææ°ã«ã¯ãæä½ 2 æ¡ã常ã«å«ã¾ãã¾ãã å¤ã 0
             ã§ããå ´åãææ°ã¯ 00 ã«ãªãã¾ãã

     f       double å¼æ°ã丸ãããã [-]ddd.ddd, ã®ã¹ã¿ã¤ã«ã§ 10 é²ã«å¤æããã¾ãã
             å°æ°ç¹ä»¥ä¸ã®æ¡æ°ã¯ã精度æå®ã«çãããªãã¾ãã
             精度ãæå®ããã¦ããªãå ´å㯠6 ãä»®å®ããã¾ãã 精度ã 0
             ã§ããå ´åãå°æ°ç¹ã¯åºåããã¾ããã å°æ°ç¹ãåºåãããå ´åã¯ãå°æ°ç¹ä»¥ä¸ã«æä½
             1 æ¡ãåºåããã¾ãã

     gG      double å¼æ°ããã¹ã¿ã¤ã« f ã e (G å¤æã®å ´å㯠E)
             ã§å¤æããã¾ãã精度ã¯æå¹æ¡æ°ãæå®ãã¾ãã 精度ãæå®ããã¦ããªãå ´å㯠6
             ãä»®å®ããã¾ãã 精度ã 0 ã§ããå ´å㯠1 ã¨ãã¦æ±ããã¾ãã å¤æå¾ã®ææ°ã -4
             ããå°ããã精度以ä¸ã§ããå ´åã¯ãã¹ã¿ã¤ã« e ã使ç¨ããã¾ãã å¾ç¶ã® 0
             ã¯ãçµæã®å°æ°é¨ããåé¤ããã¾ãã å°æ°ç¹ã¯ãå°æ°ç¹ä»¥ä¸ã«æä½ã§ã 1
             æ¡ããå ´åã«åºåããã¾ãã

     c       int å¼æ°ã unsigned char ã«å¤æãããå¤æãããæåãåºåããã¾ãã

     s       char * å¼æ°ããæååã®éåãæããã¤ã³ã¿ (æååã¸ã®ãã¤ã³ã¿) ã¨ã¿ãªããã¾ãã
             éåã®æåã¯ãæå¾ã®ãã«æåã¾ã§åºåããã¾ã ( NULL æåã¯åºåããã¾ãã)ã
             精度ãæå®ããã¦ããå ´åãæå®ãããæ°ä»¥ä¸ã¯åºåãããªãã®ã§ã NULL æå‐
             ã¯å¿è¦ããã¾ããã 精度ãæå®ããã¦ããªãå ´åã
             ã¾ãã¯ç²¾åº¦ãéåã®ãµã¤ãºä»¥ä¸ã§ããå ´åã éåã®æå¾ã«ã¯ãã«æåãå¿è¦ã§ãã

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

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

     %       ‘%’ ãåºåããã¾ããå¤æãããå¼æ°ã¯ããã¾ããã å®å¨ãªå¤ææå®ã¯ ‘%%’ ã§ãã

     ãã£ã¼ã«ãå¹ãåå¨ããªãå ´åãã¾ãã¯ãã£ã¼ã«ãå¹ãå°ããå ´åã§ãã
     ãã£ã¼ã«ãã¯åãæ¨ã¦ããã¾ãããå¤æçµæããã£ã¼ã«ãå¹ãã大ããå ´åã
     ãã£ã¼ã«ãã¯å¤æçµæãå容ã§ããããã«ãªãã¾ã§æ¡å¼µããã¾ãã

ä¾
     weekday 㨠month ãæååã¸ã®ãã¤ã³ã¿ã§ããå ´åã« “Sunday, July 3, 10:02”
     ã¨ããå½¢å¼ã§æ¥ä»ã¨æå»ãåºåããå ´å:

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

     π ãå°æ°ç¬¬ 5 ä½ã¾ã§åºåããå ´å:

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

     128 ãã¤ãã®æååãå²ãæ¯ããããã«åºåããå ´å:

           #include <stdio.h>
           #include <stdlib.h>
           #include <stdarg.h>
           char *newfmt(const char *fmt, ...)
           {
                           char *p;
                           va_list ap;
                           if ((p = malloc(128)) == NULL)
                                   return (NULL);
                           va_start(ap, fmt);
                           (void) vsnprintf(p, 128, fmt, ap);
                           va_end(ap);
                           return (p);
           }

é¢é£é ç®
     printf(1), scanf(3)

è¦æ ¼
     fprintf(), printf(), sprintf(), vprintf(), vfprintf(), ããã³ vsprintf()
     é¢æ°ã¯ã ISO/IEC 9899:1990 (“ISO C90”) ã«é©åãã¦ãã¾ãã

æ´å²
     asprintf() é¢æ°ã¨ vasprintf() é¢æ°ã¯ã GNU C ã©ã¤ãã©ãªã«è¿½å ããã¾ãããããã¯ã
     FreeBSD 2.2 㧠Peter Wemm <peter@FreeBSD.org> ã«ãã£ã¦å®è£ããã¾ãããã
     OpenBSD 2.3 ã§ã¯å¾ã« Todd C. Miller <Todd.Miller@courtesan.com>
     ã®ã·ã¹ãã ã§ç½®ãæãããã¾ããã

ãã°
     å¤æå½¢å¼ %D, %O, ããã³ ã¯æ¨æºçã§ã¯ãªããä¸ä½äºææ§ãä¿ã¤ããã«æä¾ããã¦ãã¾ãã
     å½¢å¼ã« ( 0 ãã©ã°ã精度ãæå®ãããã¨ã§) 0 ãããã£ã³ã°ãããã¨ã %n å¤æ㨠%p å¤æã§
     # ãã©ã°ãæå®ãããã¨ã %Ld ã®ãããªç¡æå³ãªçµã¿åããã¯æ¨æºçã§ããã¾ããã
     ãã®ãããªçµã¿åããã¯é¿ãã¦ãã ããã

     sprintf() 㨠vsprintf() ã§ã¯ç¡éã«é·ãæååãä»®å®ãããã®ã§ã
     å¼ã³åºãå´ã§ã¯å®éã®ç©ºéã ãªã¼ãããã¼ããªãããã«æ³¨æããå¿è¦ãããã¾ãã ãªã¼ããã‐
     ã¼ããªããã¨ãä¿è¨¼ãããã¨ã¯å°é£ã§ãã å®å¨ã®ããã代ããã« snprintf()
     ã¤ã³ã¿ãã§ã¼ã¹ã使ç¨ãã¦ãã ããã æ®å¿µãªããããã®ã¤ã³ã¿ãã§ã¼ã¹ã¯ç§»æ¤ã§ãã¾ããã