printf输出多列时的列对齐

  1. 列左对齐

printf("%-*s", 20, string); 表示输出字符串左对齐输出20,如果字符串不够20个,以空格补齐。 -表示左对齐。


例:

struct help_struct
{
char *option_name;
char *option_value;
char *option_ext;
};


struct help_struct options[] =
{
{"set_node_id", "node_id", "set the node id"},
{"set_debug_level", "debug_level", "4:DEBUG 3:INFO 2:WARNING 1:ERR 0:NESS"},
{"flush_pool", "pool_name", ""},
{"get_lu_count", "", ""},
{"check_pools", "pool_name", ""},
{"invalid_pool", "pool_name", ""},
{"set_mirror", "mirror_state", "0:disable 1:enable"},
{(char*)NULL, (char*)NULL, (char*)NULL}
};


static void show_usage(const char *execname)
{
struct help_struct *opt = NULL;
printf("Usage: %s [options] ...\n", execname);
printf("Options:\n");
for(opt = options; opt->option_name; opt++)
{
printf("  --");
printf("%-*s",25, opt->option_name);
printf("%-*s",20, opt->option_value);
printf("%-*s",50, opt->option_ext);
printf("\n");
}
}

show_usage输出结果为:

Usage: ./vicm_test [options] ...
Options:
  --set_node_id              node_id             set the node id                                   
  --set_debug_level       debug_level      4:DEBUG 3:INFO 2:WARNING 1:ERR 0:NESS             
  --flush_pool                  pool_name                                                             
  --get_lu_count                                                                                   
  --check_pools              pool_name                                                             
  --invalid_pool               pool_name                                                             
  --set_mirror                  mirror_state       0:disable 1:enable  



2. 右对齐

printf("%*s", 20, string); 表示输出字符串右对齐输出20,如果字符串不够20个,以空格补齐。 没有-表示右对齐。


例:

static void show_usage(const char *execname)
{
struct help_struct *opt = NULL;
printf("Usage: %s [options] ...\n", execname);
printf("Options:\n");
for(opt = options; opt->option_name; opt++)
{
printf("  --");
printf("%*s",25, opt->option_name);
printf("%*s",20, opt->option_value);
printf("%*s",50, opt->option_ext);
printf("\n");
}
}

show_usage输出结果为:

Usage: ./vicm_test [options] ...
Options:

  --                 set_node_id                 node_id                                                                    set the node id
  --          set_debug_level          debug_level             4:DEBUG 3:INFO 2:WARNING 1:ERR 0:NESS
  --                     flush_pool            pool_name                                                  
  --                 get_lu_count                                                                      
  --                  check_pools           pool_name                                                  
  --                   invalid_pool            pool_name                                                  
  --                      set_mirror           mirror_state                                                           0:disable 1:enable

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值