Help With #define

closed account (yvUR216C)
Hello,
I have a problem with #define macro.

I would get _DIGIT_ number x with the macro _DIGIT(x)

1
2
3
4
5
6
7
  #define _DIGIT_0 5
  #define _DIGIT_1 6
  #define _DIGIT_2 7
  #define _DIGIT_3 8
  #define _DIGIT_4 20
  
  #define _DIGIT(x) _DIGIT_x 
Last edited on
[Edit: See salem c's post]
You can't do that (Edit: in that way, see salem c's answer). _DIGIT_x is literally the character 'x' at the end, as part of the identifier. It doesn't know you're trying to use the other macros.

First, why do you need to do this? What problem are you actually trying to solve?
Using macros in arcane ways is usually frowned upon.
You could have a lookup table, something like:
1
2
3
4
5
int digit(int x)
{
    static int arr[] = {5, 6, 7, 8, 20};
    return arr[x];
}


Also, note that "_{capital letter}" identifiers are reserved for internal use, so it's best to avoid doing that.
Last edited on
Hello Sylvain41300,

First I would suggest removing the (_). It is very easy to conflict with a definition in a header file. Best to leave variable starting with 1 or 2 (_) to the header files.

In VS 2017 my error message says "_DIGIT': macro redefinition". Which tells me that "_DIGIT" is already defined in some header file.

When I removed the beginning (_)s from everything it compiled without any warnings or errors.

Your English is better than some I have seen.

Andy
You have to make use of the ## token pasting operator.
But it's not quite as simple as just using the ## operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>

#define _DIGIT_0 5
#define _DIGIT_1 6
#define _DIGIT_2 7
#define _DIGIT_3 8
#define _DIGIT_4 20

#define STR(x,y)  x ## y

#define _DIGIT(x) STR(_DIGIT_,x)

int main ( ) {
  printf("4 = %d\n", _DIGIT(4));
}
Thanks salem c for the example. I knew about ## but I thought it had to be used with string literals for some reason. It's actually more elegant than I thought it would be.
closed account (yvUR216C)
salem It's works but, when put a variable is doesn't work :
'_DIGIT_i' was not declared in this scope


1
2
3
4
5
6
7
8
9
10
11
12
#define _DIGIT_0 5
#define _DIGIT_1 6
#define _DIGIT_2 7
#define _DIGIT_3 8
#define _DIGIT_4 20

#define _DIGIT(x) _DIGIT_ ## x

int funct(int i)
{
    return _DIGIT(i);
}
#define macros are evaluated at compile time. For _DIGIT(i) to work, it would have to be evaluated at runtime because the program doesn't know the value of i until the program is running.

Why not just use the actual digit instead of the macro? Is the real problem something more complex?
closed account (yvUR216C)
It just an example, because I would put his macro in loop for.
I'm gonna abandon the idea and do Ganado said even though it uses RAM.
It would use RAM either way. Data isn't free, it's just that it would be stored in a static location instead if you did _DIGIT(3).
Edit: Actually, depending on usage, it may in fact just hardcode the numbers in the assembly instructions. But that couldn't be done for a for loop w/ i anyway.
Last edited on
this looks like you want an enum to me.
Topic archived. No new replies allowed.