0

Is there a recommended way in C to tab my output so that it actually aligns with the data? I am currently using the escape sequence \t which can be viewed below.

#include <stdio.h>

int main()
{

    int i;
    int meatBalls[5] = {1, 2, 3, 4, 5};

    printf("\tElement \t Address \t Value \n");

    for(i=0; i < 5; i++) {
        printf("meatBalls[%d] \t %p \t %d \n", i, &meatBalls[i], meatBalls[i]);
    }


    return 0;
}

This is my current output and as you can see the titles are not aligning

    Element      Address     Value   
meatBalls[0]     0x7fff547d0640      1 
meatBalls[1]     0x7fff547d0644      2 
meatBalls[2]     0x7fff547d0648      3 
meatBalls[3]     0x7fff547d064c      4 
meatBalls[4]     0x7fff547d0650      5 
Wunderbread
  • 898
  • 2
  • 14
  • 34
  • 1
    You should look into the alignment options in format strings for printf. Something like this might be helpful: https://stackoverflow.com/questions/13548785/custom-string-alignment-using-printf-in-c – Retired Ninja May 30 '17 at 03:00
  • 1
    Notice that `%p` is outputting an implementation specific string. So you cannot in principle know in advance its width. Pedantically any `%p` formatting is *implementation specific* – Basile Starynkevitch May 30 '17 at 03:14
  • Note that you [should cast the argument to %p to `void *`](https://stackoverflow.com/questions/24867814/printfp-and-casting-to-void), or the behavior is undefined. – Ilja Everilä May 30 '17 at 03:28

2 Answers2

3

Step in the right direction:

#include <stdio.h>

int main(void)
{
    int i;
    int meatBalls[5] = {1, 2, 3, 4, 5};

    printf("%12s %18s %6s\n", "Element", "Address", "Value");

    for(i=0; i < 5; i++) {
        printf("meatBalls[%d] \t %p \t %d \n", i, &meatBalls[i], meatBalls[i]);
    }

    return 0;
}

Output

     Element            Address  Value
meatBalls[0]     0x7fff60a1f700      1 
meatBalls[1]     0x7fff60a1f704      2 
meatBalls[2]     0x7fff60a1f708      3 
meatBalls[3]     0x7fff60a1f70c      4 
meatBalls[4]     0x7fff60a1f710      5 
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • It works, but without guarantee. `%p` is implementation specific. Might not work, e.g. on a 32 bits ARM the same way as on a 64 bits Linux/x86-64. – Basile Starynkevitch May 30 '17 at 03:15
  • Thanks everyone for the feedback. Using the above and updating my printf to include "%12s %18s %6s" looks sufficient on my machine. OS X 10.11.6 using CLion IDE. However, I am going to go ahead and look into all that the printf statement has to offer. – Wunderbread May 30 '17 at 03:21
1

The simplest way is probably to just make better use of "printf" to set field widths... perhaps something like this:

#include <stdio.h>

int main() {
    int i;
    int meatBalls[] = {1, 2, 3, 4, 5};

               /-- 12 characters in "Element": 9 in "meatBalls", plus 3 in the subscript ("[#]")
               |      /-- 14 characters in an address
               |      |    /-- unimportant, since @ end of line
    printf("%-12s  %-14s  %s\n", "Element", "Address", "Value");
    for(i=0; i < 5; i++) {
        printf("meatBalls[%1d]  %14p  %d\n", i, &meatBalls[i], meatBalls[i]);
    }
    return 0;
}

Note that you may have to play with the pointer format, since pointers are platform-dependent

SGeorgiades
  • 1,771
  • 1
  • 11
  • 11