How to repeat char with printf?

I would like to do something like printf("?", count, char) to repeat the character count times.

Which correct format string is suitable for this?

EDIT: Yes, it is obvious that I could call printf() in a loop, but this is exactly what I wanted to avoid.

+48
c printf
04 Feb '13 at 0:23
source share
13 answers

The short answer is yes, the long answer is: not the way you want it.

You can use the% * printf form, which takes a variable width. And, if you use β€œ0” as the value for printing, combined with right-aligned text with zero padding on the left.

 printf("%0*d\n", 20, 0); 

produces:

 00000000000000000000 

With my tongue firmly planted in my cheek, I offer this small piece of horror code.

On several occasions, you just need to do things badly to remember why you try so hard the rest of the time.

 #include <stdio.h> int width = 20; char buf[4096]; void subst(char *s, char from, char to) { while (*s == from) *s++ = to; } int main() { sprintf(buf, "%0*d", width, 0); subst(buf, '0', '-'); printf("%s\n", buf); return 0; } 
+42
May 23 '13 at 12:08
source share

You can use the following technique:

 printf("%.*s", 5, "================="); 

It will open "=====" It works for me on Visual Studio, so it should not work on all C compilers.

+88
Apr 30 '13 at 12:25
source share

In C ++, you can use std :: string to get a duplicate character

 printf("%s",std::string(count,char).c_str()); 

For example:

 printf("%s",std::string(5,'a').c_str()); 

exit:

 aaaaa 
+14
May 15 '13 at 5:21
source share

If you limit yourself to repeating either 0 or a space you can make:

For spaces:

 printf("%*s", count, ""); 

For zeros:

 printf("%0*d", count, 0); 
+10
Aug 11 '15 at 6:46
source share

There is no such thing. You need to either write a loop using printf , or puts , or write a function that copies the number of lines to a new line.

+8
Feb 04 '13 at 0:26
source share

If you have a compiler that supports the alloca () function, then this is a possible solution (rather ugly):

 printf("%s", (char*)memset(memset(alloca(10), '\0', 10), 'x', 9)); 

It basically allocates 10 bytes on the stack, which are populated with "\ 0", and then the first 9 bytes are populated with "x".

If you have a C99 compiler, then this might be a tidier solution:

 for (int i = 0; i < 10; i++, printf("%c", 'x')); 
+6
Dec 24 '13 at 17:54
source share

printf does not do this - and printf overflows to print a single character.

 char c = '*'; int count = 42; for (i = 0; i < count; i ++) { putchar(c); } 

Do not worry that it is ineffective; putchar() buffers its output, so it will not perform a physical output operation for each character if it is not needed.

+5
Feb 04 '13 at 0:53
source share
 char buffer[31]; /*assuming you want to repeat the c character 30 times*/ memset(buffer,(int)'c',30); buffer[30]='\0'; printf("%s",buffer) 
+2
Nov 21 '14 at 1:40
source share

you can create a function to do this job and use it

 #include <stdio.h> void repeat (char input , int count ) { for (int i=0; i != count; i++ ) { printf("%c", input); } } int main() { repeat ('#', 5); return 0; } 

This will lead to the conclusion

 ##### 
+2
Sep 18 '15 at 12:53 on
source share
 printf("%.*s\n",n,(char *) memset(buffer,c,n)); 

n <= sizeof(buffer) [possibly also n <2 ^ 16]

However, the optimizer can change it to puts(buffer) , and then there will be no EoS .....

And it is assumed that memset is an assembler instruction (but still a chip loop).

It is clearly seen that there is no solution if you pre-installed "No loop".

+1
Apr 27 '14 at 16:26
source share

This is not a general solution, but if you need to print the indicated number of whitespace, you can do the following.

 printf("%*.0i", count, 0); 
0
Aug 23 '14 at
source share
 char buffer[41]; memset(buffer, '-', 40); // initialize all with the '-' character<br /><br /> buffer[40] = 0; // put a NULL at the end<br /> printf("%s\n", buffer); // show 40 dashes<br /> 
0
Feb 27 '18 at 22:27
source share

I think I like it.

 void printchar(char c, int n){ int i; for(i=0;i<n;i++) print("%c",c); } printchar("*",10); 
-one
Feb 04 '13 at 0:50
source share



All Articles