4

What does the pound sign indicate in this line of code?

#define CONDITION(x)    if(!(x)){ HandleError(#x,__FUNCTION__,__LINE__);return false;}

This is how it is being called:

CONDITION(foo != false);
Rikesh
  • 26,156
  • 14
  • 79
  • 87
marsh
  • 2,592
  • 5
  • 29
  • 53

1 Answers1

14

A single # before a macro parameter converts it to a string literal.

#define STRINGIFY(x) #x
STRINGIFY(hello)   // expands to "hello"

In your example, the string would be "foo != false", so that the error message shows the code that was being tested.

A double ## between two tokens within a macro combines them into a single token

#define GLOM(x,y) x ## y
GLOM(hello, World) // expands to helloWorld
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • can you elaborate on the double pound example. – Mohit May 11 '15 at 06:15
  • @ahujamoh: As I said, it combines two tokens to make a single token (in the example, `hello` and `World` are combined to make `helloWorld`). I'm not sure how I can elaborate further, but your introductory book should describe it in detail. – Mike Seymour May 11 '15 at 10:10