C语言 如何用一个 printf 打印多个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32315569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:05:29  来源:igfitidea点击:

How can I print multiple character with one printf?

cprintf

提问by Shift Loader

I want to print multiple character using printf. My approach up to now is this-

我想使用printf. 到目前为止,我的方法是——

#include <stdio.h>

int main()
{
    printf("%*c\n", 10, '#');

    return 0;
}

But this only prints 9 spaces before the #.

但这只会在#.

I want to print it like this-

我想像这样打印它-

##########

I am unable to figure out how to do this. Please help me?

我无法弄清楚如何做到这一点。请帮我?

采纳答案by Dipu

You can not use printflike that to print repetitive characters in Ansi C. I suggest you to use a loop like this -

你不能用printf这样的方式在 Ansi C 中打印重复的字符。我建议你使用这样的循环 -

#include <stdio.h>

int main()
{
    int i;
    for(i = 0; i < 10; i++) putchar('#');

    return 0;
}

Or if you have absolutely no desire to use loops, you can do something like this-

或者,如果您绝对不想使用循环,则可以执行以下操作-

#include <stdio.h>
int main()
{
    char out[100];
    memset(out, '#', 10);
    out[10] = 0;
    printf("%s", out);

    return 0;
}

By the way, using printflike this also works-

顺便说一句,printf像这样使用也有效-

#include <stdio.h>

int main()
{
    printf("%.*s", 10, "############################################");

    return 0;
}

回答by Luis Colorado

I think the best approach, if you have an upper limit to the number of characters to be output is:

如果您对要输出的字符数有上限,我认为最好的方法是:

printf("%.*s", number_of_asterisks_to_be_printed,
"**********************************************************************");

I think this will also be the most efficient, portable way to do that.

我认为这也将是最有效、最便携的方式。

回答by user3629249

this will print ten # characters, followed by a newline

这将打印十个 # 字符,后跟一个换行符

char tenPounds[] = "##########"; 
printf( "%s\n", tenPounds);

回答by user3629249

I am working on a similar problem in "The C Programming Language" book (exercises 1-13 and 1-14). My own program, to start simply, is to count the occurrences of the digits 0 to 9 in a given input, and print a horizontal histogram made of '=' bars according to each count.

我正在研究“C 编程语言”一书中的类似问题(练习 1-13 和 1-14)。我自己的程序,简单地说,是计算给定输入中数字 0 到 9 的出现次数,并根据每个计数打印由“=”条组成的水平直方图。

To do this, I created the following program;

为此,我创建了以下程序;

main() {
    int c, ix, k;
    int nDigit[10];

    //Instantiate zero values for nDigits array
    for(ix = 0; ix < 10; ix++) {
        nDigit[ix] = 0;
    }

    //Pull in input, counting digit occurrences and 
    //incrementing the corresponding value in the nDigit array
    while ((c = getchar()) != EOF) {
        if (c >= '0' && c <= '9') {
            ++nDigit[c-'0'];
        }
    }

    //For each digit value in the array, print that many
    //'=' symbols, then a new line when completed
    for (ix = 0; ix < 10; ix++) {
        k = 0;
        while (k <= nDigit[ix]) {
            putchar('=');
            k++;
        }
        printf("\n");
    }

}

}

Note that this is a work in progress. A proper histogram should include axes labels, and most importantly this program does not account for digits of zero count. If the input includes five 1's but no 0's, there is no visual way to show that we have no zeros. Still, the mechanism for printing multiple symbols works.

请注意,这是一项正在进行的工作。一个合适的直方图应该包括轴标签,最重要的是这个程序不考虑零计数的数字。如果输入包含五个 1 但没有 0,则没有直观的方式表明我们没有零。尽管如此,打印多个符号的机制仍然有效。