Problem : 2


#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/

12 thoughts on “Problem : 2

  1. 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

  2. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s