printf输出多列时的列对齐
2012-05-25 10:03
363 查看
列左对齐
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
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
相关文章推荐
- C语言printf对齐输出方式
- C语言 printf 格式化 输出 右对齐补零
- C语言 printf 格式化 输出 右对齐补零
- printf输出多列时的列对齐
- printf 十六进制格式化输出 2字节对齐
- printf函数十六进制格式化输出并且2字节对齐
- printf 动态适配终端大小对齐显示输出
- C语言 printf 格式化 输出 右对齐补零
- printf 十六进制格式化输出 2字节对齐
- shell脚本输出对齐问题(使用printf)
- C语言 printf /sprintf格式化 输出 右对齐补零
- <C语言>printf的对齐输出
- printf 十六进制格式化输出 2字节补0对齐 ,
- shell printf命令:格式化输出语句
- 把十进制数(long型)分别以二进制和十六进制形式输出,不使用printf系列库函数
- 通过printf设置Linux终端输出的颜色和显示方式
- printf打印颜色输出控制
- c语言:有趣的printf输出,“4321”
- Python:字符串格式化输出(format/printf)
- printf格式化输出