Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,892 questions

51,823 answers

573 users

How to align columns in textual table with printf function in C

1 Answer

0 votes
#include <stdio.h>

int main(void) {

    char *row1[] = {"idx", "cat", "num"};
    int col1[] = { 1, 2, 3 };
    char *col2[] = { "a", "bb", "ccc" };
    int col3[] = { 85487, 9473, 91 };

    printf("%*s | %*s | %*s\n", -3, row1[0], -8, row1[1], 7, row1[2]);
    printf("%*s | %*s | %*s\n", -3, "---", -8, "---", 7, "---");

    size_t len = sizeof col1 / sizeof col1[0];
    for (int i = 0; i < len; ++i) {
        printf("%-3d | %-8s | %7d\n", col1[i], col2[i], col3[i]);
    }

    return(0);
}





/*
run:

idx | cat      |     num
--- | ---      |     ---
1   | a        |   85487
2   | bb       |    9473
3   | ccc      |      91

*/

 



answered May 3, 2021 by avibootz

Related questions

...