关于printf的输出对齐解决思路
关于printf的输出对齐
看下图,每一项输出怎么保证第二列对齐的?

------解决思路----------------------
Format Specification Fields: printf and wprintf Functions
A format specification, which consists of optional and required fields, has the following form:
%[flags] [width] [.precision] [{h
------解决思路----------------------
l
------解决思路----------------------
I64
------解决思路----------------------
L}]type
printf Width Specification
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).
The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).
If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.
------解决思路----------------------
printf 详细可查资料看看。
技巧方面.....看个人喜欢了
看下图,每一项输出怎么保证第二列对齐的?
------解决思路----------------------
Format Specification Fields: printf and wprintf Functions
A format specification, which consists of optional and required fields, has the following form:
%[flags] [width] [.precision] [{h
------解决思路----------------------
l
------解决思路----------------------
I64
------解决思路----------------------
L}]type
printf Width Specification
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).
The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).
If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.
------解决思路----------------------
printf 详细可查资料看看。
技巧方面.....看个人喜欢了
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
char buf_lab[128];
char buf_val[128];
int tab_width = 30; // 宽度控制
printf("\n-- style 1) -------------------------------\n");
for (i=1; i < 32; i+=5) {
sprintf(buf_lab, "label%d:", ( 1 << i));
sprintf(buf_val, "val -- %d:", ( 1 << 32 - i));
printf(" %-*s:%s\n", tab_width, buf_lab, buf_val);
}
printf("\n--style 2) --------------------------------\n");
printf( "%s%s\n%s%s\n%s%s\n%s%s\n",
"ELF Header: ", " ",
" Magic: ", "7f 45 4c 01 ... ",
" 我更老喜欢这种方式.........: ", "因为在代码中比较直观... (byres)",
" Section header string table index: ", "31"
);
return 0;
}