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.
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.
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
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.
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.
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.
Sudip said:
1 decade ago
Guys the correct answer is 60, 6. I compiled this program in other version i.e. GNU GCC version 4.7.2 and other too. And in this compiler its showing 27, 6! absolutely wrong.
Himansu said:
1 decade ago
What was output of :
#define PRODUCT(x) ( x * x )
main( )
{
int i = 3, j, k ;
j = PRODUCT( i++ ) ;
k = PRODUCT ( ++i ) ;
printf ( "\n%d %d", j, k ) ;
}
#define PRODUCT(x) ( x * x )
main( )
{
int i = 3, j, k ;
j = PRODUCT( i++ ) ;
k = PRODUCT ( ++i ) ;
printf ( "\n%d %d", j, k ) ;
}
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.
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.
b=3.
Cube(3++) (3++*4++*5++).
So, 3*4*5 and finally b is returned as 6.
Sam said:
1 decade ago
Actually the behaviour is undefined.
In Turbo C k=++i + ++i; i.e., 2*i then i incremented two times.
In GCC k=++i + ++i; i incremented two times then 2*i;.
In Turbo C k=++i + ++i; i.e., 2*i then i incremented two times.
In GCC k=++i + ++i; i incremented two times then 2*i;.
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers