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, ...);
       int asprintf(char **strp, const char *format, ...);
       int dprintf(int d, 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 vsnprintfvsnprintf(char *str, size_t size, const char *format,
       va_list ap);
       int vasprintf(char **strp, const char *format, va_list ap);
       int vdprintf(int d, const char *format, va_list ap);

説æ
       printf é¢æ°ã°ã«ã¼ãã¯ã以ä¸ã§è¿°ã¹ãããã«ã format ã«å¾ã£ã¦åºåãçæãããã®ã§ããã
       printf 㨠vprintf ã¯åºåã stdout ï¼æ¨æºåºåã¹ããªã¼ã )ã«æ¸ãåºãã fprintf ã¨
       vfprintf ã¯ä¸ããããåºåã stream ã«æ¸ãåºãã sprintf, snprintf, vsprintf ããã¦
       vsnprintf ã¯æåå str ã«æ¸ãè¾¼ãã asprintf 㨠vasprintf
       ã¯ãåçã«åå¾ããããããã¡ã«æ¸ãè¾¼ã¿ããã®ã¢ãã¬ã¹ã¯ *strp ã«è¿ãããã dprintf ã¨
       vdprintf ã¯ä¸ãããããã¡ã¤ã«è¨è¿°åã«æ¸ãè¾¼ãã

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

       ä¸è¨ã®é¢æ°ã¯æ¸ãè¾¼ã¾ããæåã®åæ°ãè¿ã ï¼æå‐
       åã®æå¾ã示ãããã«ä½¿ç¨ãã`\0'ã¯å«ã¾ããªãï¼ã snprintf 㨠vsnprintf ã¯ãï¼æå‐
       åã®çµç«¯ã«ãã'\0'ãå«ãã¦ï¼ size ãã¤ããè¶ããæåæ°ãæ¸ãè¾¼ã¾ãªãã
       ããã¦ããã®å¶éã«ãã£ã¦åºåãä¸æãããå ´åã«ã¯-1ãè¿ãã

       ãã©ã¼ãããç¨ã®æååã¯ã¼ãå以ä¸ã®å½ä»¤(directives)ã«ãã£ã¦
       æ§æããã¦ãããå½ä»¤ã«ã¯ã åºåã¹ããªã¼ã ã«ãã®ã¾ã¾ã³ãã¼ããã( % 以å¤ã®)é常ã®æå‐
       ã¨å¤ææå(conversion specifications)ãããã å¤ææåã¯ããããããã¼ãå以ä¸ã®å¼æ°ãåãã
       å¤ææåã¯æå % ã§å§ã¾ãã (åã®æ ¼ä¸ãã®å¾ã§ã¯)å¼æ°ã¯å¤ææåã表ãåã¨æ‐
       £ç¢ºã«å¯¾å¿ããªããã°ãªããªãã % ã®å¾ã«ç¶ãã¦ã次ã®ãããªæåãç¾ããï¼

       ·      以ä¸ã«ç¤ºãã¼ãå以ä¸ã®ãã©ã°( flags):

              #      ã¯ãæ°å¤ã``å¥ã®å½¢å¼''ã«å¤æããããã¨ãæå®ããã c, d, i, n, p, s,
                     u ã§ç¤ºãããå¤æã«ã¤ãã¦ã¯ããã®ãªãã·ã§ã³ã¯æå³ãæããªãã o
                     å¤æã®å ´åãåºåæååã®æåã«å¼·å¶çã«0ãä»å ããããã« æ°å‐
                     ã®ç²¾åº¦ãå¢ãããã(æ示çã«ç²¾åº¦ãã¼ã‐
                     ã«æå®ãã¦0ãåºåãããå ´åãé¤ã)ã x 㨠X
                     å¤æã®å ´åãæ°å¤ã0ã§ãªãã¨ãã«ã¯æåå `0x' ( X å¤æã®å ´åã«ã¯
                     `0X' ) ãåã«æ·»ä»ãããã e, E, f, g, G å¤æã§ã¯ã
                     å°æ°ç¹ã«ç¶ãæ°åããªãã¦ãã çµæã¯å¸¸ã«å°æ°ç¹ãå«ãã§ããã
                     (é常ã¯ãå°æ°ç¹ã®å¾ã«æ°åãç¶ãå ´åã«éã£ã¦ãçµæã®ãªãã«
                     å°æ°ç¹ã表ãã)ã g 㨠G
                     å¤æã®å ´åãä»ã®å¤æã¨ã¯ç°ãªããå¾ã«ç¶ãã¼ããçµæããåãåãããªãã

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

              -      ï¼è² ã®ãã£ã¼ã«ãé·ã表ããã©ã°ï¼å¤æå¤ããã£ã¼ã«ãå¢çã§å·¦æãã«ãªã
                     ãã¨ãæ示ããã n å¤æãé¤ãã¦ãå¤æãããå¤ã¯ 空ç½æåãã¼ã‐
                     ã§å·¦å´ã§ã¯ãªãå³å´ãåããããã - 㨠0
                     ã®ä¸¡æ¹ãä¸ãããã¦ããå ´åã«ã¯ã - ãåªåãããã

              ´ ´    ï¼ï¼åã®åè§ã¹ãã¼ã¹ï¼ 符å·ä»ãå¤æ (d, e, E, f, g, G, i)
                     ã«ãã£ã¦çæãããæ£ã®æ°åã®åã«ç©ºç½ãç½®ããããã¨ã æå®ããã

              +      符å·ä»ãå¤æã«ãã£ã¦åºåãããæ°åã®ã¾ãã«ã常ã«ç¬¦å·ãç½®ããããã¨ã
                     æå®ããã + ã¨åè§ã¹ãã¼ã¹ã® 両æ¹ã使ããã¦ããå ´åã«ã¯ã +
                     ãåªåãããã

              '      ãã±ã¼ã«ã®æå ±ãä½ãã示ãã¦ããå ´åã
                     å¼æ°ãæ°å¤ã§ããã¨ãã«ã¯åºåãã°ã«ã¼ãåãããã¨ãæå®ããã
                     å¤ãã®ãã¼ã¸ã§ã³ã® gcc ãããã®ãªãã·ã§ã³ãæ§æ解æã§ããã«ã è‐
                     ¦å(ã¯ã¼ãã³ã°)ãçºãããã¨ã«æ³¨æãããã¨ã

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

       ·      ãªãã·ã§ã³ã§æå®ãã精度ã ããã¯ãããªãªã (`.')
              ã¨ããã«ç¶ããªãã·ã§ã³ã®10é²æ°ã¨ããå½¢ã§ç¤ºãããã æ°å‐
              ãçç¥ããå ´åã精度ã¯ã¼ãã¨ã¿ãªãããã 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 (ã¨ã«)ã¯ãå¼ãç¶ã d, i, o, u, x, X å¤æã long int
              ã¾ã㯠unsigned long int
              å¼æ°ã¸ã®ãã¤ã³ã¿ã«é©ç¨ããããã¨ãæå®ããããããã¯ãå¼ãç¶ã n å¤æã long int
              å¼æ°ã¸å¯¾å¿ãã¦ãããã¨ãæå®ããã Linux ã¯ããµãã¤ã® l ãã©ã°ã q ã¾ã㯠L
              ã¨å義ã«ä½¿ç¨ã§ããã¨ãã ANSI ã«ã¯ãªãæ©è½ãæä¾ãã¦ããã ãã®ããã«ã ll
              ã¯å®æ°å¤æã¨ä½µç¨ãã¦ä½¿ç¨ãããã¨ãã§ããã
              ãããããã®ä½¿ç¨æ¹æ³ã¯æ¨å¥¨ãã¦ããªãã

       ·      æå L 㯠e, E, f, g, G å¤æ㯠long double å¼æ°ã«å¯¾å¿ãããã¨ããã㦠d,
              i, o, u, x, X å¤æ㯠long long å¼æ°ã«å¯¾å¿ãããã¨ãæå®ããã long long
              㯠ANSI C ã§æè¨ããã¦ããªããã¨ãããã¦ãã®ããã«å¨ã¦ã®æ©ç¨®ã«
              移æ¤å¯è½ã§ã¯ãªããã¨ã«æ³¨æãããã¨ã

       ·      ãªãã·ã§ã³æå q ã ãã㯠L ã¨åä¸ã§ããã ll, L, ãã㦠q
              ã®ä½¿ç¨ã«ã¤ãã¦ã®ã³ã¡ã³ãã¯ãæºæ ãã®ã»ã¯ã·ã§ã³ã¨
              ããã°ãã®ã»ã¯ã·ã§ã³ãè¦ããã¨ã

       ·      æå Z ã¯ãç¶ãæ´æ°ã size_t å¼æ°ã«å¯¾å¿ããå¤æ (d, i, o, u, i, x, X)
              ã§ãããã¨ãæå®ããã

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

       ãã£ã¼ã«ãé·ã精度(ãããã¯ããã®ä¸¡æ¹)ã¯æ°åã®ãããã«ã¢ã¹ã¿ãªã¹ã¯ `*'
       ã«ãã£ã¦ç¤ºããããã¨ãããã ãã®å ´åã int å¼æ°ããã£ã¼ã«ãé·ã精度ãæå®ããã
       è² ã®ãã£ã¼ã«ãé·ã¯ã å·¦æãã®ãã©ã°ã«ç¶ãæ£ã®ãã£ã¼ã«ãé·ã¨ãã¦æ±ãããã
       è² ã®ç²¾åº¦ã¯ç¡è¦ãããã

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

       diouxX int å¼æ°(ãããã¯åçãªå¥ã®åã®å¼æ°)ã符å·ã¤ã10é²æ° (d ãã㦠i),
              符å·ãªã8é²æ° (o), 符å·ãªã10é²æ° (u), 符å·ãªã16é²æ° (x 㨠X)
              ã®ããããã«å¤æããã x å¤æã« abcdef ã®æåã 使ç¨ããããã¾ãã X å¤æã«ã¯
              ABCDEF ã®æåã使ç¨ãããã
              (ããæå®ããã¦ããå ´åã«ã¯)精度ã¯åºåããªããã°ãªããªãæå°ã®æ¡æ°ãä¸ããã
              å¤æãããå¤ãããããå°ããªæ¡æ°ã§ããå ´åãæ°åã®å·¦å´ã¯ã¼ãã§åããããã

       eE     double å¼æ°ãã丸ããã㦠[-]d.dddedd ã®å½¢ã«å¤æãããã
              å°æ°ç¹ã®åã«ã¯ä¸æ¡ã®æ°åãããããã®å¾ã®æ¡æ°ã¯ç²¾åº¦ã§æå®ããã æ¡æ°ã«ãªãã
              精度ãæå®ãããªãã£ãå ´åã«ã¯6ã¨ãã¦æ±ããã 精度ãã¼ã‐
              ã§ããå ´åã«ã¯å°æ°ç¹ãç¾ããªãã E å¤æã¯ææ°ã表ç¾ããã¨ãã« ( e ã§ã¯ãªã)
              E ãç¨ããã ææ°ã¯å°ãªãã¦ãäºæ¡ã«ãªã£ã¦ããã ããªãã¡ããã®å¤ãã¼ã‐
              ãªãã°ãææ°ã¯ 00 ã§ããã

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

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

       c      int å¼æ°ã¯ unsigned char ã«å¤æããããã®çµæã«å¯¾å¿ããæåãæ¸ãè¾¼ã¾ããã

       s      æååã®éåã¸ã®ãã¤ã³ã¿(æååã¸ã®ãã¤ã³ã¿)ã§ãããã¨ãæå¾ããã¦ãã ``char *''
              å¼æ°ã éåããã®æå㯠çµç«¯æå NUL ã¾ã§(çµç«¯æåã¯å«ã¾ããã«)åºåãããã
              精度ãæå®ãããå ´åã«ã¯ãæå®ãããåæ°ä»¥ä¸ã¯åºåãããªãã
              精度ãä¸ããããå ´åã«ã¯ãçµç«¯æåãåå¨ããå¿è¦ã¯ãªãã
              精度ãæå®ããã¦ããªãã£ããããã®å¤ãéåã®å¤§ãããã大ããªå ´åã«ã¯ã
              éåã¯çµç«¯æå NUL ãå«ãã§ããªããã°ãªããªãã

       p      ``void *'' ã¨è¡¨ããããã¤ã³ã¿ã§ããå¼æ°ã ( %#x ã¾ã㯠%#lx
              ã®ãããª)16é²æ°ã§åºåãããã

       n      ããã¾ã§ã«åºåãããæåæ°ã ``int *'' (ã¾ãã¯ããã¨ç‐
              価ãª)ãã¤ã³ã¿å¼æ°ã§ç¤ºãããæ´æ°ã«ä¿åãããã å¼æ°ã®å¤æã¯è¡ããªãã

       %      `%' ä¸æåãåºåãããã å¤æãããå¼æ°ã¯ç¡ãã å®å¨ãªå¤æã®è¨æ³ã§è¡¨ã㨠`%%'
              ã§ããã

       ãã£ã¼ã«ãé·ã®æå®ãç¡ãã£ãããå®éã®é·ããããå°ããã¦ãã
       ãã£ã¼ã«ãã®åãè©°ãããããã¨ã¯ãªãã
       å¤æã®çµæããã£ã¼ã«ãé·ãããé·ãå ´åã«ã¯ããã£ã¼ã«ã㯠å¤æã®çµæãå«ãããã«æ¡å¼µãããã

ä¾
       æ¥ä»ã¨æéã `Sunday, July 3, 10:02' ã®å½¢å¼ã§åºåããã«ã¯ (ããã§ã weekday ã¨
       month ã¯ãæååã¸ã®ãã¤ã³ã¿ã§ãã)ï¼
              #include <stdio.h>
              fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
                   weekday, month, day, hour, min);

       Ïãï¼æ¡ã§åºåããã«ã¯ï¼
              #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 é¢æ°ã¯ ANSI
       C3.159-1989 (``ANSI C'') ã«æºæ ãã¦ããã

       q ãã©ã°ã¯ long long ã® BSD 4.4 ã®è¨æ³ã§ãããä¸æ¹ãæ´æ°å¤æã«ããã ll ã L
       ã®ä½¿ç¨ã¯ GNU ã®è¨æ³ã§ããã

ãã°
       ãã¹ã¦ã®é¢æ°ã¯å®å¨ã« ANSI C3.159-1989 ã«æºæ ãã¦ããã ãããã 追å ã® q, Z , '
       ãã©ã°ãæä¾ããã¦ãããåæ§ã« L 㨠l ãã©ã°ã®åä½ãå¤æ´ããã¦ããã å¾è㯠ANSI
       C3.159-1989 ã§å®ç¾©ããã¦ãããã©ã°ã®æ¯ãèããå¤ãããã®ã§ããã
       å¾ã£ã¦ãå¾èã¯ãã°ã¨ã¿ãªããããããããªãã

       %p ãã©ã¼ããã㧠( 0 ãã©ã°ã精度ãæå®ãã¦)ã¼ããåããå¹æã %n ã %p å¤æã«å¯¾ã㦠#
       ãã©ã°ãæå®ããå ´åã®è¦ªåå¹æ(# ãç¡è¦ãããã¨)ã® ãããªç¡æå³ãªçµåãã¯æ¨æºã§ã¯ãªãã
       ãã®ãããªçµã¿åããã¯é¿ããã¹ãã§ããã

       ANSI C ã§å®ç¾©ããããã©ã°ã®çµã¿åããã«ã¯ æå³ããªããªããã®ããã (ä¾ãã° %Ld) ã
       ããã㯠Linux ä¸ã§ã¯è¯ãå®ç¾©ãããæ¯ãèããæã¤ã§ããããã ä»ã®ã¢ã¼ã‐
       ãã¯ãã£ã¼ã§ãããã§ããå¿è¦ã¯ãªãã ããããã«ãã»ã¨ãã©ã®å ´åã ANSI C
       ã§å®ç¾©ããã¦ããªããã©ã°ãé常使ç¨ããªãæ¹ãè¯ãã diouxX å¤æã¾ã㯠ll ã¨ã¨ãã« L
       ã使ç¨ããã®ãªãã°ã q ã使ç¨ããã¹ãã§ããã

       å®æ°å¤æã«ãã㦠L ã¨ç価ã«ä½¿ç¨ãããã¨ãã£ã q ã®ä½¿ç¨æ¹æ³ã¯ BSD 4.4
       ã®ããã¨ã¯åãã§ã¯ãªãã

       sprintf 㨠vsprintf ã¯ç¡éã«é·ãæååãä»®å®ãã¦ããã®ã§ãå¼ã³åºãå´ã¯
       å®éã®é åããããµããªãããã«æ³¨æããªããã°ãªããªãã
       ããã¯ãä¿è¨¼ãããã¨ãä¸å¯è½ã§ãããã¨ãå¤ãã å¾ã£ã¦ã asprintf ã¾ãã¯ã vasprintf
       使ãã¹ãã§ããã



Linux Manpage                   28 January 1996                      PRINTF(3)