is this possible with #define?

I've been playing around with this idea for a few hours and can't get the code to work as expected. I'm not even sure if this is even possible. I'm probably overlooking something very simple but i just can't quite grasp it. (for fun) i'm trying to use #define to define $ as auto plus a space. So that i could declare a variable as $var and have it expand to auto var. I've tried a variety of different things and it doesn't seem to matter which way i try to trick it i still get some kinda error or another. I can get $ var to work no problems. Its getting the "invisible" space that is the issue. I'll post my latest miserable fail just for fun. any help would be great thanks.

1
2
3
4
5
6
7
  #define empty  
#define aut auto empty
#define $$(x) #x
#define $$ $$(aut) 
#define $ $$

 $ a=1;
No, it's not possible. The preprocessor tokenizes the input and will only match a complete token with the defined identifiers. If you don't put a space between the $ and the a then they become the single token $a, which does not match $. Similarly you can't define abc as something and expect it to replace the first half of abcdef.
ok so could you possibly help me use a function to make the #define dynamic so that lets say $ was a function call that returned the value of $ whatever followed as the token to be defined?
It's quite likely that the preprocessor is treating the input as a single token $foo, not a sequence of tokens $ and foo.

The mainstream preprocessors exhibit a great deal of implementation divergence. Maybe there exists an implementation that doesn't work this way. I don't know if such a preprocessor would be conforming or not off the top of my head.

I do know that $ is not in the basic source character set.
No. The preprocessor doesn't execute functions.

If you're talking about a function-like macro then I guess you could do this, but I don't see how it's any better than having a space after the $.

1
2
3
4
5
#define $(x) auto x

int main() {
    $(a) = 1;
}

Last edited on
after playing around with this some more im really starting to see the conundrum that the whole thing creates.
m really starting to see the conundrum that the whole thing creates.

There's no conundrum. It simply isn't possible to pull apart an identifier with the preprocessor (or, of course, the language itself). You would need to add your own pre-preprocessor step.
Topic archived. No new replies allowed.