C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 3)
3.
What will be the output of the program?
#include<stdio.h>
#define SQR(x)(x*x)

int main()
{
    int a, b=3;
    a = SQR(b+2);
    printf("%d\n", a);
    return 0;
}
25
11
Error
Garbage value
Answer: Option
Explanation:

The macro function SQR(x)(x*x) calculate the square of the given number 'x'. (Eg: 102)

Step 1: int a, b=3; Here the variable a, b are declared as an integer type and the variable b is initialized to 3.

Step 2: a = SQR(b+2); becomes,

=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .

=> a = 3+2 * 3+2;

=> a = 3 + 6 + 2;

=> a = 11;

Step 3: printf("%d\n", a); It prints the value of variable 'a'.

Hence the output of the program is 11

Discussion:
21 comments Page 1 of 3.

Poornima said:   1 decade ago
The answer should have been 25. Why 11?

Yash said:   1 decade ago
I think 3+2*3+2=25.
Can anyone explain about the priority of operators.

Amitjajoo said:   1 decade ago
How can explain answer for this question? It should answer 25 but you wrote answer 11 please exaplain me bease your explanation

a=3+2*3+2 , a=3+6+2

This step I could not understand, so please give explanation.

Mayank Sachan said:   1 decade ago
According to expression its (b+2)

So on squaring its (b+2)*(b+2)
as b is 3.

So its
(3+2)*(3+2)
now accrdng to BODMAS
bracket should be solved first so..
it becomes
(5)*(5)
which is equals to 25

Mubin said:   1 decade ago
1st--------division
2nd--------multiplication
3rd--------add or subtract

Dineshkumargct said:   1 decade ago
Here in the macros the function is specified as SQR(X) x*x .so SQR(b+2) it would evaluate (b+2) as 3+2*3+2 here according to BODMAS rule output=11.suppose if the macros is specified as SQR(X) (x) * (x). Then SQR(b+2) would be evaluated as (3+2) * (3+2)
which leads to (5)*(5). So the output is 25.

However since ()*() is not used the output 25 is not possible.

Munisha Sharma said:   1 decade ago
But according to me the x value will be calculated first and then its square root is to be found.

SQR(x) x*x will be SQR(b+2) that is SQR(5)as x=b+2=5

becomes a constant value of x and the square is 25.

Bhushan said:   1 decade ago
The SQR(x) function declared is directive statement. Means before compilation starts it replaces the statement in the program.

So SQR(x) is replaced by x*x and then executed
So in our question,
a = SQR(b+2)
In normal situation (b+2) should be evaluated as 5 then SQR(5) should be called but thats not that case because the SQR function is directive function and it is replaced by statements in the preprocessor stage.

So now the actual programs looks like this :

a = b+2 * b+2

Which evaluates to :
a = 3+2 * 3+2

Then priority first is for multiplication then to addition.

Sowmiya senya said:   1 decade ago
According to BODMAS concept,first we have to perform multiplication afterwards only we have to perform addition, So
3+2 * 3+2.

2*3 = 6(according to BODMAS concept [B-Brkt, O-of, D-division, M-multiplication, A-addition, S-subtraction]).

Now 3+6+2=11.

Phyu said:   1 decade ago
So, how we should decide?

In which situation we should use BODMAS or ordinary arithmetic operators?

Please give me answer :-).


Post your comments here:

Your comments will be displayed after verification.