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 2 of 5.
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?
#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?
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.
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.
Nti said:
8 years ago
Yes, 60 6 is right. I agree with it.
Nidhi said:
9 years ago
I did not understand this output. Can anyone help me?
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.
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?
Laxmikant said:
9 years ago
The answer is 60, 6.
a = cube(b++) means initially b = 3 then ++ makes it 4 and again ++ makes it 5 which equals to 3 x 4 x 5 = 60. Which is the value of a, and now at last b becomes 6.
a = cube(b++) means initially b = 3 then ++ makes it 4 and again ++ makes it 5 which equals to 3 x 4 x 5 = 60. Which is the value of a, and now at last b becomes 6.
Vinitha said:
9 years ago
The answer is 27,6.
The compiler in post increment takes b as 3 and the macro function replace it by 27.
The increment operator works for b from left to right,
First b++ -> b=4.
Second b++ ->b=5.
Third b++ -> b==6.
The compiler in post increment takes b as 3 and the macro function replace it by 27.
The increment operator works for b from left to right,
First b++ -> b=4.
Second b++ ->b=5.
Third b++ -> b==6.
Ratan Lal said:
9 years ago
I got the answer on GCC is 60, 6.
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers