2

Hello i'm new to C and have some confusion Regarding fprintf

I'm trying to create a Data table and i want it to look exactly like this:

Rectangle A                    Rectangle B 
SW corner   Height   Width     SW corner   Height   Width

Most of my confusion is coming from the white spaces, surely there is a better way than just adding an empty String.

3 Answers 3

5

You can use width specifier:

printf("[%5d] [%-5d]\n", 42, 42);
Sign up to request clarification or add additional context in comments.

Comments

4

You can give a field width with the format specifier:

printf( "%20s", str );

You can also make contents left- or right-aligned within that field, and many other things. It's really worth checking out the details of the documentation: man printf

Comments

0

printf has lots of options, is this what you had in mind?

#include <stdio.h>

main() {

    char *label1="Rectangle A";
    char *label2="Rectangle B";

    printf("%-31s%-31s\n",label1,label2);
    printf("SW corner   Height   Width     SW corner   Height   Width\n");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.