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;
}
9, 4
27, 4
27, 6
Error
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 4 of 5.

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.

Pankaj said:   1 decade ago
Your answer in totally wrong. You can check by compiling. It is 60, 6.

Chandra shekar said:   7 years ago
The answer is 60, 6.

Because (b++*b++*b++),
3*4*5,
60 and b=6;

Ahmed said:   3 years ago
I think the right answer will be 60, 6
Because 3*4*5 = 60.
(9)

Srinivas said:   1 decade ago
Here how 'b' value became six can you explain in detail?

Kuldeep Saroj said:   7 years ago
Value of A and B must be as below:

a= 3*4*5=60;
b=6;

Nidhi said:   9 years ago
I did not understand this output. Can anyone help me?

Prashant said:   1 decade ago
Actual answer is (b++*b++*b++)

3*4*5


60 and b=6;

Nti said:   8 years ago
Yes, 60 6 is right. I agree with it.

Ratan Lal said:   9 years ago
I got the answer on GCC is 60, 6.


Post your comments here:

Your comments will be displayed after verification.