This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Why does sizeof(x++) not increment x?
Here the value of i is 5 but it should be 6 or 7 I guess. What's the reason?
int main(void){
int i=5,j;
j=sizeof(i++ + ++i);
printf("%d %d",i,j);
}
Related
This question already has answers here:
Why the following function is called thrice
(6 answers)
Closed 5 years ago.
After executing i am getting the output as 12 6 11. please explain how this is possible
#include<stdio.h>
#define MAN(x,y) (x)>(y)?(x):(y)
int main()
{
int i = 10,j = 5,k = 0;
k = MAN(i++,++j);
printf("%d %d %d", i, j, k);
return 0;
}
The macro will expand the line
k=MAN(i++,j++)
as
k=(i++)>(j++)?(i++):(j++);
k=(i++)>(j++)?(i++), (j++) will not be evaluated.
so
i will be incremented twice and result in 12 j will be incremented once so 6 when k will be assigned i value is 11 so its value as11`.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
First of all, I would like to apologize, if this question has been asked before in this forum. I searched, but could't find any similar problem.
I am a beginner in C. I was going through a tutorial and came across a code, the solution of which I can't understand.
Here is the code -
#include <stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
int i=3, j, k;
j = PRODUCT(i++);
k = PRODUCT(++i);
return 1;
}
I tried running the code through compiler and got the solution as "j = 12" and "k = 49".
I know how #define works. It replace every occurrence of PRODUCT(x) by (x*x), but what I can't grasp is how j and k got the values 12 and 49, respectively.
Any help would be appreciated.
Thank you for your time.
Your code will invoke undefined behavior. Anything could happen. The macro in statements
j = PRODUCT(i++);
k = PRODUCT(++i);
will be expanded to
j = x++ * x++;
k = ++x * ++x;
In both statements x is being modified more than once between two sequence points.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
Code in C language.
#include<stdio.h>
#define PRODUCT(x) ( x * x * x)
int main()
{
int i =5,k;
k = PRODUCT( ++i );
printf("i is :%d ",k);
return 0;
}
My Question is why i is : 392? According to me output should be 336. (because 6 * 7 * 8 = 336)
Am I really messed up here??
Preprocessed code will have
( ++i * ++i * ++i)
which have Lack of sequence point between the two execution on same variable resulting Undefined behaviour.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Undefined Behavior and Sequence Points
#include<stdio.h>
int main(){
int i=5,j=5,y,x;
int m=++i;
int n=++i;
x=m+n;
y=++j + ++j ;
printf("%d %d ",x,y);
return 0;
}
OUTPUT : 13 14
Can any one plz explain why 'y' value is 14 and not 13.
Most compilers will increment j twice before performing the addition and attributing the value to y, that is why the result in your case is 14.
The C standard doesn't specify the order of evaluation of that expression, though, so on another compiler the result could be 13 indeed.
In other words, this is undefined behavior and should be not be used other than in obfuscation contests and puzzles.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined Behavior and Sequence Points
the output of the programme
#include<stdio.h>
main()
{
int i = 10;
printf("%d %d %d\n", ++i, i++, ++i);
}
is 13 11 13. Can someone please explain this ?
It's the oldest question ever. Why do people find this so fascinating?
This is undefined behavior; you're relying on side-effects without a sequence point between modifications.