Monday 26 June 2017

Write a macro PRINT for printf


The preprocessor ## operator is token concatenation. But,  #x makes the contents of makes a literal string the same as "x",
x##y---->token concatenation operator=> xy
#x---->Stringizing operator=> "x" 
 
#define PRINT(v1,v2,v3) printf("\n" #v1 "=%d" #v2 "=%d" #v3 "=%d", v1, v2, v3);

int main()
{
 int x=4, y=4, z=5; 
 int a=1, b=2, c=3;
 PRINT(x,y,z); 
 PRINT(a,b,c);
}

such that it outputs

x=4 y=4 z=5
a=1 b=2 c=3

No comments:

Post a Comment