How can I do this with macros:

I want to have an error message that prints the variable name, and its current value.

so far I have:

1
2
3
4
5
6
7
#define describe(a, b) cout << "param_a = " << a << " and param_b = " << b << endl
//some code goes here
//and then later
int x = 5, y = 6;
#define param_a a
#define param_b b
describe(x, y);

which outputs:

param_a = 5 and param_b = 6;

I want it to read:

x = 5 and y = 6;
Thanks, that really helps alot.

But I was reading that macros when encounted expand any macros withim them. So why isn't param_a and param_b being expanded? Just because they're inside quotations?
Just because they're inside quotations?

Macros don't expand if they're within quotations. Also, param_a and param_b are defined after describe, so they wouldn't be expanded anyway.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

#define HELLO NOT_HELLO

int main()
{
     int NOT_HELLO = 5;

     cout << " HELLO " << ": " << HELLO << endl;

     return 0;
}
Last edited on
Topic archived. No new replies allowed.