problem on performing increment in an expression - c

When i compile this code,it showing an error as lvalue required as increment operand
int main(void)
{
int x,y,z;
x=8++;
y=++x++;
z=(x+y)--;
printf("x=%d,y=%d,z=%d",x,y,z;
return 0;
}

When you write something++ or ++something it's roughly equivalent to
something = something + 1
(the difference between something++ and ++something is in what you get when you assign the result to something else).
Because of this, the operand of ++ has to be something you can assign to.
8++ is equivalent to 8 = 8 + 1, but you can't assign to a number.
++x++ would be equivalent to something like (x = x + 1) = (x = x + 1) + 1, I can't even fathom what this could be intended to mean.
(x+y)-- is equivalent to (x + y) = (x + y) - 1. You can't assign to an addition expression (which variable would you be setting)?
You should only use ++ or -- when you actually want to update a variable. It's not a general replacement for + 1 or - 1. So your program should be:
x = 8 + 1;
y = 1 + x + 1;
z = (x + y) - 1;
This will print
x = 9, y = 11, z = 19

constant cannot be changed. 8 is a constant value. (x+y) is also not the value which can be incremented and stored somewhere.
x++ uses the value of x then increments the x. So the lvalue is changed itself not only the result of the operation.
correct is
z=(x+y)-1;
x=8+1;

Related

C Code Algebraic output is not what I thought

Please explain to me how th3 output is 11.
int main(){
int delta = 12, alpha = 5, a = 0;
a = delta - 6 + alpha++;
printf("%d", a);
return 0;
}
In C, putting ++ after a variable in an expression increments it after the expression is evaluated. Therefore, substituting the variables for their values, it would be a = 12 - 6 + 5, and afterwards alpha would be equal to 6.
If you put ++ before the variable, it increments it before the expression is evaluated. So therefore, if you had done a = delta - 6 + ++alpha, it would have been 12.
The result of x++ is the current value of x - as a side effect, x is incremented. Logically, your expression is equivalent to writing
tmp = alpha;
a = delta - 6 + tmp;
alpha = alpha + 1;
with the caveat that the assignment to a and the update to alpha can occur in any order, even simultaneously. Point is that a is computed with the value of alpha before the increment. Now, your specific compiler may choose to generate code like
a = delta - 6 + alpha;
alpha = alpha + 1;
because that makes sense here, but the point is that it isn't specified exactly when the side effect to alpha is applied.
The result of ++x is the current value of x plus one - as a side effect, x is incremented. Had you used ++alpha in your statement, it would logically equivalent to writing
tmp = alpha + 1;
a = delta - 6 + tmp;
alpha = alpha + 1;
with the same caveat as above. a is computed with the value of alpha + 1. Again, your compiler may choose to generate code equivalent to
alpha = alpha + 1;
a = delta - 6 + alpha;
or it could have done something like
a = delta - 6 + (alpha + 1);
alpha = alpha + 1;
Again, it isn't specified exactly when the side effect is applied.

Example of = (assignment) being right-associative

Where would the associativity of the = assignment operator make a difference in an expression? I thought that the associativity relates to operands that share an operator, but in the case of assignment, how would that work? A few examples that (might) be relevant are:
x = 1
x + 2 = y + 3 = z + 5
Does this just mean that, in the assignments above, we would have:
y + 3 = z + 5
Done before, for example:
x + 2 = y + 3
Or what other scenarios are there where assignment associativity 'matters' ?
Your examples don't demonstrate anything, because associativity only comes into play when you have several operators with the same precedence (or the same operator) next to each other.
Consider x = y = 42, which sets both variables to 42.
Because of right-associativity, it's parsed as x = (y = 42), where y = ... returns the new value of y, which is 42.
This is why it works. If = was left-associative and it was parsed as (x = y) = 42, then:
In C it wouldn't compile at all, because x = ... returns an rvalue rather than an lvalue, and those can't be assigned to.
In C++, where assignments return lvalues, it would work like x = y; x = 42;, which is far from being intuitive.

Precedence in C operators == and ( = )

I have to analyze what some code in C does, and I have a doubt about what happens in a certain line. The code is
#define PRINTX printf("%d\n", x)
void problem() {
int x = 2, y, z;
x *= 3 + 2; PRINTX;
x *= y = z = 4; PRINTX;
x = y == z; PRINTX;
x == (y = z); PRINTX; // This line is the problem
}
This code snippet prints the resulting numbers:
10
40
1
1 // This result **
the problem is that I'm still trying to figure out why does the last line prints out x = 1, when the operation is x == (y = z). I'm having trouble finding out what that 1 means and the precedence of the operations. Hope someone can help me! :)
Nothing in the last statement changes the value of x, so its value remains unchanged.
Parens were used to override precedence, forcing the = to be the operand of the ==.
An operator's operands must necessarily be evaluated before the operator itself, so we know the following:
y is evaluated at some point before the =.
z is evaluated at some point before the =.
x is evaluated at some point before the ==.
= is evaluated at some point before ==.
That's it. All of these are valid orders:
z y = x ==
y z = x ==
x y z = ==
etc.
But whenever x, y and z are evaluated, we can count on the following happening:
= assigns the value of z (currently 4) to y and returns it.
== compares the value of x (currently 1) with the value returned by = (4). Since they're different, == returns 0 (which isn't used by anything).
As you see, nothing changed x, so it still has the value it previously had (1).
In the last statement, nothing is changing the value of x. We are testing if x equals something, but we aren't changing it's value.
So it continues having the same value as it had in the previous statement, in particular, a value of 1.
the reason is because the == operator checks if the 2 numbers are equal, and returns 1 if equal and 0 if not equal that is why it returns one you can check by making x= 1 and y=2 and using the == operator between them
The comparison result of x and assignment of y with (y = z) is discarded. Last line could have dropped the compare: y = z; PRINTX;.
The assignment is not subsequently used either, so the line could have been PRINTX;.

Why does increment operator modify the original value and bit operators don't?

The increment operator modifies the original value like,
int i = 5;
i++;
printf("%d",i); //prints 6
but the bit operator does not, example,
int x = 5;
x<<1;
printf("%d",x);//should print 10 but outputs the original value i.e. 5
x << 1 is analogous to operations like x * 2. If you don't store the result anywhere, it is just discarded and the line may just get omitted entirely by an optimizing compiler.
If you want to store the result of an operation like that back into x, you have options like:
x = x * 2;
x *= 2;
The << operator is the same:
x = x << 1;
x <<= 1;

Can't understand x *= y+1 output

I have a problem understanding the output of the code. Any explanation please...
#include<stdio.h>
void main()
{
int x=2,y=5;
x*=y+1;
printf("%d",x);
}
The output is as 12. But as per my understanding x*=y+1;is x=x*y+1; but as per operator precedence x*y should be evaluated followed by adding 1 so it should be 10+1=11. But it is 12 — can anyone explain please?
It will be evaluated as
x = x * (y + 1);
so
x = 2 * ( 5 + 1 )
x = 12
What's going on here is how the order of operations happens in programming.
Yes, if you were to have this equation x*y+1 it would be (x * y ) + 1 and result in eleven.
But in programming, the equation to the right of the = sign is solved for prior to being modified by the symbol proceeding the = sign. In this equation it is multiplied.
So x *= y + 1 is actually x = x * ( y + 1 ) which would be 12.
^ In this case, the asterisk(*) is multiplying the entire equation on the right hand side by x and then assigning that outcome to x.
It is translated into : x = x*(y+1);
So very obviously it prints out 12.
Your understanding is correct but it's somthing like this:
x*=y+1; => x = x * (y + 1);
Now apply BODMAS
x *= y + 1 is x = x * (y + 1)
Operator + has higher precedence than operator *=.
x*=y; works like x=x*y;
and here x*=(y+1) is getting expanded like x = x * (y + 1);
*= and similar ones are a type of C assignment operators, i.e. these operators are different from * and alike.
Now from C operator precedence, these operators have lowest precedence (higher than ,) hence y + 1 will be evaluated first, then *= will be evaluated and result will be assigned to x.
It evaluates as
x = x * (y + 1);
so
x = 2 * (5 + 1) = 12
Take a look at Operators order, you will see why in this case it is evaluated like that.
Here from operator procedure in c you can see
Addition/subtraction assignment has lower procedure than simply add operation.
so here
x*=y+1;
+ get executed first.
so
x = x * (6)
so x = 2 * 6
x = 12;

Resources