in DS edited by
771 views
0 votes
0 votes
#include <stdio.h>
#define foo(a,b) #b
int main(void) {
    int a=10,b=15,ab=20;
    // your code goes here
    printf("%d",ab+foo(a,b));
     return 0;
}


 
What is the output?

in DS edited by
771 views

2 Answers

1 vote
1 vote

Output is some random integer
ab is integer but foo(a,b) is string string + integer = God knows
Thing is    #define foo(a,b) #a     
means you are converting argument a into string   # here is stringify operator  
e.g : if u write  printf("%s",foo(ohyeah,b));
this will make a = ohyeah in  #define foo(a,b) #a    
and then #a  = #ohyeah    which means ohyeah is now a string which will replace foo(ohyeah,b)

I hope you got this if not lemme know :)

edited by
by

8 Comments

@aehkn, if d data type of macro is not specified then it is treated as a string type. Is it?
0
0
# is used for concatenation purpose.rt?
0
0
May be it print some ASCII value of string after concatention

I didn't get it

I run this program and output is 134513988

Can you elaborate more
1
1
Actually it will give undefined behaviour
# is stringification operator.
and when it will execute ab+"b"
for some difference in pasting it will give different result
0
0
edited by
ab=20 and "b" gives a pointer value.and  for this pointer value it will give undefined behaviour
0
0
That means #b returns a pointer value of b it isn't return 'b' right?
1
1
it returns string means base address of string is returned
In any place where string is returned or it exist it means we are reffering its base address
in above scenario  ab + "a"  for %d  means ab + base adr(a)
2
2
Okay!
0
0
0 votes
0 votes
It should be a#b rather than #b acc to me.# is used to concatenate a & b.So it should be like 1015+20= 1035.Plz correct me if I am wrong.

Related questions