使用 printf 函数格式化间距 C

Formatting spacing with the printf function C

如何创建 NumbersMonths 的间距彼此等距的代码。代码源码已在issue.

的回答中获取
#include <stdio.h>

#define MAX_LEN 32

int main() {
    int Numbers[12] = {3, 4, 5, 1, 2, 7, 8, 9, 3, 4, 7, 8};
    char Months[][MAX_LEN] = {"Jan",       "Feb",     "March",    "April",
                              "May",       "June",    "July",     "August",
                              "September", "October", "November", "December"};

    for (int i = 0; i < 12; i++)
        printf("%s: %d\n", Months[i], Numbers[i]);

    return 0;
}

输出

Jan: 3
Feb: 4
March: 5
April: 1
May: 2
June: 7
July: 8
August: 9
September: 3
October: 4
November: 7
December: 8

预期输出:

Jan:       3
Feb:       4
March:     5
April:     1
May:       2
June:      7
July:      8
August:    9
September: 3
October:   4
November:  7
December:  8

您可以在格式字符串中使用额外的修饰符来使 printf() 正确对齐:

  • 要打印特定宽度的内容,请在格式化字符之前将宽度作为整数预先添加:例如,%10s 会将字符串打印到宽度为 10 的(最大)列,并向左填充空间。如果您的字符串超过 10 个字符,它会打印完整的字符串,但请记住这一点。所以你的具体例子:
char Months[][MAX_LEN] = {"Jan",       "Feb",     "March",    "April",
                          "May",       "June",    "July",     "August",
                          "September", "October", "November", "December"};

for (int i = 0; i < 12; i++) {
  /* Note the 10 here! */
  printf("%10s: %d\n", Months[i], Numbers[i]);       
}

结果

       Jan: 3
       Feb: 4
     March: 5
     April: 1
       May: 2
      June: 7
      July: 8
    August: 9
 September: 3
   October: 4
  November: 7
  December: 8

如果你想左对齐你的字符串,你只需在整数值前加上一个-,即:

printf("%-10s: %d\n", Months[i], Numbers[i]);

产生

Jan       : 3
Feb       : 4
March     : 5
April     : 1
May       : 2
June      : 7
July      : 8
August    : 9
September : 3
October   : 4
November  : 7
December  : 8
  • 或者,您也可以使用 *,它允许您在 printf() 调用中可变地声明要填充的长度。要使用它,你需要计算你希望填充的长度作为一个额外的参数:
int myPadWidth = 15;
/* calculate the length of Months[i] each loop and cast size_t result to int */
int lenMonths = (int)strlen(Months[i]);
/* Note the extra argument in the printf call here! */
printf("%s: %*d\n",Months[i],myPadWidth-lenMonths,Numbers[i]);

这会导致

Jan:            3
Feb:            4
March:          5
April:          1
May:            2
June:           7
July:           8
August:         9
September:      3
October:        4
November:       7
December:       8

有多种方法可以实现这一点。

一种是在printf格式串中使用一个*字符作为(最小)field width specifier;这指定在调用函数时根据额外参数确定实际值,在紧接应用它的值之前给定。

可以通过从(最大)所需字段宽度中减去前面字段的长度来计算该宽度说明符的值;在您的例子中,这将是 Months[i] 字符串的长度,它由 strlen 函数返回。

这是一个工作示例:

#include <stdio.h>
#include <string.h> // For the definition of "strlen"

#define MAX_LEN 32

int main()
{
    int Numbers[12] = { 3, 4, 5, 1, 2, 7, 8, 9, 3, 4, 7, 8 };
    char Months[][MAX_LEN] = { "Jan",       "Feb",     "March",    "April",
                               "May",       "June",    "July",     "August",
                               "September", "October", "November", "December" };

    for (int i = 0; i < 12; i++)
        printf("%s: %*d\n", Months[i], 10 - (int)strlen(Months[i]), Numbers[i]);

    return 0;
}

输出:

Jan:       3
Feb:       4
March:     5
April:     1
May:       2
June:      7
July:      8
August:    9
September: 3
October:   4
November:  7
December:  8