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,517
  • 1
  • 41
  • 46
sj755
  • 3,944
  • 14
  • 59
  • 79
  • 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
  • 608
  • 2
  • 7
  • 20
Alok Save
  • 202,538
  • 53
  • 430
  • 533
10

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

sepp2k
  • 363,768
  • 54
  • 674
  • 675
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,495
  • 10
  • 50
  • 92
  • 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
  • 9
    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