C Programming - C Preprocessor - Discussion

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;
}

[A]. 25
[B]. 11
[C]. Error
[D]. Garbage value

Answer: Option B

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


Poornima said: (Oct 3, 2011)  
The answer should have been 25. Why 11?

Yash said: (Dec 5, 2011)  
I think 3+2*3+2=25.
Can anyone explain about the priority of operators.

Amitjajoo said: (Dec 12, 2011)  
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: (Jan 12, 2012)  
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: (Jan 20, 2012)  
1st--------division
2nd--------multiplication
3rd--------add or subtract

Dineshkumargct said: (Mar 22, 2012)  
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: (Aug 30, 2012)  
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: (Sep 13, 2012)  
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: (Aug 19, 2013)  
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: (Mar 18, 2015)  
So, how we should decide?

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

Please give me answer :-).

Jyothi said: (Jan 12, 2016)  
1) Division.

2) Multiplication.

3) Add so definitely answer will 11.

Sunil said: (Apr 27, 2016)  
Hi, friends.

Mainly there are 2 types of the macro.

1. Object type
2. Function type.

Here we are considering the function type so the bracket after the SQR are the brackets of function, not the usual one.SQR(x)(x*x) = (3+2)*(3+2).

Thippesh_Madakari said: (Jun 24, 2016)  
At the compilation stage, only 3 is passed into the macro.

Because we are passing b+2, which will be calculated at the run time. During run time it will become 9+2=11.

Alli Mohammed said: (Jul 22, 2016)  
Write a program to calculate the valude of x, Where x=(3+4ab)3/2c^2.

Can anyone help me with this?

Nithish said: (Aug 27, 2016)  
If we convert it in postfix expression, then evaluate it will be 11.

Nithish said: (Aug 27, 2016)  
If we give (b + 2) then we get 25.

Manoj said: (Feb 17, 2017)  
#define SQR(x)(x*x)
#include<stdio.h>
int main()
{
int a, b=3;
a = SQR(b+a);
printf("%d\n", a);
return 0;
}

The output will be a=3, how is possible? Pease explain me.

Ravi said: (Jul 8, 2017)  
If sqr(x) x*x then it should be 11.

Akshay said: (Jul 14, 2017)  
If sqr(x) (x)*(x) then it should be 25.
And if sqr(x) (x*x) then it will be 11.

Kunijiuma said: (Feb 8, 2021)  
#include<stdio.h>
int SQR(int x){return x*x;}
int main()
{
int a, b=3;
a = SQR(b+2);
printf("%d\n", a);
return 0;
}

o/p:
25.

And the result should be 25 instead of 11.

Mangesh Kulkarni said: (Jun 26, 2021)  
The Rule of macros is Find and Replace.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.