Post-increment and pre-increment operator in C [duplicate]
Asked Answered
R

7

5

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);
}
Railing answered 27/10, 2012 at 10:56 Comment(0)
M
8

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.

Maxfield answered 27/10, 2012 at 10:59 Comment(0)
S
6

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

Sophy answered 27/10, 2012 at 10:59 Comment(0)
P
1

These operators are known to be short-circuited operators. So, if the expression ++i && ++j is true, it does not evaluate k (we know the value of the expression regardless to the value of k).

Pansy answered 27/10, 2012 at 10:59 Comment(0)
C
1

It has to do with order precedence. Anytime a logical OR is executed, it will stop if the first operand is true, which in this case is j.

Colored answered 27/10, 2012 at 10:59 Comment(0)
K
0

The logical operator || works like this: if first condition is true then the second one is not evaluated.

So first (++i && ++j) checks. It returns true, so after it || is not evaluated. Thus, the value of k is not increased.

Kitty answered 25/6, 2014 at 17:58 Comment(0)
M
0
#include<stdio.h>
int add(int);
void main()
 {
    int i=3,k,l;
    clrscr();
    k= add(++i);
    l=add(i++);
    printf("i=%d k= %d  l=%d",i,k,l);
     getch();
 }
 int add(int ii)
 { 
   ++ii;
   return(ii);
 }

/*th eoutput is 
5    
5
5     can anyone expalin how? */
Maxie answered 20/8, 2015 at 8:52 Comment(0)
D
-1

#include"stdio.h"

#include"conio.h"

main()

{

int rmv=10,vivek=10;

clrscr();

rmv++;//this post incremant means incremant next step 10+1

//rmv+1 and rmv++ both are same

printf("1.rmv++=%d\n",rmv);//value is 10

printf("2.rmv++=%d\n",rmv++);//the value is increment 11

++vivek;//this pre increment means first increment the value so,1+10=11

printf("1.\t++vivek=%d\n",vivek);////++vivek and 1+vivek both are same

printf("2.\t ++vivek=%d",++vivek");

getch();

}

Duvall answered 9/10, 2014 at 7:49 Comment(1)
The pre increment is firft increment value(++rmvivek) and The post increment is next step increment value(rmvivek++)Duvall

© 2022 - 2024 — McMap. All rights reserved.