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
  • 9,449
  • 19
  • 60
  • 105

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
  • 74,635
  • 5
  • 99
  • 170
  • 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
  • 378,987
  • 63
  • 458
  • 590