+ 1

what is the output of this and explain how it works??

#include <stdio.h> #define cube(x)(x*x*x) int main() { int a,b=3; a=cube(b++); printf("%d %d",a,b); return 0; }

21st Oct 2020, 6:18 PM
mohana hemanth
mohana hemanth - avatar
8 Answers
+ 1
no that is wrong answer @HBhZ_C anyway thanks for answering
21st Oct 2020, 6:32 PM
mohana hemanth
mohana hemanth - avatar
+ 1
The preprocessor substitute every occurrence of cube(x) with x*x*x So the program actually runs: a = b++ * b++ * b++; Wich means 3 * 4 * 5 = 60 While b ends up being 6
21st Oct 2020, 6:54 PM
Davide
Davide - avatar
+ 1
mohana hemanth as Martin said that's a bad piece of code with unpredictable behaviour. So it's perfectly fine that one of the possible output is 27. But the answer is not 27 nor 60, It's what Martin said
22nd Oct 2020, 11:47 AM
Davide
Davide - avatar
0
This code will call the cube function with 3 as param which returns 3*3*3 =>27 .Then a will be 28 and b will be 3.printf function will be called and will disply a and b value .output 28 3 This is my simple thinking and not tested Are i right?
21st Oct 2020, 6:27 PM
HBhZ_C
HBhZ_C - avatar
0
What about a value Davide
21st Oct 2020, 6:58 PM
mohana hemanth
mohana hemanth - avatar
0
mohana hemanth a is obviously 60
21st Oct 2020, 7:09 PM
Davide
Davide - avatar
0
Davide answer for a is 27
22nd Oct 2020, 4:26 AM
mohana hemanth
mohana hemanth - avatar