Preprocessor Operators

suggest change

# operator or stringizing operator is used to convert a Macro parameter to a string literal. It can only be used with the Macros having arguments.

// preprocessor will convert the parameter x to the string literal x
#define PRINT(x) printf(#x "\n")

PRINT(This line will be converted to string by preprocessor);
// Compiler sees
printf("This line will be converted to string by preprocessor""\n");

The compiler concatenates adjacent string literals, so the final printf() argument is one string literal with a newline at the end.

Preprocessor will ignore the spaces before or after the macro argument. So below print statement will give us the same result.

PRINT(   This line will be converted to string by preprocessor );

If the argument contains a character that must be escaped in a string literal, such as a double quote, the preprocessor inserts the escape.

PRINT(This "line" will be converted to "string" by preprocessor); 
// Compiler sees
printf("This \"line\" will be converted to \"string\" by preprocessor""\n");

## operator or Token pasting operator is used to concatenate two parameters or tokens of a Macro.

// preprocessor will combine the variable and the x
#define PRINT(x) printf("variable" #x " = %d", variable##x)

int variableY = 15;
PRINT(Y);
//compiler sees
printf("variable""Y"" = %d", variableY);

and the final output will be

variableY = 15

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents