How to format strings using printf() to get equal length in the output?

后端 未结 6 1639
一个人的身影
一个人的身影 2020-11-28 05:10

I have two functions, one which produces messages like Starting initialization... and another which checks return codes and outputs \"Ok\", \

6条回答
  •  一整个雨季
    2020-11-28 05:52

    Additionally, if you want the flexibility of choosing the width, you can choose between one of the following two formats (with or without truncation):

    int width = 30;
    //no truncation uses %-*s
    printf( "%-*s %s\n", width, "Starting initialization...", "Ok." );
    // output is "Starting initialization...     Ok."
    
    //truncated to the specified width uses %-.*s
    printf( "%-.*s %s\n", width, "Starting initialization...", "Ok." ); 
    // output is "Starting initialization... Ok."
    

提交回复
热议问题