-2

I need to implement something like this but using CMacro:

int a = //some constant;
int x = //constant
int c = //constant
int y = a*x+b;

I've tried something like:

#define A 3
#define X 6
#define B 8
#define Y ((A)*(X) + (B))

However if I use the flag -E in the gcc it shows me 3*6 + 8.

Isn't there any way to perform integer arithmetic using CMacro, before the substitution I mean?

My purpose would be something like, just a piece of code...:

#define func_impl(NY,NX,R) \
void func_#NY_#NX(int* y, int* x) { \ //x size NX, y size NY
   int tmp[2*NX - R]; \
   for(int i = 0; i < 2*NX - R; i++) tmp[i] = 0; \
   //other processing operations... \
 }

I know both NX, NY and R a priori, although they're generated by another program. So basically I want to avoid to let the program P1 to generate too many macro constants if possible.

user8469759
  • 2,522
  • 6
  • 26
  • 50
  • Is there a reason why you need macros in the first place? Why can't you write one generic function, or alternatively use an array of function pointers? Function-like macros that generate functions are almost certainly the wrong solution to any known programming problem. – Lundin May 16 '16 at 10:54
  • Also, this `#define Y A*X + B` is a common beginner bug, which in turn suggests that you should stay away from macros entirely, for now. – Lundin May 16 '16 at 10:56
  • ^^ yes, until you have more experience. When you do have the experience, you will know to stay away from macros by yourself. – Martin James May 16 '16 at 10:59
  • @Lundin, It's a long story why I need them, I don't get the "beginner" bug. There's no bug in my example, because it works, I just wanted to show you both my effort and what I want to achieve. – user8469759 May 16 '16 at 10:59
  • 1
    @lukkio it's either doing what you want, in which case there is no problem at all and you did not need to post anything, or it's not doing what you want, in which case it's a bug. In this case, it seems that you do not appreciate the operation of the preprocessor as a text handler, and so the bug is in an app running on your brain:) – Martin James May 16 '16 at 11:04
  • 2
    Remember to parenthesize expressions in macros (`(A*X + B)`). In case one day you're clever and calculate `5 * Y`. – Antti Haapala -- Слава Україні May 16 '16 at 11:04
  • 2
    @AnttiHaapala `((A)*(X) + (B))` preferably – M.M May 16 '16 at 11:20
  • @M.M yeah preferably, but minimally, given the `#definitions` above – Antti Haapala -- Слава Україні May 16 '16 at 11:21
  • @lukkio In the pre-processor chapter of any C programming book, there will most likely be a very similar example of a bug, to show such macros will break because of operator precedence. The same book will teach you to use parenthesis around the macro expression, and also around every parameter of a function-like macro. – Lundin May 16 '16 at 11:24
  • Guys... just to be clear, I'm aware that the preprocessor "barely" performs "text substitution" however I just wanted to be sure whether or not there's some other way around. Again... sorry for my poor use of the parenthesis but for the minimal example I wanted to show the use of parenthesis doesn't affect the result, likewise the content of my question. I'm aware also that massive use of macro is not a good programming practice, but I'm forced to do that. – user8469759 May 16 '16 at 12:01

2 Answers2

2

However if I use the flag -E in the gcc it shows me 3*6 + 8.

That's actually fine, because any decent compiler will perfom constant folding on such expression, so you'll end up with 26 within machine code.

Other than that, you might try using an static inline function, which has advantage of being type-safe. Macros are pretty error-prone and are rather difficult to debug.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
1

No, macros only serve as textual replacement, nothing else.

The only place where the preprocessor evaluates expressions is in its own #if/#else construct. But there it might actually be a bit different than you think at a first look, because the types for integers are always [u]intmax_t.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177