Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post-increment and pre-increment operator in C [duplicate]

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);
}
like image 862
Rishab777 Avatar asked Oct 27 '12 10:10

Rishab777


People also ask

What is ++ i and i ++ in C?

++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.

How pre increment and post increment works in C?

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.

Is i ++ the same as i i 1?

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.

How does pre increment and Post increment work in for loop?

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.

What is the difference between pre increment and post increment in C++?

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. ...

What is the use of pre increment operator in C++?

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.

What is the use of the post increment operator in JavaScript?

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.

What is post-increment operator in C++?

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.


2 Answers

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.

like image 133
Daniel Fischer Avatar answered Sep 23 '22 15:09

Daniel Fischer


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

like image 44
mathematician1975 Avatar answered Sep 24 '22 15:09

mathematician1975