How to format printf statement better so things always line up

前端 未结 3 689
一生所求
一生所求 2021-01-22 05:53

I have this printf statement:

 printf(\"name: %s\\t\"
        \"args: %s\\t\"
        \"value %d\\t\"
        \"arraysize %d\\t\"
        \"scope %d\\n\",
               


        
3条回答
  •  半阙折子戏
    2021-01-22 06:20

    Each conversion specifier can be given a field width which give the minimum number of characters that conversion will use. There are other flags and precision that can be used to control the output (for example with the %s conversion the precision item says how many characters maximum will be used).

    printf("name: %20.20s\t"
            "args: %10.10s\t"
            "value %6d\t"
            "arraysize %6d\t"
            "scope %6d\n",
             sp->name,
             sp->args,
             sp->value,
             sp->arraysize,
             sp->scope);
    

提交回复
热议问题