How to format printf statement better so things always line up

前端 未结 3 696
一生所求
一生所求 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:26

    Like dtrosset said:

    printf("name: %12s\t"
           // etc...
    

    Here is some documentation on printf format strings:

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

    Just make sure that the field width you specify is larger than whatever you expect to be printing. If you specify %2d, for instance, and then print 555, it will still print with 3 characters even though the rest of your fields are 2 characters, and it won't line up the way you want it to.

提交回复
热议问题