Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange C macro syntax (#var)

Tags:

c

syntax

macros

What does the # symbol mean when used as a variable prefix in a #define macro?

For example,

#define my_setopt(x,y,z) _my_setopt(x, 0, config, #y, y, z)
like image 625
An̲̳̳drew Avatar asked Dec 22 '22 09:12

An̲̳̳drew


2 Answers

It's the Stringizing Operator, which converts macro parameters to string literals.

So in your example:

my_setopt(1, 2, 3)

would expand to:

_my_setopt(1, 0, config, "2", 2, 3)
like image 171
RichieHindle Avatar answered Dec 27 '22 07:12

RichieHindle


# quotes the expression. For example:

#define SHOW(BAR) printf("%s is %d\n", #BAR , BAR)
SHOW(3+5);  // prints: 3+5 is 8
like image 29
sepp2k Avatar answered Dec 27 '22 07:12

sepp2k