Variable sized padding in printf

后端 未结 5 1369
终归单人心
终归单人心 2020-11-30 01:21

Is there a way to have a variable sized padding in printf?

I have an integer which says how large the padding is:

void foo(int paddingSi         


        
5条回答
  •  醉话见心
    2020-11-30 01:54

    Be careful though - some compilers at least (and this may be a general C thing) will not always do what you want:

    char *s = "four";
    printf("%*.s\n", 5, s); // Note the "."
    

    This prints 5 spaces;

    char *s = "four";
    printf("%*s\n", 3, s);  // Note no "."
    

    This prints all four characters "four"

提交回复
热议问题