2

While I was attempting a technical exam as part of a recruitment process I came across a question which has macros as shown below:

#define random 2,4

what does this mean? How can I define two values with a comma for a single macro?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    You do not define "two values" but you define one replacement text. You can define any text that is used to replace the macro. What that text means is fully dependent on where you use that macro. – Gerhardh May 26 '21 at 14:50
  • 1
    Whenever the `random` macro is used it will be replaced by `2,4`. Macros are pretty much a textual search-replace. – Some programmer dude May 26 '21 at 14:50
  • `int a[3] = { random };` ... `int x, y = random;` ... `pow(random) == 16` ... – pmg May 26 '21 at 14:52
  • 3
    It's not a great usage of macros... – al3c May 26 '21 at 14:53
  • 1
    @pmg that expands to `int x, y = 2,4;`. which is a syntax error. – Wyck May 26 '21 at 14:57
  • Even once you got to understand what it does - *please, please*, don't make/use macros like this. – Eugene Sh. May 26 '21 at 15:07
  • What you've got so far is about the best we can do without more context. Did this exam show you `RANDOM` being used? – user4581301 May 26 '21 at 15:07
  • @user4581301 Just two random numbers, obviously. [xkcd](https://imgs.xkcd.com/comics/random_number.png) – Aykhan Hagverdili May 26 '21 at 15:08
  • Just like when you're testing you need to see if your program does the wrong thing with unexpected inputs, sometimes you put bad ideas into a hiring test to see what the prospective programmer does with bad inputs. – user4581301 May 26 '21 at 15:09
  • @AyxanHaqverdili If I'm reading you right, `return random;` would be an interesting test of a new programmer's understanding of macros AND the comma operator. And if I'm not, it would still be interesting. – user4581301 May 26 '21 at 15:12
  • Right, @Wyck, my bad. I meant `int x, y = (random);` – pmg May 26 '21 at 15:23
  • @user4581301 yeah it was shown inside a if statement if i remember rightly. I think question was just trying to confuse us with multiple values. Thanks everyone for the answers I now understand it. – Faris Kamal Kakkengal May 26 '21 at 16:09
  • Thanks all . I got it now – Faris Kamal Kakkengal May 26 '21 at 16:09
  • @pmg your revised version expands to `int x, y = (2,4);` which leaves `x` uninitialized and assigns `y` a value of `4`. discarding the value of `2` according to the rules of [how the comma operator works](https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work). – Wyck May 26 '21 at 18:37

1 Answers1

6

It literally replaces all occurrences of the token random with 2,4. The pre-processor is just a glorified text-replacement tool.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 1
    Does not replace text in C comments. Aslo replacement does not apply to character constants either like the poor code of `'random'`. Perhaps other exceptions too. – chux - Reinstate Monica May 26 '21 at 15:17
  • @chux-ReinstateMonica whether or not the comments get replaced doesn't matter though. I will add the character constants. Thank you for the feedback! – Aykhan Hagverdili May 26 '21 at 15:19
  • "whether or not the comments get replaced doesn't matter though." --> Consider if a `random` was `*/` . Replacement would end a `/* random */` early. But it does not as there was no replacement. Perhaps all comments are replaced with a `' '` first. – chux - Reinstate Monica May 26 '21 at 16:10
  • Thanks everyone for the replies, I got it now. The question may be designed to confuse who is not well versed about macros. – Faris Kamal Kakkengal May 26 '21 at 16:11
  • @chux-ReinstateMonica good point. Macros aren't allowed to start or end a comment iirc though. – Aykhan Hagverdili May 26 '21 at 16:20
  • 1
    If you said something like "…all occurrences of the token `random`…" you would cover the bases nicely. Character constants and string literals are tokens in their own right and include the quotes, so they are never the token `random`. Comments are replaced by white space so they are never the token `random` either. If there was a macro `#define redefine(deterministic, random) …random…` then neither `random` is replaced by `2,4` as they are parameters to the macro. There are also variations on `#define macro(a,b) a ## b` plus an invocation `macro(ran,dom)` which generates the token `random`. – Jonathan Leffler May 27 '21 at 01:37
  • @JonathanLeffler I didn't know those. I rephrased the answer accordingly. Thank you for the feedback! – Aykhan Hagverdili May 27 '21 at 09:06