Suppose I want to generate ------
, with only -
, is there a C macro to generate repeated string ?

- 22,498
- 29
- 129
- 265
-
1Yes: http://stackoverflow.com/a/10542793/726361 and remember that character literals placed side-by-side are automatically concatenated. – Seth Carnegie Jul 18 '12 at 02:14
-
1On my keyboard you just have to hold the key down. No need for macros. – Bo Persson Jul 18 '12 at 11:49
3 Answers
use boost, E.g
#include <stdio.h>
#include <boost/preprocessor/repetition/repeat.hpp>
#define Fold(z, n, text) text
#define STRREP(str, n) BOOST_PP_REPEAT(n, Fold, str)
int main(){
printf("%s\n", STRREP("-", 6));
return 0;
}

- 39,699
- 7
- 33
- 70
-
4
-
3@TheMask: agreed. Why would you increase your compile time (*a lot*) just for this? Boost is overkill. – sfstewman Jul 18 '12 at 02:41
-
1maybe overkill but on the other side why people shouldn't use tools exactly designed for such problems ? BOOST is great library - good examples like this make more ppl intersted in using it. – Piotr Tkaczyk Jul 18 '12 at 08:02
-
@TheMask - I think there is a literal because I just want eventually. – BLUEPIXY Jul 18 '12 at 08:19
-
@sfstewman - There is not much time at compile time but will increase slightly. However, the benefits to be received is greater. Also, there is no need to make existing ones. – BLUEPIXY Jul 18 '12 at 08:22
-
that's a great answer. Surely it would be possible to do the same without boost with pure C then. – Jean-François Fabre Apr 19 '18 at 14:18
Yes and no. It's not simple, and not generally a good idea, but you can do it for finite, constant sizes, and for constant characters. There are many ways to do this with the C preprocessor. Here is one:
#define DUP(n,c) DUP ## n ( c )
#define DUP7(c) c c c c c c c
#define DUP6(c) c c c c c c
#define DUP5(c) c c c c c
#define DUP4(c) c c c c
#define DUP3(c) c c c
#define DUP2(c) c c
#define DUP1(c) c
#include <stdio.h>
int main(int argc, char** argv)
{
printf("%s\n", DUP(5,"-"));
printf("%s\n", DUP(7,"-"));
return 0;
}
It's not pretty, and only useful when you really want the string to be stored as static (constant) data. Both the n
and 'c' parameters to DUP
have to be a constants (they cannot be variables). The Boost.Preprocessor module has a lot of good information for how and when to (ab)use the C/C++ preprocessor like this. Although Boost is a C++ library, the preprocessor information is largely applicable to straight C.
In general, you're much better off doing this in normal C code:
/* In C99 (or C++) you could declare this:
static inline char* dupchar(int c, int n)
in the hopes that the compiler will inline. C89 does not support inline
functions, although many compilers offered (inconsistent) extensions for
inlining. */
char* dupchar(int c, int n)
{
int i;
char* s;
s = malloc(n + 1); /* need +1 for null character to terminate string */
if (s != NULL) {
for(i=0; i < n; i++) s[i] = c;
}
return s;
}
or, use memset
, as @Jack suggested.

- 5,589
- 1
- 19
- 26
-
+1, I thought the `DUP` macro was clever although I agree not a good idea. – Marlon Jul 18 '12 at 01:17
-
+1 for macro solution. but in `dupchar` function: `s` must be checked if is non-NULL value; I think that you could put a note about variable declaration into the first argument of loop-statement is C99 feature; is not a good idea take `c` as `char`. `int` must be used instead of. – Jack Jul 18 '12 at 02:09
-
@Jack: you're right about the NULL check, and I've fixed the function to comply with C89. Why would you fill a `char*` array with `int` values, though? – sfstewman Jul 18 '12 at 02:17
-
@TheMask: There's no guarantee that the compiler would inline a function that calls `malloc` and has `for` loop with variable range. – sfstewman Jul 18 '12 at 02:19
Not in C standard.You need to write your own implementation.
EDIT:
something like this:
#include <stdio.h>
#include <string.h>
#define REPEAT(buf, size, ch) memset(&buf, ch, size)
int main(void)
{
char str[10] = { 0 };
REPEAT(str, 9, '-');
printf("%s\n", str); //---------
return 0;
}

- 16,276
- 55
- 159
- 284