C printf缩进问题

C printf indentation issue

我希望正确缩进下面的内容,我希望 DG1, DG2, DG3, DG4 应该在同一列中,有没有最佳方法?谢谢,任何指点或帮助将不胜感激。


      Test                                                                 DG1                                                                 
                                                                       DG2                                                                     
                                                                       DG3                                                                     
                                                                       DG3                                                                     

sh-4.2#^C

#include <stdio.h>

int main()
{
    printf("%-64s\t%s\n", "List", "Bindings");
    printf("-----------------------------------------------------------------------------\n");

    const char *curr_dl_result[] = {
        [0] = "DG1",
        [1] = "DG2",
        [2] = "DG3",
        [3] = "DG3",
    };
    int dg_count = 4;
    int  j = 0;
    char buff[1024];
    printf("%s", "Test");
    if(dg_count) {
        unsigned char is_print_done = 0;     
        for(j = 0; j < dg_count; j++) {     
            printf("%78s\n", (curr_dl_result[j]));
            is_print_done = 1;
        }
    } else {
        printf("%78s\n","-" );
    }
return 0;
}

例如用这个 if 替换 for 里面的 printf 应该可以解决问题:

if (j == 0)
     printf("%74s\n", (curr_dl_result[j])); // account for "Test" (4 chars) on same line
else
     printf("%78s\n", (curr_dl_result[j]));

你怎么不换

printf("%s", "Test");

printf("%s\n", "Test"); 

这将正确打印和缩进 DG1、DG2、DG3、DG4,如果您不介意的话,将在测试后打印一行 看到这个 http://ideone.com/ZhOlhF

一些动态分配怎么样(仍然有一些硬编码的东西但更通用):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    printf("%-64s\t%s\n", "List", "Bindings");
    printf("-------------------------------------------------------------------------------\n");

     const char *curr_dl_result[] = {
        [0] = "DG1",
        [1] = "DG2",
        [2] = "DG3",
        [3] = "DG3",
     };
     int dg_count = 4;
     int  j = 0;
     int pos = printf("%s", "Test");
     if(dg_count) {
         unsigned char is_print_done = 0;
         for(j = 0; j < dg_count; j++) {
             char *spacer = malloc(74-pos*sizeof(char));
             memset(spacer, ' ', 74-pos);
             printf("%s", spacer);
             printf("%s\n", (curr_dl_result[j]));
             is_print_done = 1;
             pos = 0;
             free(spacer);
         }
     }
     else {
         printf("%78s\n","-" );
     }
    return 0;
}

也许使用 printf returns 打印的字符数这一事实, 然后在打印最后一个字符串之前用空格填充。

#include <stdio.h>

void my_out(const char* str1, const char* str2)
{
    int len = printf("%s", str1);
    int i;
    for( i=len ; i<60 ; i++ )
    {
        printf(" ");
    }
    printf("%s\n", str2);
}

int main()
{

    my_out("List", "Bindings");
    printf("-----------------------------------------------------------------------------\n");

    const char *curr_dl_result[] = {
        [0] = "DG1",
        [1] = "DG2",
        [2] = "DG3",
        [3] = "DG3",
    };

    my_out("Test",curr_dl_result[1]);
    my_out("Test  eee",curr_dl_result[2]);
    my_out("Test 22",curr_dl_result[3]);
    return 0;
}

这将打印:

List                                                        Bindings
-----------------------------------------------------------------------------
Test                                                        DG2
Test  eee                                                   DG3
Test 22                                                     DG3