5

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

2
  • 1
    Yes: stackoverflow.com/a/10542793/726361 and remember that character literals placed side-by-side are automatically concatenated. Commented Jul 18, 2012 at 2:14
  • 1
    On my keyboard you just have to hold the key down. No need for macros. Commented Jul 18, 2012 at 11:49

3 Answers 3

14

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

include boost for it? Personally, I prefer do a simple loop.
@TheMask: agreed. Why would you increase your compile time (a lot) just for this? Boost is overkill.
maybe 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.
@TheMask - I think there is a literal because I just want eventually.
@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.
|
7

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.

4 Comments

+1, I thought the DUP macro was clever although I agree not a good idea.
+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: 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?
@TheMask: There's no guarantee that the compiler would inline a function that calls malloc and has for loop with variable range.
5

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;
}

1 Comment

memset seems to be the cleanest solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.