#include <stdio.h>
int main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d %d %d", x, y, z);
}
Output: 2 3 3
Can anyone explain this?
And what does i =+ j mean (suppose i = 1 and j = 2)?
y = --x means "decrease x by one, then store the result in y"
z = x-- means "save a temp of x. Decrease x by one. Store the temp value in z"
Hence:
x starts at 4.
It gets decreased by 1 (to 3). 3 is stored in y.
x is saved to a temp. x is decreased again (to 2). then the temp (3) is stored in z.
y and z are printed as 3, x is printed as 2.
The postfix decrement operator (x--) returns the value of the variable before it was decremented.
x = 2, because you've decremented it
twice.
y = 3, because you've assigned
it to the value of x after it was
decremented from 4
z = 3, because you've
assigned it to the value of x
before it was decremented from 3.
simple explanation:
--x or ++x : Value will be modified after.
x-- or x++ : Value will be modified before
Detailed explanation:
--x or ++x: pre-decrement/increment: will first do the operation of decrementing or incrementing first, then it will assign x.
x-- or x++: post:decrement/increment: will first assign the value of x and then it will do the operation of decrementing or incrementing after.
lets write your code in a nicer format and go through your code step by step and annotate it to show you visually what happens:
main() {
//We declare the variables x, y and z, only x is given a value of 4.
int x=4,y,z;
//--x will decrement var x by 1 first THEN it will assign the value of x to y.
//so: x = 3, y = 3 and z = nothing yet.
y = --x;
//x-- will assign the value of x to z first, then var x will be decremented by 1 after.
//so: x = 2, y=3 and z = 3
z = x--;
printf ("\n %d %d %d", x,y,z);
}
You have to understand the notions of post-decrement and pre-decrement operator.
Both will decrement your variable, but one of them will return the original value (x--) and one of them will return the decremented value (--x).
Postfix decrement (x--) is different from prefix decrement (--x).
The former return the value x, then decrements it; the latter decrements and then returns the value.
And if you thing how a postfix is written at low level, you'll find that it is a liiiitle slower than the prefix... :)
y = --x;
X is decremented, then Y is assigned with the value of X (3)
z = x--;
Z is assigned with the value of X (3), the X is decremented (2)
Yes:
x = 4
y = pre-decrement x (basically decrement by 1 and then assign, i.e. y = x-1 = 3
x =3
z = post-decrement x (decrement by 1 after assigning the value, i.e. z = x = 3, then x = x - 1
x = 2
So x = 2, y = 3, z = 3, exactly what you saw.
If the operator is a prefix the incrementation happens before the assignment, if the operator is a postfix the incrementation happens after the assignment.
let ** be an increment/decrement operator. **e means apply ** to e and evaluate the result whereas e** means evaluate e and then apply ** to it.
Ergo, if decrementation and evaluation are seperated, the code reads as:
int x=4,y,z;
x-=1;//3
y = x;//3
z = x;//3
x-=1;//2
which gives you the ouput you have ;)
x++ / x-- is called post increment / decrement (value modified after...)
++x / ++x is called pre increment / decrement (value modified before...)
Here is what happens (roughly) in your example :
int x=4,y,z; // declare x=4, y=0, z=0
y = --x; // decrement x then assign it to y (pre decrement) (y=x=3)
z = x--; // assign x to z then decrement x (post decrement) (z=3 and x=2)
printf ("\n %d %d %d", x,y,z); // output 2 3 3
A pre increment/decrement looks like this in pseudocode
read value
increment/decrement value
write value
assign value
and a post increment/decrement looks like this
read value
assign value
increment/decrement value
write value
#include<stdio.h>
main ()
{
int x=4,y,z;
y = --x;
z = x--;
printf ("\n %d %d %d", x,y,z);
}
output 2,3,3.................................first time x=4 fine. y=--x, means value of x is decremented by 1 and stored in y, thus now y=3 and x is also 3. then z=x-- means value of x is stored in z( z=3) and then x is decremented i.e now x=2 but z=3. when u r printing the value, then printf() prints 2 3 3
Talking about what i=+j; means(given i=1 and j=2)
i=+j; is equivalent to i=i+j; so your expression becomes i=1+2 i.e. i=3
Related
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;.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
Please explain me the outcome of this code.
//code a
when I run this code on my laptop, value of y is 4. And I think, logically value of y should be 5 because by doing x++ it should return 2 without incrementing as it is post increment and then when we add x which now contains an incremented value ie 3. So 2+3 is 5, according to me. But according to my turbo c++ 3.0 answer is 4.
#include <stdio.h>
void main() {
int x = 2,y;
**int y = x++ + x;** // ans 4
printf("y is :%d", y);
}
// code B
When I run this code, the answer is 6. Here turbo c++ 3.0 in ++x is picking up an incremented value of x++, which is the opposite of above mention code. Logically here answer is correct but in the above code it's not.
#include <stdio.h>
void main() {
int x = 2,y;
**int y = x++ + ++x;** //and 6
printf("y is :%d", y);
}
Firstly, assignment operator that is = works from right to left, which means if you write x = 2 + 4 + 1; your compiler starts reading it from rightmost digit that is 1 then it add 4 to it and so on and then it assigns that value to x.
So, in your case statement y = x++ + x; compiler starts seeing it from right that is it first sees x i.e. 2 and then sees x++ i.e. also 2 as it is post increment operator finally it adds them and assigns y as 2 + 2 that is 4.
In the second case, that is y = x++ + ++x;, compiler first looks at ++x and as it is pre increment operator it increases x with one, i.e. x is now 3. After that x++ is seen and as stated above because it is post operator it would be treated as x in this operation and that value is 3 (remember we incremented x by one earlier) and hence, compiler assigns 3 + 3 i.e. 6 to y.
First program where the result is 4
#include <stdio.h>
void main() {
int x = 2,y;
**int y = x++ + x; // x=2, so y=2+2=4
printf("y is :%d", y); // after post increment x=3
}
Since a variable only gets it post incremented value only after the execution of the statement is completed so y=2+2
Second program where the result is 6.
Associativity of ++ operator is "right to left"
void main() {
int x = 2,y;
**int y = x++ + ++x; //Since associativity is left to right so y=3+3=6
printf("y is :%d", y);// after the post increment x=4
}
Here the pre-increment is performed first since because of the associativity rule so y=3+3
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on that code, and I tried to use x + 1 instead of ++x and the result was infinite loop and zeros were printed on the screen.
this is the code:
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x + 1) //instead of ++x
printf("%d\t",x);
return 0;
}
I wondered this action.....why the compiler didn't produce such an error,,,, and what actually happens??!! and is x++ is treated as x += 1 ?!
You need the value of x to change, or your loop will never terminate. When you have only x + 1, then at the end of the iteration, x + 1 is computed, but its resulting value is discarded. The result of the expression is not stored any place. So x will remain zero, and x <= 100 will remain true, and your loop will continue forever.
If you have any of the following:
x = x + 1
x += 1
x++
++x
the value of x itself is incremented. That's what you need.
Although the basic structure of a for loop is like-
for(initialization; condition; increment or decrement)
But in its core part the "condition" part is treated only to control the loop behavior. So, if other two parts are syntactically correct, then compiler won't produce any error.
Since, x+1 is a valid C statement and the value of the x is not changing so it will go to an infinite loop for the condition [x <= 100 => 0 <= 100] being true forever.
Again, x++ or ++x both treated as x = x + 1 when used independently. But, x++ is actually Post-increment operator, while ++x is Pre-increment operator. That means, the with ++x, the value of the x will be incremented first and then assigned to x. Whereas, the current value of x will be used for the entire statement in x++ and then x will be incremented and assigned with new value. Look at the following example-
#include<stdio.h>
void main()
{
int x=5;
/* Post & Pre-Increment used independently */
x++;
printf("x : %d", x);
++x;
printf("\nx : %d", x);
/* Used with printf() statement */
printf("\nPre-increment of x: %d", ++x);
printf("\nPost-increment of x: %d", x++);
printf("\nPost-increment effect on x: %d", x);
}
Output:
x : 6
x : 7
Pre-increment of x: 8
Post-increment of x: 8
Post-increment effect on x: 9
I Hope my explanation have made you understand, if still not you can reply me back.
The expression x + 1 should be x = x + 1 in for loop, so correct:
for(x = 0; x <= 100; x + 1)
^
doesn't increment x
Infinite loop!
as:
for(x = 0; x <= 100; x = x + 1)
or
for(x = 0; x <= 100; x += 1) // or simply x++ or ++x
The compiler didn't produce such an error because x + 1 is a valid expression (but its not what you wants).
So, x += 1, x++ or ++x are not just add one to x but also modify value of x.
Your approach is not perfect because it definitely increases the value of x but when it comes to assignment, it fails. so your statements should cover all your requirements.
try to use
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x = x + 1)
printf("%d\t",x);
return 0;
}
or you can use
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x++)
printf("%d\t",x);
return 0;
}
Instead of x++ (post increment) you can also use pre increment (++x).
The expression x = x + 1; can also be written as x += 1; which is a shorthand method for assignment embedded with increment. It is not just about incremnent you can also use other operators.
x -= 3;
x *= 2;
#include<stdio.h>
main()
{
int x = 5, y = 10, z = 10;
x = y == z; // This computational expression causes the value of x to be 1. I fail to understand why
printf("%d\n", x); //Why is the value of x 1 here.
}
I fail to understand the statement x = y ==z;
According to me - x = 10 since y == z. z=10 and is stated to be equivalent to y. The value of y is then assigned to x - x = y
You assign the result of the comparison »Is y equal to z« to x, which is 1, i.e. true.
Note the different operators:
x = ... // assignment
y == z // comparison with either 0 (false) or 1 (true) result
Let's break the program down a little bit further:
You initialize x to 5 and y and z to 10.
You perform a comparison (see above) of y and z without caring for the result. So that's a line that can safely be ignored. But it results in 1 since y is equal to z.
You print the current values of all three variables.
You perform the same comparison, this time assigning the result to x. x now has the value »Is y equal to z«, which is 1 in this case.
Because of operators precedence
x = y == z;
is the same as
x = (y == z);
Now as y == z evaluates to 1, so x value is 1 after the statement.
y == z returns 1 if they are the same and 0 if they are not and the result of this is set to x
== is a comparison operator, so will return 1 (true) if both of the operands are equal and 0 (false) if they are not.
The statement x = y == z is equivalent to x = (y == z), because == has a higher precedence than =. Because y is equal to z, this will assign 1 to x.
Please refer to Operator Precedence Table. == (Comparison Operator) has a higher precedence over = (Assignment Operator). So, the y == z gets executed first and then yields result of 1 as y and z are having the same values which results in x being assigned a value of 1.
= is the assignment operator and == is the comparison operator. you compare y and z, they are equal, so the comparison returns true which is 1. this value is assigned to x.
the == operator acts first.
Hence it becomes x= (value of y==z);
now since y and z are the same, the value of y==z is 1 that gets assigned to x.
== has a greater precedence than =.
The expression y == z is evaluated to 1.
The instruction x = y == z puts 1 in x.
The instruction printf("%d\n", x); prints the value of x (1).
The precedence of == operator is higher than = operator.
y==z evaluates to 1; since both are equal.
Thisn value gets assigned to x.
I have a question about these two C statements:
x = y++;
t = *ptr++;
With statement 1, the initial value of y is copied into x then y is incremented.
With statement 2, We look into the value pointed at by *ptr, putting that into variable t, then sometime later increment ptr.
For statement 1, the suffix increment operator has higher precedence than the assignment operator. So shouldn't y be incremented first and then x is assigned to the incremented value of y?
I'm not understanding operator precedence in these situations.
You're mistaken about the meaning of your 2]. Post-increment always yields the value from before the increment, then sometime afterward increments the value.
Therefore, t = *ptr++ is essentially equivalent to:
t = *ptr;
ptr = ptr + 1;
The same applies with your 1] -- the value yielded from y++ is the value of y before the increment. Precedence doesn't change that -- regardless of how much higher or lower the precedence of other operators in the expression, the value it yields will always be the value from before the increment, and the increment will be done sometime afterwards.
Difference between pre-increment and post-increment in C:
Pre-increment and Post-increment are built-in Unary Operators. Unary means: "A function with ONE input". "Operator" means: "a modification is done to the variable".
The increment (++) and decrement (--) builtin Unary operators modify the variable that they are attached to. If you tried to use these Unary Operators against a constant or a literal, you will get an error.
In C, here is a list of all the Built-in Unary operators:
Increment: ++x, x++
Decrement: −−x, x−−
Address: &x
Indirection: *x
Positive: +x
Negative: −x
Ones_complement: ~x
Logical_negation: !x
Sizeof: sizeof x, sizeof(type-name)
Cast: (type-name) cast-expression
These builtin operators are functions in disguise that take the variable input and place the result of the calculation back out into the same variable.
Example of post-increment:
int x = 0; //variable x receives the value 0.
int y = 5; //variable y receives the value 5
x = y++; //variable x receives the value of y which is 5, then y
//is incremented to 6.
//Now x has the value 5 and y has the value 6.
//the ++ to the right of the variable means do the increment after the statement
Example of pre-increment:
int x = 0; //variable x receives the value 0.
int y = 5; //variable y receives the value 5
x = ++y; //variable y is incremented to 6, then variable x receives
//the value of y which is 6.
//Now x has the value 6 and y has the value 6.
//the ++ to the left of the variable means do the increment before the statement
Example of post-decrement:
int x = 0; //variable x receives the value 0.
int y = 5; //variable y receives the value 5
x = y--; //variable x receives the value of y which is 5, then y
//is decremented to 4.
//Now x has the value 5 and y has the value 4.
//the -- to the right of the variable means do the decrement after the statement
Example of pre-decrement:
int x = 0; //variable x receives the value 0.
int y = 5; //variable y receives the value 5
x = --y; //variable y is decremented to 4, then variable x receives
//the value of y which is 4.
//x has the value 4 and y has the value 4.
//the -- to the right of the variable means do the decrement before the statement
int rm=10,vivek=10;
printf("the preincrement value rm++=%d\n",++rmv);//the value is 11
printf("the postincrement value vivek++=%d",vivek++);//the value is 10