简体   繁体   English

fprintf C多个空格

[英]fprintf C multiple spaces

Hello i'm new to C and have some confusion Regarding fprintf 你好,我是C的新手,对于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 值得查看文档的详细信息: man printf

printf has lots of options, is this what you had in mind? printf有很多选择,这是你的想法吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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