1
#define var(N) variable ## N

var(1) got variable1

I want to get variable, how can I define the macro?

var( ) works, but it always give a warning. is there any other solutions?

camino
  • 10,085
  • 20
  • 64
  • 115

2 Answers2

2

In fact the version that you presented always needs a non-empty argument for N. If you have a modern C compiler you can use this construct:

#define var(...) variable ## __VA_ARGS__

This accepts empty arguments and you should be fine.

"modern" here means C as of 1999.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Notes on this answer: you must be using a C99 compiler (-std=c99 in gcc) or at least one that supports variadic macros. ## is the concatenation operator in the C preprocessor. – Tyler Durden Feb 27 '15 at 16:47
-2

Don't use the argument inside the macro:

#define var(n) variable
unwind
  • 391,730
  • 64
  • 469
  • 606