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 1 of 5.

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

Dhananjay Khairnar said:   4 years ago
@All.

#include<stdio.h>
#define CUBE(x) (x*x*x)
int main(int argc, char const *argv[])
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}

I got output 60, 6 from the GCC compiler.

Correct me If my coding is wrong.
(2)

Madan said:   5 years ago
The answer is wrong.

When we substitute the macros it becomes
(b++ * b++ * b++).

Intitially b is 3 and substitute in the above expression.
(3 * 4 * 5). It is post incre, So the final value in the b is 6.
So, the output is 60 and 6.
(2)

Kunijiuma said:   5 years ago
The result should be 27, 4.

Try this code:
#include<stdio.h>
int CUBE(x) {return x*x*x;}

int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
(1)

Richik said:   6 years ago
The explanation is wrong. The answer will be 60,6 in macro cube receives value 3. i.e
b=3.

Cube(3++) (3++*4++*5++).

So, 3*4*5 and finally b is returned as 6.

Md Wasim Akram said:   7 years ago
In this question, the actual answer is a=60 and b=6.

I compiled and run of this code in Codeblock. Our answer is right theoretically as well as practically but this option is available in this question.

According to C-Language in postfix increment, first we assign the variable's value then we increment it and it is happening in left to right direction.

Soln- a=b++(3)*b++(4)*b++(5)= 3*4*5= 60 and in last we will increase last value of b++(5) with 1 then we found b=6 , so we get final answer is a=60 and b=6 .

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.

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

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

Amrendra said:   7 years ago
@Chandu.

At first ++b means b=4,
Then * operator comes,then ++b means b=5,
Then multiplication is done i.e 5*5=25,
Then *, then ++b means b=6,
Then finally 25*6=150, a=150 and b=6.

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

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


Post your comments here:

Your comments will be displayed after verification.