简体   繁体   English

使用 printf 后如何打印 int 和带空格的字符串?

[英]How to print an int and a string with spaces after using printf?

I usually use printf("%-8d",a);我通常使用printf("%-8d",a); for example for 8 spaces after (and including) an integer.例如,在 integer 之后(包括)的 8 个空格。 My code:我的代码:

#include <stdio.h>
#include <string.h>
    
int main()
{
    int a = 10;
    char b = "Hello";
}

How can I print: '#10-Hello ' with 16 spaces (8 is the integer and the string, and 8 spaces after)?如何打印: '#10-Hello '有 16 个空格(8 个是 integer 和字符串,后面有 8 个空格)?

Do it in two steps.分两步进行。 First combine the number and string with sprintf() , then print that resulting string in a 16-character field.首先将数字和字符串与sprintf()组合,然后在 16 个字符的字段中打印结果字符串。

int a = 10;
char *b = "Hello";
char temp[20];
sprintf(temp, "#%d-%s", a, b);
printf("%-16s", temp);

A tab is 8 spaces, so, you can add \t\t The below is a super basic way to print what you wanted.一个制表符是 8 个空格,因此,您可以添加 \t\t 以下是打印您想要的内容的超级基本方法。

printf('#' + a + '-' + b + '\t\t');

I'm not as familiar with the syntax of C so it may be:我不太熟悉 C 的语法,所以它可能是:

printf('#', a, '-', b, '\t\t');

Also, as mentioned in a previous answer, "Hello" is not a char but either an array of char or a String.此外,如上一个答案中所述,“Hello”不是字符,而是字符数组或字符串。

#include <stdio.h>
#include <string.h>
    
int main()
{
    int a = 10;
    char b[] = "Hello";
    printf("#%d-%-17s",a,b);
}

this should get the job done, adjust your spacing as needed这应该可以完成工作,根据需要调整间距

Could do this with 2 printf() s.可以用 2 个printf()来做到这一点。 Use the return value of the first to know its print length, then print spaces needed to form a width of 16. No temporary buffer needed.使用第一个的返回值知道它的打印长度,然后打印空间需要形成一个宽度为 16。不需要临时缓冲区。

#include <assert.h>
#include <stdio.h>

int main(void) {
    int width = 16;
    int a = 10;
    char *b = "Hello"; // Use char *

    int len = printf("#%d-%s", a, b);
    assert(len <= width && len >= 0);
    printf("%*s", width - len, "");  // Print spaces
}

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

相关问题 在 C 中使用 printf 打印空格数 - print number of spaces using printf in C 为什么printf会打印我分配给int空间的空间数? - Why does printf print the number of spaces I assigned to the int space? 有没有一种方法可以指定使用printf()打印出一个字符串中的多少个字符? - Is there a way to specify how many characters of a string to print out using printf()? 如何使用 printf 打印字符串而不打印尾随换行符 - How to print a string using printf without it printing the trailing newline 如何使用 printf 在字符串中打印多个变量? - How can you print multiple variables inside a string using printf? 将int数组转换为字符串,然后使用printf打印,而不分配新的内存 - Cast an int array to string, then print with printf, without allocating new memory Printf 不会打印字符串 &amp; 调试显示字符串在到达 printf 后消失 - Printf won't print string & debug shows that the string disappears after it reaches printf 如何使用 (s)printf 将地址完美地打印到 c 字符串或标准输出中? - How to print an address flawlessly into a c-string or stdout using (s)printf? 如何使用printf打印非空终止字符串? - How do I print a non-null-terminated string using printf? 如何在C中使用printf()格式在double前面保留空间 - How to reserve spaces in front of a double using printf() formatting in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM