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

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.

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

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

Farooq Ahmed said:   8 years ago
Answer Will Be 60,6
How means;

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 variable b id initialized to 3.

Step 2: a = CUBE(b++); becomes

=> a = b++ * b++ * b++; Here 3++ * 3++ * 3**; Therefore because post increment first time it will be 3 then become 4 then become 5

=> a = 3 * 4 * 5;

=> a = 60; Here, 60 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 60, 6.

Chandu said:   8 years ago
#include<stdio.h>
#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(++b);
printf("%d, %d\n",a,b);
}

How answer is coming 150, 6?

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

Because (b++*b++*b++),
3*4*5,
60 and 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.

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

a= 3*4*5=60;
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.

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 .


Post your comments here:

Your comments will be displayed after verification.