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 1 of 5.
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.
Srinivas said:
1 decade ago
Here how 'b' value became six can you explain in detail?
Ankur said:
1 decade ago
According to Dennis Ritchie any variable can't be incremented more thar once in a single statement as in the problem and signal error when run in ASCII compiler.
Shweta said:
1 decade ago
In step 2, I did not get that why cube (b++) is used. It could also b written as cube (b). Writing b++ means we are incrementing it then we cube it and not even understood the output value of b.
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.
Neetesh said:
1 decade ago
If we increment the value of b as pre increment then the answer is different
a=CUBE(++b) then the value of
a=150 and b=6
How is it possible? Please explain this compile in GCC.
a=CUBE(++b) then the value of
a=150 and b=6
How is it possible? Please explain this compile in GCC.
Mayank Sachan said:
1 decade ago
If b is incremented to 6 then in that case cube of it should also be incremented like 3*4*5.
Pushkar bhauryal said:
1 decade ago
@Neetesh.
In pre increment CUBE(++b); is proceed as
b=3 initial value:
For first x in cube value ++b =4
For second X value is ++4=5
For third x value is ++5=6
Now the output=4*5*6=150
In pre increment CUBE(++b); is proceed as
b=3 initial value:
For first x in cube value ++b =4
For second X value is ++4=5
For third x value is ++5=6
Now the output=4*5*6=150
Prashant said:
1 decade ago
Actual answer is (b++*b++*b++)
3*4*5
60 and b=6;
3*4*5
60 and 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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers