Increment and decrement operator [duplicate] - c

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
Here is a code
#include<stdio.h>
int main()
{
int i=5;
printf("%d%d",++i,++i);
}
I am not understand, why the output is 77?

++i and i++ are expressions that have side effects. Using two of these in the same expression results in undefined behavior. Basically, anything goes.
Specifically, I'm guessing the compiler says you want to increment i twice and then use the result, so it evaluates ++i twice, resulting in 7, and then sends that to printf.

Related

A 7 Line Simple C Code That I Can't Figure Out [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Parameter evaluation order before a function calling in C
(7 answers)
Closed 1 year ago.
Can someone please explain why the output here is 2 1 1, what does c prioritise here? how come the output of i++ is 1. Thanks in advance
~
#include <stdio.h>
void main(){
int i=1;
int *p=&i;
printf("%d%d%d\n",*p,i++,i);
}
The behavior of this code is not defined by the C standard per C 2018 6.5 2:
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined…
i++ has the side effect of updating i with its incremented value. The function call arguments also include *p and i, both of which refer to i and use its value. i is a scalar object. The order of evaluation of function call arguments and their side effects are unsequenced (meaning the C standard does not impose any ordering requirements on them). Therefore, all the conditions for the rule above are met, so the behavior is not defined by the C standard.

Are statements in printf() executed from right to left? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Explain the order of evaluation in printf [duplicate]
(5 answers)
Parameter evaluation order before a function calling in C
(7 answers)
Closed 4 years ago.
If C Compiler works from left to right then why is the following code's output in a way as if it is executing from right to left?
int x = 15 ;
printf("%d %d %d", x!=15, x=20, x<30);
Output :
1 20 1
First of all, those are expressions and not statements.
Secondly, the order of argument evaluation is unspecified. You can't tell in what order the arguments will be evaluated, and your code would lead to undefined behavior.
For more information about evaluation order and sequencing see e.g. this reference.

C program different Output on different Compilers? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
Here is the C program which is giving different output depending on Compiler used:
#include<stdio.h>
int main()
{
int i = 5,j;
j = ++i + i++ + ++i + i++;
printf("%d",j);
return 0;
}
Check the output on the following link.
http://imgur.com/z9aMSwj,Vwx3P9S
http://imgur.com/z9aMSwj,Vwx3P9S#1
My question is what is the technical reason that output is different?
The technical reason is that there is no defined behaviour for such an operation, allowing compilers to handle this kind of an order as they wish. Such cases are generally referred as Undefined Behaviour.
According to C language, expressions like ++i + i++ + ++i + i++ have undefined behavior.
In a i++ expression, two things happen. First, the expression is evaluated to i, then i is incremented later. The behaviour of 'later' is undefined. If there are 4 such expressions in a statement, there are countless possible orders to evaluate the increments and decrements.

Output of C program [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
int a[]={10,20,30,40};
int x=0;
int v=a[++x]+ ++x + a[--x];
printf("%d",v);
What will be the output of this program??
Completely confused with the output. No way it is going to be done according to my operator precedence knowledge.
According to me, in this expression Array subscripting [] has highest precendence and should be executed first. so both [] should be executed first from left to right. In this case value of x will increment first, then decrement and finally come back to 0. so expression will become int v=a[0] + ++x + a[0]. Then the pre increment is having highest precedence and it will be incremented to 1. so our expression will become int v=a[0]+1+a[0]. so final output will be 21.
But this is not the case. I have checked on different compiler implementations and no one prints 21.
I am much surprised because the value printed is 43, which is no where understandable to me. That's why I want someone to help me understand and come to the result 43.
The link which others have suggested is using only increment and same rvalue and lvalue cases. But this is somewhat different and not clear. I tried to contruct expression tree for this and solve but 43 is no where in scope.
Output of this code:
int v=a[++x]+ ++x + a[--x];
is undefined and it depends on the compiler implementation.

Postfix and prefix operators as function arguments - why is this happening? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
I came across a very interesting fact while executing the following a simple c program:
#include<stdio.h>
int main( )
{
int k=0;
printf("%d%d%d", k++, k, ++k);
return 0;
}
in windows it was showing output as: 1 1 1
but in linux(ubuntu) it was showing as: 1 2 2
why is it so?
It's undefined behaviour. When there are no / ambiguous sequence points. See this wikipedia article:
http://en.wikipedia.org/wiki/Sequence_point
There are two problems. First is that the order in which the expressions k++, k, and ++k in the printf call are evaluated is not specified; the compiler is free to evaluate them in any order it sees fit. Second is that an object may not have its stored value updated more than once between sequence points by the evaluation of an expression. Both k++ and ++k attempt to update the value stored at k, and there is no sequence point between those expressions, so the behavior is undefined; any result is permitted.
The standard does not specify an ordering in which the arguments of a routine are evaluated.
Writing code that depends on the ordering is not portable.

Resources