edited by
2,871 views
2 votes
2 votes
#include <stdio.h>
int main()
{

   int a = 1;

   int b = 1;

   int c = a || --b;

printf("%d %d",c,b);

}

Answer is given  1, 1 but i m having doubt in logical OR operator and uninary operator .uni operator having higher precedence then so we  firstly decrement the 'b' and then perform OR  by which i got output 1,0  where i m wrong plz explain.

edited by

2 Answers

Best answer
4 votes
4 votes

int main()
{
   int a = 1;    // memory created with variable name a and contain 1.
   int b = 1;    // memory created with variable name b and contain 1.
  int c = a || --b;
// memory created with variable name c and assign a to it since a has 1 , (we know or gate if 1st is 0 then only second calculate otherwise statement evaluate true. same here since a is 1 so without doing --b assign a to c which is 1
printf("%d %d",c,b); // print variable c,b and contain 1 and 1.
}

selected by
0 votes
0 votes
first 1 is initialised to a

then 1 is initialised to b

in c we have pre decrement so b becomes 0....  

c=1 || 0 which gives the result 1

so output is 1,1

Check the precedence table

Related questions

0 votes
0 votes
0 answers
4
saurabh111 asked Nov 2, 2018
239 views
in char data type ,plzzz tell me initilization can be seprated from declaration..