12

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

#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?

Shift Loader
  • 129
  • 1
  • 1
  • 7
  • @Olaf I know I can get answer by lot of searching. But all of them are long and hard to understand. Here I got answer within minutes. Is not this site for asking question like this? – Shift Loader Aug 31 '15 at 16:31
  • 2
    Well, you are expected to make a reasonable effort on your own before asking. Otherwise SO will soon become a tutorial site, which is definitively not intended. If you have basic problems with undrstanding, it is often a good idea to take a step back and read a book or an online-tutorial. If that is homework, you should ask your tutor. A good one will appreciate the feedback and adopt his course to anser the question. – too honest for this site Aug 31 '15 at 16:38
  • For the format string: This is very clearly stated in few sentences. A more appropriate answer might have been about how this format-option actually works. Anyway, you got your answers. – too honest for this site Aug 31 '15 at 16:39
  • @Olaf Okay. Sometimes you need quick answers to move forward. That's why I posted it here. Next time I will be more careful about it. – Shift Loader Aug 31 '15 at 16:43
  • 1
    Possible duplicate of [How to repeat a char using printf?](http://stackoverflow.com/questions/14678948/how-to-repeat-a-char-using-printf) – Ciro Santilli OurBigBook.com Mar 24 '17 at 08:33

4 Answers4

10

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

#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 printf like this also works-

#include <stdio.h>

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

    return 0;
}
Dipu
  • 6,999
  • 4
  • 31
  • 48
4

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.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
2

this will print ten # characters, followed by a newline

char tenPounds[] = "##########"; 
printf( "%s\n", tenPounds);
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • 1
    I don't know beforehand how many characters I need to print. But by the way thanks a lot. – Shift Loader Aug 31 '15 at 16:20
  • for your solution, just print with `puts(thenPounds);` and you'll get the same ten characters, with the trailing newline. No need to further format anything. – Luis Colorado Feb 13 '17 at 09:29
0

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.

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.