Post increment and Pre increment in C - c

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

Related

Why do the following C expressions involving pointers not evaluate to an error?

I have been reviewing material regarding C for the upcoming semester, and stumbled across some topics involving pointers and memory. I did not write the code, but I am trying to understand all of it. The expressions are as follows (for conciseness I removed int main()):
int x = 5;
int y;
int *p = NULL;
p = &x;
y = *p + 2; /* y is assigned 7 */
y += *p; /* y is assigned 12 */
*p = y; /* x is assigned 12 */
(*p)++; /* x is incremented to 13 */
I do not understand the 7th statement, because it isn't consistent with the way the 5th statement is evaluated. As far as I was concerned, *p is used to access the value p points to, being x, which is 5. In the 5th statement, we set y = *p + 2, which evaluates to y = 5 + 2 = 7. So, why in the 7th statement is *p used as a pointer to x, and not the value 5? (ie. why doesn't the expression evaluate to 5 = 12 and give an error?).
Thanks for any of the help, appreciate it.
*p = y; sets *p to the value of y and does not set 5 to the value of y for the same reason that x = y; sets x to the value of y and does not set 5 to the value of y.
Specifically, in most places, when there is expression that refers to an object, like x or *p or y, it is automatically converted to the value stored in that object. This is why, in x = y;, the y is converted to 12. This conversion occurs due to C 2018 6.3.2.1, which says:
… an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue)…
But the full sentence lists exceptions:
Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue)…
In x = y; or *p = y;, the x or *p is the left operand of an assignment operator. So these are not converted to values. Each remains an lvalue. An lvalue is a reference to an object; it designates the object itself, not its value.
So x = y; puts the value of y into the object referred to by x, and *p = y; puts the value of y into the object referred to by *p.
why doesn't the expression evaluate to 5 = 12
It's possible to write to the memory pointed to by a pointer.
Similarly, x = y; is valid because you can read from the variable y and you can write to x. Same thing with pointers: *p = *p + 1;, for example, will read from p, add one to that value and write the result back to p.

Why the value of X is not incremented while assignment

I'm new to C language and I'm not entirely sure why on Line 4 when post-increment is done the value of x isn't change what I mean is that
x = printf("%d",x++);
The value for x was 12 so the printf would print 12 and then x should be assigned 2 and while ++ was there x should be later changed with 2+1 and on line 6 pre-increment is done so output shouldn't be 124.
Why x on line 4 isn't added?
Please help.
#include <stdio.h>
int main(){
int x = 12;
x = printf("%d", x++);
printf("%d", ++x);
return 0;
}
Make yourself aware of sequence point. From this [emphasis mine]:
There is a sequence point after the evaluation of all function arguments and of the function designator, and before the actual function call.
From this [emphasis mine]:
Increment operators initiate the side-effect of adding the value 1 of appropriate type to the operand. Decrement operators initiate the side-effect of subtracting the value 1 of appropriate type from the operand. As with any other side-effects, these operations complete at or before the next sequence point.
Looks at this statement:
x = printf("%d", x++);
The post increment operator increase the value of operand by 1 but the value of the expression is the operand's original value prior to the increment operation.
So, the value of x passed to printf() will be its original value which is 12 and due to sequence point, before calling printf() the value of x will be incremented by 1. The return value of printf() will be assigned to x which overwrites the last value of x which is the incremented value due to post ++ operator. Hence, after this statement the value of x is 2.
The value for x was 12 so the printf would print 12 and then x should be assigned 2 and while ++ was there x should be later changed with 2+1 and on line 6 pre-increment is done so output shouldn't be 124.
no, the assignment is done after all concerning printf("%d", ++x);, your code is equivalent to that :
#include <stdio.h>
int main(){
int x = 12;
int y = printf("%d", x++);
x = y;
printf("%d", ++x);
return 0;
}
so x = printf("%d", ++x); does :
printf writes 12
then x is incremented to value 13
then x is assigned to the result of printf valuing 2
then you execute printf("%d", ++x); while x values 2 before, so x is incremented before to be given in argument, so 3 is written
and the final print result is 123
PS. As said by #H.S. in an other remark :There is a sequence point after the evaluation of all function arguments (x++ is an argument to printf()) and the pre/post increment/decrement operation complete at or before the next sequence point.
This will work for you:
++x is prefix increment and the x++ is postfix increment, and here is the difference on both of them:
The prefix increment returns the value of a variable after it has been incremented.
On the other hand, the more commonly used postfix increment returns the value of a variable before it has been incremented.
#include <stdio.h>
int main()
{
int x = 12;
x = printf("%d", ++x);
printf("%d", ++x);
return 0;
}

C language basics 1

What is the difference between a++ and a=a+1 in C if a is a variable.
Tell me the difference between the following assignments;
x=a+1;
x=a++;
I am not able to find a convincing explanation on Google.Please explain the difference clearly and step by step.
a = a+1; calculates the result of a+1 and assigns it to a.
a++; increments a by 1, but evaluates to the previous value of a.
++a; increments a by 1, and evaluates to the new value of a.
a += 1 is identical to ++a;.
So for example:
x = a++; → a will be incremented by 1, and the previous value of a will be assigned to x.
x = ++a; → a will be incremented by 1, and the new value of a will be assigned to x.
x=a+1 just sets the value of x.
x=a++ first sets x to the value of a, and then increments a. a++ is an operation in itself, and you could call it as a single statement to increment a, without assigning it to x whatsoever.
FYI, there is also ++a, which could be used like this:
x=++a. In this case, a is incremented first, and then the incremented value is assigned to x. In either case, the ++ operator modifies a, which is not the case for a+1.
From the C11 Standard (draft):
6.5.2.4 Postfix increment [...] operator[...]
[...]
2 The result of the postfix ++ operator is the value of the operand. As a side effect, the
value of the operand object is incremented (that is, the value 1 of the appropriate type is
added to it).
So
int a = 0;
int x = a++;
results in a being 1 and x being 0.
x = a++ returns the value of a to the variable x first and later it is incremented.
x = a+1 Here a+1 is evaluated and the result is stored in x
3 differences
Return value and side effect
// a is incremented, but its former value is assigned to x
x=a++; //
// a is unchanged. The sum of `a+1`is assigned to x
x=a+1;
Operator precedence. post-fix ++ (aka Suffix increment) is higher precedence than simple addition.
++ only works with integer and non-void* pointer types. +1 works with all scalar type including double.
int a;
x=a++; // valid
x=a+1; // valid
double a;
x=a++; // invalid
x=a+1; // valid

pre/post increment pointer

Is there a difference between the two expressions:
(*x)++
and
++(*x)
I can see both of these statements replace the contents of (*x+1) in *x. But are there any differences between them?
(*x)++ evaluates to the value of*x; as a side effect, the value in *x will be incremented by 1.
++(*x) evaluates to the value of *x + 1; as a side effect, the value in *x will be incremented by 1.
Assuming the code:
int a = 5;
int *x = &a;
(*x)++ will evaluate to 5, while ++(*x) will evaluate to 6.
Note that the side effect does not have to be applied immediately after the expression has been evaluated; it only needs to be applied before the next sequence point.
Also note that the parentheses matter for (*x)++; postfix ++ has higher precedence than unary *, so *x++ would be parsed as *(x++); it still evaluates to the value of *x, but as a side effect advances the pointer, rather than increment the thing being pointed to. Prefix ++ and unary * have the same precedence, so they're applied left-to-right; thus ++(*x) and ++*x would have the same result (*x gets incremented, not x).
Let us say the value pointed by x is 10 i:e (*x is 10)
y = (*x)++;
the above statement will be executed as
1. y = *x
2. *x = *x + 1
so y = 10 & *x is 11
y = ++(*x);
the above statement will be executed as
1. *x = *x + 1
2. y = *x
so y = 11 & *x is 11
One increments the values stored in x before it is used (pre), while the other does it after it is used (post).
Also note that ++(*x) is not the same as (*x + 1). The first increments the value and stores it back, the other just increments the value.
One is "pre" and the other is "post". That's the difference. One is evaluated before the the increment (the first option), the other is evaluated after the increment (the second option).

Increment and Decrement Operators

#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

Resources