24

I'm taking a look at an application that defines a large set of constant arrays. What really confuses me is the use of two pound signs next to each other in a macro. For example:

#define r0(p,q,r,s) 0x##p##q##r##s

What do those two pound signs mean?

a3f
  • 8,227
  • 1
  • 36
  • 42
sj755
  • 3,774
  • 14
  • 56
  • 78
  • 10
    `#` = hash. `£` = pound sign. – TRiG Jan 30 '12 at 10:52
  • 18
    @TRiG I'm not British... http://en.wiktionary.org/wiki/pound_sign – sj755 Jan 31 '12 at 07:28
  • Possible duplicate of [What are the applications of the ## preprocessor operator and gotchas to consider?](https://stackoverflow.com/questions/216875/what-are-the-applications-of-the-preprocessor-operator-and-gotchas-to-conside) – phuclv Aug 17 '18 at 05:05

3 Answers3

29

## provides a way to concatenate actual arguments during macro expansion.

Bretsko
  • 409
  • 2
  • 5
  • 18
Alok Save
  • 196,531
  • 48
  • 417
  • 525
10

## concattenates symbols. So for example if the value of p is ab, 0x##p would become 0xab.

sepp2k
  • 353,842
  • 52
  • 662
  • 667
5

Als and sepp2k give correct answer.

However I would like to add, that this macro seems to be completely unnecessary.

unsigned int value = r0(b,e,a,f);

can be replaced by better and shorter:

unsigned int value = 0xbeaf;
noisy
  • 6,137
  • 10
  • 49
  • 88
  • 2
    If it's being used as part of a larger macro, it would be cleaner to read `r0(p,q,r,s)` instead of `0x##p##q##r##s` all over the place. – StilesCrisis Jan 30 '12 at 05:54
  • @StilesCrisis: No, if it is used as part of a large macro, it would be cleaner to rewrite the code without any macros. – Lundin Jan 30 '12 at 07:27
  • 8
    C is hardly a perfect language--sometimes a macro is still the best choice. Without knowing more about the OPs' code it's hard to say. – StilesCrisis Jan 30 '12 at 09:07