Output of the following program [duplicate] - c

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
How to interpret these pre increment operators?
Pre increment operators have right to left associativity, so the right most i will be incremented or all the i's will be incremented once?
main()
{
int i=3,j;
j=++i*++i*++i;
printf("%d",j);
}
Answer is 216.

According to the c11 standard, ยง6.5 par. 2 of the working draft, expressions like these are indeed undefined.

++i increments i before the operation.
You code is equivalent to :
int i=3,j;
j=(i+1)*(i+2)*(i+3);
i++;i++;i++;
printf("%d",j);
This will output 4*5*6=120

Answer is 150
Is it equal to (++i * ++i) * ++i, first ++i increments i (i=4), second ++i increments i (i=5) but it is the same i so 5*5 = 25.
Finally 25 * ++i = 150

Related

What is order of evaluation in printf() for pointer [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 months ago.
`
a = 10;
int *ptr = &a;
printf("%d %d\n", a, ++*ptr);
`
The output is - 11 11
How is it evaluated??
This is undefined behaviour, so the result could be anything. There is no sequence point in the line, which means that both operations are unsequenced - either argument could be evaluated first, or both simultaneously (see https://en.wikipedia.org/wiki/Sequence_point and John Bollinger's comment).
For example, when evaluating with the clang compiler, this is the output:
<source>:5:26: warning: unsequenced modification and access to 'a' [-Wunsequenced]
printf("%d %d\n", a, ++a);
~ ^
1 warning generated.
Execution build compiler returned: 0
Program returned: 0
2 3
See this answer for more: Order of operations for pre-increment and post-increment in a function argument?

Increment and decrement operators in one statement in C [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 3 years ago.
I know it is theoretically undefined behavior and of course bad style. This is the example of a school (I am not the pupil).
But why do I get 7 (example a) resp. 1 (example b) out of this operation:
Online example:
https://onlinegdb.com/B172lj8k8
a:
#include <stdio.h>
int main()
{
int i = 2;
printf("%d; i=%d", ++i + i++, i);
return 0;
}
b:
#include <stdio.h>
int main()
{
int i = 2;
printf("%d; i=%d", ++i - i++, i);
return 0;
}
In my opinion the output should be 6 and 2.
Execute i++, yield 2, increment i
Execute ++i yield 4
Additon 2 + 4
The other example should be 4 - 2.
Exexcuting the increment in a statement seems to yield the result of the increment immediately, no matter if it is postfix or prefix, which is odd. Or do I get it wrong totally?
The order in which the arguments passed to a function are evaluated is not specified, and the order of evaluating the operants of + is unspecified, too.
So in printf("%d %d", i+1, i-1), for example, you cannot rely on the order of evaluation of the arguments; i+1 might be evaluated after i-1, actually; You will not recognise, since the evaluation of the one does not effect the result of the other.
In conjunction with "side effects" like the post-increment i++, however, the effect of incrementing i at a specific point in time might influence the result of other evaluations based on i. Therefore, it is "undefined behaviour" in C, if a variable is used more than once in an expression and a side effect changes its value (formally, to be precise, if there is no sequence point in between).

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?

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

Undefined behavior while using increment operator [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 years ago.
I am new to C, i have an Increment operator program in C
#include<stdio.h>
main(){
int a, b;
a = 2;
b = a + ++a + ++a;
printf("%d", b);
getchar();
}
The output is 10, can someone explain me how the output will be 10 .
a + ++a + ++a;
Behaviour for this is undefined.
The compiler might generated code that evaluated this as 2 + 4 + 4 or 3 + 3 + 4, but any combination/ordering of incrementing and accessing is a "valid" result.
This is undefined, the ++i can happen in any order.
Function call arguments are also ambigiously evaluated, e.g. foo(++i,++i).
Not all operator chains are undefined, a||b||c is guaranteed to be left-to-right, for example.
The guarantees are made in places known as sequence points although this terminology is being deprecated and clarified in C++0x.
What's odd in your example is that neigher 2+3+4 nor 4+4+3 happened, so the compiler evaluated the left side first in one step and the right side first in the other. This was probably an optimisation to flatten the depencency graph.

Resources