代码之家  ›  专栏  ›  技术社区  ›  Aamir

%*s格式说明符是什么意思?

  •  39
  • Aamir  · 技术社区  · 15 年前

    %*s

    fprintf(outFile, "\n%*s", indent, "");
    
    5 回复  |  直到 7 年前
        1
  •  61
  •   sth Wojciech Parzych    15 年前

    It's used to specify, in a dynamic way, what the width of the field is

    所以“缩进”指定要为参数列表中紧随其后的字符串分配多少空间。

    所以

    printf("%*s", 5, "");
    

    printf("%5s", "");
    

    这是在文件中放置一些空格的好方法,可以避免循环。

        2
  •  10
  •   user2132064    11 年前

        3
  •  2
  •   Basheer AL-MOMANI    7 年前
        4
  •  1
  •   jitter    15 年前

    printf("%*d", 5, 10) //will result in "10" being printed with a width of 5.
    
        5
  •  1
  •   pauldoo    15 年前

    http://www.cplusplus.com/reference/clibrary/cstdio/printf/

    printf("%*s", 4, myValue); printf("%4s", myValue);

        6
  •  1
  •   zeitgeist    3 年前

    在printf和fprintf中使用时:

    printf("%*s", 4, myValue); is equivalant to printf("%4s", myValue);
    

    在scanf和sscanf中使用时:

    /* sscanf example */
    #include <stdio.h>
    
    int main ()
    {
      char sentence []="Rudolph is 12 years old";
      char str [20];
      int i;
    
      sscanf (sentence,"%s %*s %d",str,&i);
      printf ("%s -> %d\n",str,i);
      
      return 0;
    }
    

    Rudolph -> 12