Anders Lind
Anders Lind

Reputation: 4840

What does #x inside a C macro mean?

For example I have a macro:

#define PRINT(int) printf(#int "%d\n",int)

I'm somewhat aware of the outcome. But how come #int represent the whole thing?

I'm somewhat forget this detail. Can anybody kindly give me a hint?

Thanks!

Upvotes: 61

Views: 34570

Answers (5)

Dúthomhas
Dúthomhas

Reputation: 10093

This is kind of long for a comment, but it is worth noting that messing with the preprocessor is not always as simple as you would think.

The canonical way to do convert something to a string literal via the CPP is a macro pair that reads:

#ifndef STRINGIFY
    #define STRINGIFY(s) STRINGIFY_(s)
    #define STRINGIFY_(s) #s
#endif

You can then use it anywhere without error, such as in your example macro, which I will better name:

#define PRINTLN_INT(x) printf(STRINGIFY(x) "%d\n", x)

This definition is significantly more readable and won’t fail for magic raisins. (But it will still properly fail to compile if x is not convertible from an int.)


EDIT: Wait, this question is ancient. Why did it pop up in my feed?

Upvotes: 0

Aagam Sheth
Aagam Sheth

Reputation: 691

'#' is called a stringize operator. Stringize operator puts quotes around the parameter passed and returns a string. It is only used in a macro statements that take the arguments.

#include<stdio.h> 

#define stringLiteral(sl) #sl

int main()
{
   char StringizeOpreator = 'a'; 
   printf(stringLiteral(StringizeOpreator));
   return 0;
}

Here the stringLiteral macro takes the formal argument sl and returns #sl. Actual argument passed is StringizeOpreator variable. The return statement #sl has # operator, that puts quotes around the argument like "StringizeOpreator" and returns a string.

So the output of the above program is the name of the actual parameter StringizeOpreator rather than the value stored in the actual parameter passed.

output :
StringizeOperator

To learn more visit this link: Stringize Operator

Upvotes: 3

Jack Peking
Jack Peking

Reputation: 201

"#" can show the name of a variable, it's better to define the macro as this:

#define PRINT(i) printf(#i " = %d\n", i)

and use it like this:

int i = 5;
PRINT(i);

Result shown:

i = 5

Upvotes: 19

Karthik T
Karthik T

Reputation: 31962

That is a bad choice of name for the macro parameter, but harmless (thanks dreamlax).

Basically if i write like so

PRINT(5);

It will be replaced as

printf("5" "%d\n",5);

or

printf("5 %d\n",5);

It is a process called Stringification, #int is replaced with a string consisting of its content, 5 -> "5"

Upvotes: 8

metamatt
metamatt

Reputation: 14459

In this context (applied to a parameter reference in a macro definition), the pound sign means to expand this parameter to the literal text of the argument that was passed to the macro.

In this case, if you call PRINT(5) the macro expansion will be printf("5" "%d\n", 5); which will print 5 5; not very useful; however if you call PRINT(5+5) the macro expansion will be printf("5+5" "%d\n", 5+5); which will print 5+5 10, a little less trivial.

This very example is explained in this tutorial on the C preprocessor (which, incidentally, is the first Google hit for c macro pound sign).

Upvotes: 62

Related Questions