#include <stdio.h> #define prod(a,b) a*b int main () { int x = 3, y = 4; printf("%d\n", prod (x+1, y-1)); return 0; } // What's the output ? [Do not execute / run] // Source : http://www.youngprogrammer.com/

Advertisements

Skip to content
# Problem : 2

##
12 thoughts on “Problem : 2”

### Leave a Reply

অন্তহীন ছুটে চলা, অবিরাম জীবনের অর্থ খুজে ফেরা

#include <stdio.h> #define prod(a,b) a*b int main () { int x = 3, y = 4; printf("%d\n", prod (x+1, y-1)); return 0; } // What's the output ? [Do not execute / run] // Source : http://www.youngprogrammer.com/

Advertisements

%d bloggers like this:

12

12

have a resemblance with this one, https://tausiq.wordpress.com/2009/10/14/problem-x1/

#define prod(a,b) a*b

if you call prod(a, b) with 2 values ‘a’ and ‘b’, it will return you a * b. simple, isn’t it ?

no, it isn’t. The real thing is bit more complexer.

Here, we passes the value of, a = x + 1

and, b = y – 1

a * b = (x + 1) * (y – 1)

a * b = (3 + 1) * (4 – 1)

a * b = 4 * 3 = 12 … wrong !!

this actually works like this,

a * b = x + 1 * y – 1

we know, multiplication has applied first before addition & subtraction.

so, a * b = 3 + 1 * 4 – 1

a * b = 3 + 4 – 1

a * b = 7 – 1 = 6 !!

Correct Output : 6

Why a * b = x + 1 * y – 1 ????

Why not a * b = (x + 1) * (y – 1) ???

Macro works like this

#define prod(a,b) a*b

here, a = x + 1

and, b = y – 1

so, a * b = x + 1 * y – 1

12

This answer would be: 6

Because,

a*b=x+1*y-1

a*b=3+1*4-1

a*b=3+4-1

a*b=6

answer would be 6!!!

6

6

12

output will be : 6

because prod (x+1, y-1) will be replaced as x+1*y-1 = 3+1*4-1 = 3+4-1=6