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.
Naveen Kumar Chaudhary said:
1 decade ago
@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.
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:
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 ) ;
}
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;.
Travis said:
1 decade ago
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:
1 decade ago
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:
1 decade ago
@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?
@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:
1 decade ago
Answer will be 60, 6 as 3*4*5, 6.
Pankaj said:
1 decade ago
Your answer in totally wrong. You can check by compiling. It is 60, 6.
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.
Himanshu said:
10 years ago
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers