1

I want to create this...

int main(void) {
  int i;

  for(i = 0; i < 10; i++){
    //Not an important Code
  }
  return 0;
}

in a fancier way, to create crazy ideas!
Is it possible to rewrite it like this?

#define A f
#define B or

int main(void) {
  int i;

  AB(i = 0; i < 10; i++){
    //Not an important Code
  }
  return 0;
}

I need to know if it's possible to create instrctions like a simple for composed by diferent #defines

  • 4
    Forget it. Trying to make a lazy implemetation for keywords is a going nowhere and makes your invented code hard to read. – Weather Vane Mar 06 '19 at 18:11
  • 2
    Avoid preprocessor magic; it makes code unreadable. You can concatenate tokens in the replacement text of a macro with `##` — `#define C(a, b) a ## b` and then `C(A, B)` would more or less do what you want; you just have to evaluate the expressions before concatenating: `#define E(a) a` — `#define C(a, b) E(a) ## E(b)` and `C(A,B)`. But that's repulsive, especially for a split keyword. – Jonathan Leffler Mar 06 '19 at 18:12
  • @JonathanLeffler I tryed your idea, and it doesn't work for me. Gcc responds with: pasting ")" and "E" does not give a valid preprocessing token #define C(a, b) E(a)##E(b) – Adria Arroyo Mar 06 '19 at 18:27
  • 1
    OK; as you can tell, I didn't test it. It's doable (the concatenation of tokens defined via macros); there are answers about how to do it (some written by me in times past). It isn't a good idea — that's the take home. (Working set of macros: `#define A f` — `#define B or` — `#define E(a, b) a ## b` — `#define C(a, b) E(a, b)` — `` — `C(A,B)` — output, `for`.) – Jonathan Leffler Mar 06 '19 at 18:29
  • @JonathanLeffler Can you please provide the link? :) – Adria Arroyo Mar 06 '19 at 18:32
  • https://stackoverflow.com/questions/1489932/how-to-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-arg; https://stackoverflow.com/questions/195975/how-to-make-a-char-string-from-a-c-macros-value – Jonathan Leffler Mar 06 '19 at 18:37
  • @JonathanLeffler You're awesome! – Adria Arroyo Mar 06 '19 at 19:04

1 Answers1

4

making

#define A f
#define B or
#define C(x, y) x##y
#define D(x,y) C(x,y)

then

D(A, B)(i = 0; i < 10; i++)

will be rewritten as

for (i = 0; i < 10; i++)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alinsoar
  • 15,386
  • 4
  • 57
  • 74