pre/post increment decrements evaluation in single line [duplicate] - c

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
can you please explain me this.
I have read almost all questions on stackoverflow
undefined behaviour
pre/post inc.. dec.. operator precedence/associativity
use the parenthesis
but results are inconclusive.
please explain this i have an interview tomorrow.
int a, x = 0;
// value of x ------> a(sum)
//gcc 4.9.9.2[dev cpp] 2 1 2 4 5 14
//acc to my calculation 2 0 2 4 5 13
//geeksforgeeks compiler 5 5 5 5 5 15
//gcc 6.3 [codechef ide] 5 5 5 5 5 15
a = ((((++x + x++) + ++x) + ++x) + ++x);
printf("%d....%d", x, a);
return 0;
if i have made mistake please help me to correct them.
Thanks

There is no sequence point, thus your code invokes Undefined Behavior.
Parentheses do not introduce sequence points, thus, despite the fact that you use them, you still invoke UB.

Related

Understanding Post increment concept in C [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Sequence Points between printf function args; does the sequence point between conversions matter?
(3 answers)
Closed 3 years ago.
Consider code below:
#include <stdio.h>
int main()
{
int x=0,y=5;
printf("x=%d,x_1=%d,sum=%d",x++,x,y+x);
return 0;
}
My assumption on this code was that, x would be printed as 0 and later on postincrement x_1 would be 1 and y+x be 5+1=6
Actual result is x as 0 as expected , x_1 as 1 as expected. But y+x be 5. I am unsure why x retains its previous value though an postincrement had occured. Could you please help clarify this?
I used gcc compiler for the same.
In
printf("x=%d,x_1=%d,sum=%d", x++, x, y+x);
// (a) (b) (b)
you are both updating x (a) and using its value (b) in the same expression (with no intervening sequence point).
That's Undefined Behaviour.
Try
printf("x=%d,x_1=%d,sum=%d", x, x + 1, y + x + 1);
x++;
This is standard undefined behaviour, order of evalution of function arguments is non deterministic. Read [Why are these constructs using pre and post-increment undefined behavior?

How to understand this C macro? [duplicate]

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 4 years ago.
I am not able to understand this code
#define sqt(x) x*x
int main(){
print("%d",sqt(3+1));
}
Manually I am getting the output of 10. But when write the code and compile it I am getting the answer as 7. Please explain this.
Remember, since you're using a macro, 3 + 1 is not evaluated before sqt is called. x becomes 3 + 1 (not 4), then order of operation causes an unexpected answer to be produced since addition happens after multiplication.
Or in other words:
sqt(3 + 1)
expands to:
3 + 1 * 3 + 1
Then, when you evaluate this like you would any other equation:
3 + 1 * 3 + 1 // Multiplication happens first
= 3 + 3 + 1
= 7
This is a good example of why you shouldn't use macros unless they're strictly necessary, or you've made proper care to ensure that things like order of operation mistakes don't happen. As #Barmar points out this particular case can be remedied by having the macro expand to include explicit parenthesis:
#define sqt(x) ((x)*(x))
Which would cause the evaluation to differ and give a proper answer:
(3 + 1) * (3 + 1)
4 * 4
16

postincrement in printf function [duplicate]

This question already has answers here:
Why doesn't a+++++b work?
(9 answers)
Closed 8 years ago.
The following code prints 7. It increments value of a, but b remains unaffected. Post increment operator is not supposed to increment first and then use the value which it has done in this case. It should be the other way around. Why this change in behaviour? Also, and perhaps more importantly, why is b not pre-incremented?
int main() {
int a=5, b=2;
printf("%d", a+++b);
}
You have a well defined behavior. Where
a++ + b = 5 +2 = 7
If you have a pre-incrementor operator like
printf("%d\n",++a+b);
Then the output would be 8
The ++ operator has higher precendence than the + unary plus operator so the evaluation from left to right of
a+++b will happen
a++ + b which will fetch you 7
Post-incrementation is done by taking the value of your variable first which in this case is 5 so
a= 5 and b=2
In your code a is added to b and then it is incremented

C code, where i have a value of an integer variable and after a pre-increment operation i need to get the output [duplicate]

This question already has answers here:
What is the difference between ++i and i++?
(20 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
i=2;
i= ++i + ++i + ++i;
printf(i)
Please give the output with explanation?
The answer I'm getting is 12 but it should be 13.
The behavior of your code is undefined according to the C standard, as you are not allowed to use the preincrement operator more than once within the same expression. The output can be anything whatsoever.
See the answer to this question for a more comprehensive treatment of the topic.
Though the behaviour is undefined, IN UR CASE IT HAS BEEN EXECUTED AS, CONSIDERING THE PARSING IS FROM left, i = 5 + 4 + 3 = 12
For explanation,
i = (++i) + (++i) + (++i)
Now i = 2, so first ++i expands as 3 and i becomes i=3
i = (++i) + (++i) + 3
Now i = 3, so ++i expands as 4 and i becomes i=4
i = (++i) + 4 + 3
Now i = 4, so first ++i expands as 4 and i becomes i=5
i = 5 + 4 + 3

Operator Precedence in C [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 10 years ago.
Cannot explain the output of the following program. According to my knowledge the output should be 19, but running it gives me output of 20. I used gcc to compile this program.
int main()
{
int x, y = 5;
x = ++y + ++y + --y;
printf("%d", x);
return 0;
}
Your program exploits undefined behavior as you modify y multiple times between two sequence points (in your case, the end of the statement). If you turn on warnings with -Wall, your compiler is probably even going to warn you about that.
6+7+6 = 19
so 19 will be your output

Resources