Why is k not getting incremented whereas,i and j are getting incremented in the same expression.And i also want to know what is the output of the program.I am getting the output as -2 3 1 0
#include <stdio.h>
void main()
{
int i=-3, j=2, m, k=0;
m=++i && ++j || ++k;
printf("%d %d %d %d", i, j, m, k);
}
++i : is pre-increment the other is post-increment. i++ : gets the element and then increments it. ++i : increments i and then returns the element. Example: int i = 0; printf("i: %d\n", i); printf("i++: %d\n", i++); printf("++i: %d\n", ++i); Output: i: 0 i++: 0 ++i: 2.
Pre-increment and Post-increment concept in C/C++? Decrement operator decrease the value by one. Pre-increment (++i) − Before assigning the value to the variable, the value is incremented by one. Post-increment (i++) − After assigning the value to the variable, the value is incremented.
These two are exactly the same. It's just two different ways of writing the same thing. i++ is just a shortcut for i += 1 , which itself is a shortcut for i = i + 1 . These all do the same thing, and it's just a question of how explicit you want to be.
Pre increment directly returns the incremented value, but post increments need to copy the value in a temporary variable, increment the original and then returns the previous made copy.
1 Pre-increment Operator. The Pre-increment operator increases the value of the variable by 1 before using it in the expression, i.e. ... 2 Post-increment Operator. The Post-increment operator increases the value of the variable by 1 after using it in the expression, i.e. ... 3 Difference between pre-increment and post-increment operators. ...
The pre increment operator is used to increment the value of some variable before using it in an expression. In the pre increment the value is incremented at first, then used inside the expression. if the expression is a = ++b; and b is holding 5 at first, then a will hold 6.
The post increment operator is used to increment the value of some variable after using it in an expression. In the post increment the value is used inside the expression, then incremented by one.
2) Post-increment operator: A post-increment operator is used to increment the value of the variable after executing the expression completely in which post-increment is used. In the Post-Increment, value is first used in an expression and then incremented.
The logical or, ||
short-circuits, and after
++i && ++j
the value of the entire expression is determined, so the right operand of the ||
isn't evaluated.
m=++i && ++j || ++k;
is parenthesized m = (++i && ++j) || ++k;
since the &&
has higher precedence than the ||
.
The short-circuiting of the logical operators means that the right operand is only evaluated when the evaluation of the left has not yet determined the final result, for ||
that means the right operand is only evaluated if the left evaluated to 0, and for &&
, the right operand is only evaluated if the left evaluated to a nonzero value.
So first ++i && ++j
is evaluated, and for that, first ++i
is evaluated. i
had the value -3
before, so ++i
evaluates to -2
, which is not 0, hence the ++j
is evaluated too. j
had the value 2
before, so ++j
evaluates to 3
, which is again nonzero, and thus ++i && ++j
evaluates to 1 (true). Since the left operand of the ||
is not zero, its result is already determined (to be 1), and the right operand isn't evaluated, thus k
remains unchanged and m
is set to 1.
If the item on the left of an ||
condition evaluates to true, there is no point evaluating the right hand side since the OR condition is already satisfied. That is why the ++k
is not being evaluated
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With