# C Programming - C Preprocessor - Discussion

### Discussion :: 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;
}
``````

 [A]. 9, 4 [B]. 27, 4 [C]. 27, 6 [D]. Error

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.

 Phaneendra said: (Dec 28, 2010) I don't understand of 'b' value ? how is it possible tell me please sir. i expected 'b' value is only 3. but answer is wrong.

 Srinivas said: (Apr 12, 2011) Here how 'b' value became six can you explain in detail?

 Ankur said: (Jun 30, 2011) 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.

 Shweta said: (Jul 25, 2011) In step 2, I did not get that why cube (b++) is used. It could also b written as cube (b). Writing b++ means we are incrementing it then we cube it and not even understood the output value of b.

 Swasti said: (Aug 26, 2011) Actually here post increment therefore while doing this a = CUBE(b++); >>>a=b++*b++*b++; a=3*3*3; b=3+1+1+1; Therefore 6.

 Neetesh said: (Oct 2, 2011) 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.

 Mayank Sachan said: (Jan 12, 2012) If b is incremented to 6 then in that case cube of it should also be incremented like 3*4*5.

 Pushkar Bhauryal said: (Jun 11, 2012) @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

 Prashant said: (Jun 26, 2012) Actual answer is (b++*b++*b++) 3*4*5 60 and b=6;

 Mangusta said: (Aug 19, 2013) @Prashant. I got that output in TurboC, then I re-launched it and compiled again, and this time it showed 27, 6.

 Naveen Kumar Chaudhary said: (Sep 20, 2013) @Mangusta and @Prashant .You both are correct. The answer to this question is "undefined behaviour". Read this Question from the C FAQ. Q: How can I understand complex expressions like i++ + ++i,i=i++,etc. and avoid writing undefined ones? What's a "sequence point"? A: A sequence point is a point in time at which the dust has settled and all side effects which have been seen so far are guaranteed to be complete. The sequence points listed in the C standard are: at the end of the evaluation of a full expression (a full expression is an expression statement, or any other expression which is not a subexpression within any larger expression); at the ||, &&, ?:, and comma operators; and at a function call (after the evaluation of all the arguments, and just before the actual call). The Standard states that Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. These two rather opaque sentences say several things. First, they talk about operations bounded by the "previous and next sequence points"; such operations usually correspond to full expressions. (In an expression statement, the "next sequence point" is usually at the terminating semicolon, and the "previous sequence point" is at the end of the previous statement. An expression may also contain intermediate sequence points, as listed above.) The first sentence rules out both the examples i++ * i++ and i = i++ from questions 3.2 and 3.3--in both cases, i has its value modified twice within the expression, i.e. between sequence points. (If we were to write a similar expression which did have an internal sequence point, such as i++ && i++ it would be well-defined, if questionably useful.) The second sentence can be quite difficult to understand. It turns out that it disallows code like a[i] = i++ from question 3.1. (Actually, the other expressions we've been discussing are in violation of the second sentence, as well.) To see why, let's first look more carefully at what the Standard is trying to allow and disallow. Clearly, expressions like a = b and c = d + e which read some values and use them to write others, are well-defined and legal. Clearly, [footnote] expressions like i = i++ which modify the same value twice are abominations which needn't be allowed (or in any case, needn't be well-defined, i.e. we don't have to figure out a way to say what they do, and compilers don't have to support them). Expressions like these are disallowed by the first sentence. It's also clear [footnote] that we'd like to disallow expressions like a[i] = i++ which modify i and use it along the way, but not disallow expressions like i = i + 1 which use and modify i but only modify it later when it's reasonably easy to ensure that the final store of the final value (into i, in this case) doesn't interfere with the earlier accesses. And that's what the second sentence says: if an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written. This rule effectively constrains legal expressions to those in which the accesses demonstrably precede the modification. For example, the old standby i = i + 1 is allowed, because the access of i is used to determine i's final value. The example a[i] = i++ is disallowed because one of the accesses of i (the one in a[i]) has nothing to do with the value which ends up being stored in i (which happens over in i++), and so there's no good way to define--either for our understanding or the compiler's--whether the access should take place before or after the incremented value is stored. Since there's no good way to define it, the Standard declares that it is undefined, and that portable programs simply must not use such constructs.

 Himansu said: (Dec 6, 2013) 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 ) ; }

 Sam said: (Jul 14, 2014) 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;.

 Travis said: (Mar 22, 2015) Correct answer is 60 6, because CUBE (b++) is replaced by (b++*b++*b++) and therefore (3*4*5) = 60 while b increments three times.

 Rajesh said: (Apr 27, 2015) I also think that, value of b will be 5. Since Increment (b++) will start from 3 not from 4 (If it will ++b then b=6).

 Amol said: (May 18, 2015) @Pushkar Bhauryal. Please see the calculation: @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 //wrong. = 4*5*6 = 120; But compiler gives 150. How it comes calculated value is 120 but actual is 150?

 Anand said: (Jun 26, 2015) Answer will be 60, 6 as 3*4*5, 6.

 Pankaj said: (Jul 31, 2015) Your answer in totally wrong. You can check by compiling. It is 60, 6.

 Sudip said: (Aug 9, 2015) 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.

 Himanshu said: (Jan 10, 2016) The above Problem has undefined result. The different compiler will have the different result as there is no standard rule regarding this defined by C standard that how this will be executed. It can be understand by taking example like i = i++; It is undefined that whether the value of I will be incremented or it will be assigned first. Kindly stop posting this type of questions or make clearer options.

 Doaa said: (Mar 5, 2016) I think it will be like this 3+1*3+2*3+3 3+3+2*3+3 8*3+3 24+3 = 27 Start with multiply then add.

 Nitish said: (Apr 9, 2016) Till now I am knowing increment operator can increase the value by '1' only in one step then, how "b" increased to 6?

 Ratan Lal said: (Apr 14, 2016) I got the answer on GCC is 60, 6.

 Vinitha said: (Sep 11, 2016) 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.

 Laxmikant said: (Sep 24, 2016) 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.

 Ahmed Salah said: (Sep 28, 2016) 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?

 Pankaj said: (Feb 9, 2017) 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: (Mar 30, 2017) I did not understand this output. Can anyone help me?

 Nti said: (Apr 13, 2017) Yes, 60 6 is right. I agree with it.

 Farooq Ahmed said: (Aug 22, 2017) 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: (Mar 16, 2018) #include #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: (Apr 27, 2018) The answer is 60, 6. Because (b++*b++*b++), 3*4*5, 60 and b=6;

 Amrendra said: (Aug 29, 2018) @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: (Sep 6, 2018) Value of A and B must be as below: a= 3*4*5=60; b=6;

 Shreya said: (Sep 9, 2018) @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: (Oct 8, 2018) 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 .

 Richik said: (Aug 2, 2019) 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.

 Kunijiuma said: (Feb 8, 2021) The result should be 27, 4. Try this code: #include int CUBE(x) {return x*x*x;} int main() { int a, b=3; a = CUBE(b++); printf("%d, %d\n", a, b); return 0; }

 Madan said: (Mar 19, 2021) 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.

 Dhananjay Khairnar said: (Dec 24, 2021) @All. #include #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.