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;
}
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.
Jyothi said:
10 years ago
1) Division.
2) Multiplication.
3) Add so definitely answer will 11.
2) Multiplication.
3) Add so definitely answer will 11.
Sunil said:
9 years ago
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).
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:
9 years ago
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.
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:
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?
Can anyone help me with this?
Nithish said:
9 years ago
If we convert it in postfix expression, then evaluate it will be 11.
Nithish said:
9 years ago
If we give (b + 2) then we get 25.
Manoj said:
8 years ago
#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.
#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:
8 years ago
If sqr(x) x*x then it should be 11.
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.
And if sqr(x) (x*x) then it will be 11.
Kunijiuma said:
5 years ago
#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.
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.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers