C Programming - C Preprocessor - Discussion
Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
Answer: Option
Explanation:
The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.)
Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3.
Step 2: a = CUBE(b++); becomes
=> a = b++ * b++ * b++;
=> a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement.
=> a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6)
Step 3: printf("%d, %d\n", a, b); It prints the value of variable a and b.
Hence the output of the program is 27, 6.
Discussion:
41 comments Page 3 of 5.
Shreya said:
7 years ago
@All.
Different compilers are giving different outputs!
GCC gave 60,6
Turbo C gave 27,6
Which is the correct answer? Please explain it.
Different compilers are giving different outputs!
GCC gave 60,6
Turbo C gave 27,6
Which is the correct answer? Please explain it.
Ahmed Salah said:
9 years ago
When I compiled and run the above code using Code::Blocks IDE, the result was 60,6 which is not exist in the options.
What can I do?
What can I do?
Swasti said:
1 decade ago
Actually here post increment therefore while doing this
a = CUBE(b++);
>>>a=b++*b++*b++;
a=3*3*3;
b=3+1+1+1;
Therefore 6.
a = CUBE(b++);
>>>a=b++*b++*b++;
a=3*3*3;
b=3+1+1+1;
Therefore 6.
Pankaj said:
9 years ago
Given answer is wrong the correct answer will be 60, 6.
Because b++ * b++ *b++ will be 3*4*5 which is 60 and b will increase to 6.
Because b++ * b++ *b++ will be 3*4*5 which is 60 and b will increase to 6.
Travis said:
1 decade ago
Correct answer is 60 6, because CUBE (b++) is replaced by (b++*b++*b++) and therefore (3*4*5) = 60 while b increments three times.
Nitish said:
9 years ago
Till now I am knowing increment operator can increase the value by '1' only in one step then, how "b" increased to 6?
Phaneendra said:
1 decade ago
I don't understand of 'b' value ?
how is it possible tell me please sir.
i expected 'b' value is only 3.
but answer is wrong.
how is it possible tell me please sir.
i expected 'b' value is only 3.
but answer is wrong.
Rajesh said:
1 decade ago
I also think that, value of b will be 5. Since Increment (b++) will start from 3 not from 4 (If it will ++b then b=6).
Mangusta said:
1 decade ago
@Prashant.
I got that output in TurboC, then I re-launched it and compiled again, and this time it showed 27, 6.
I got that output in TurboC, then I re-launched it and compiled again, and this time it showed 27, 6.
Doaa said:
10 years ago
I think it will be like this 3+1*3+2*3+3
3+3+2*3+3
8*3+3
24+3
= 27
Start with multiply then add.
3+3+2*3+3
8*3+3
24+3
= 27
Start with multiply then add.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers