0
\$\begingroup\$

Library pps.h of PIC32 contains a pre-processor function that directs a peripheral's input/output to certain micro-controller pins.

The function is:

#define PinSelOutput(grp,pin,fn)   OUT_PIN_PPS##grp##_##pin = OUT_FN_PPS##grp##_##fn    /**<Pin Select output bit*/ 

What I cannot understand is the implementation of "##_" before "##pin". It glues the string part before "_" with "_", somehow.

Is there a table with special hashtag combinations that I could check?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Duplicate from Stack Overflow: The ## operator in C. This question is about pure C programming, it has nothing to do with PIC nor electronics. So you should be asking at Stack Overflow instead. Or rather, search on SO before asking, since it has been answered numerous times before. \$\endgroup\$
    – Lundin
    Commented Sep 17, 2018 at 11:37

1 Answer 1

2
\$\begingroup\$

Since you put 'token pasting' in your title, you clearly already have some idea what ## does -- it is called the "token pasting" operator, which is something you could google to get more information.

You should not think of "##pin" and "##_" as separate things to understand; just think of "##" as an operator that glues together the things on either side of it.

So for example, if the macro is called as PinSelOutput(1, 2, 3), it will expand as follows:

OUT_PIN_PPS1_2 = OUT_FN_PPS1_3 /**<Pin Select output bit*/

Because it will first replace "grp", "pin", and "fn" with the values passed into the macro, and then "##" will glue things together into a single token.

(You need "##" because you can't just say something like "OUT_PIN_PPSgrp_pin" -- that would be interpreted as a single "token" (i.e. a single word) and the parameter values would not be inserted.)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thank you for your reply. Now it makes sense. Yes I was thinking of "##pin" and "##_" as separate things. But now seems very straightforward. Thank you Mr Willen \$\endgroup\$
    – Sparky
    Commented Sep 16, 2018 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.