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

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 :-).

Alli mohammed said:   9 years ago
Write a program to calculate the valude of x, Where x=(3+4ab)3/2c^2.

Can anyone help me with this?

Akshay said:   8 years ago
If sqr(x) (x)*(x) then it should be 25.
And if sqr(x) (x*x) then it will be 11.

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

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

Jyothi said:   10 years ago
1) Division.

2) Multiplication.

3) Add so definitely answer will 11.

Nithish said:   9 years ago
If we convert it in postfix expression, then evaluate it will be 11.

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

Mangesh Kulkarni said:   4 years ago
The Rule of macros is Find and Replace.
(3)

Ravi said:   8 years ago
If sqr(x) x*x then it should be 11.


Post your comments here:

Your comments will be displayed after verification.