简体   繁体   中英

fprintf C multiple spaces

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.

您可以使用宽度说明符:

printf("[%5d] [%-5d]\n", 42, 42);

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

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM