1

I would like to pass arguments into variables names using the macros.

For example, I would have this code :

#define FOO(I,J) double varIJiable

FOO(1,2);

And the preprocessor would output this :

double var12iable;
6
  • 2
    Read about ##. Commented Mar 12, 2020 at 12:54
  • out of curiosity: Why do you want to do this? If you have to type FOO(1,2); every single time you want to spell out the variable name, how is that better than writing double var12iable; in the first place? Commented Mar 12, 2020 at 12:57
  • I'm learning instruction level parallelism, so I'll have a bunch of double var00iable; double var01iable; double var10iable;...It it just to have a template so that I spend less time coding. Commented Mar 12, 2020 at 13:00
  • For easier searching, the ## operator is commonly called the "token pasting operator". Commented Mar 12, 2020 at 13:01
  • @truvaking it might cause less time coding but more time debugging. Being explicit is always good. Commented Mar 12, 2020 at 13:04

1 Answer 1

6

Use the ## operator to concatenate strings through the pre-processor

#define FOO(I,J) double var ## I ## J ## iable
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.