Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behaviour of && in C programming language

I am beginner in C programming language, recently I have read about Logical AND && operator.

I also know that, in C programming language all non-zero values are treated as TRUE.

NON-ZERO && NON-ZERO = 1  
NON-ZERO && ZERO = 0  
ZERO && NON-ZERO = 0
ZERO && ZERO = 0  

But when I am dealing with the following program then I am not getting expected answer.

int main(){  
  int x, y, z;  
  x = y = z = -1;  
  y = ++x && ++y && ++z;  
  printf("x = %d, y = %d, z = %d, x, y, z);  
  return 0;  
} 

I am expecting

x = 0, y = 0, z = 0 

but the answer is

x = 0, y = 0, z = -1

Can anyone please explain, Why I am getting this answer?

Edit: In this question, I have not asked about the precedence of operators.

like image 402
Mayank Tiwari Avatar asked Aug 14 '13 10:08

Mayank Tiwari


People also ask

What is behaviour and examples?

The definition of behavior is the way a person or thing acts or reacts. A child throwing a tantrum is an example of bad behavior. The actions of chimps studied by scientists are an example of behaviors.

What is behaviour word?

1. Behavior, conduct, deportment, comportment refer to one's actions before or toward others, especially on a particular occasion. Behavior refers to actions usually measured by commonly accepted standards: His behavior at the party was childish.

What is the meaning of behavior of a person?

Definition of behavior 1 : the way in which someone conducts oneself or behaves (see behave sense 1) We were grateful for the gracious behavior of our hostess. The children were rewarded for good behavior. Be on your best behavior.

What is the noun of behavior?

noun. noun. /bɪˈheɪvyər/ 1[uncountable] the way that someone behaves, especially toward other people good/bad behavior social/sexual/criminal behavior His behavior toward her was becoming more and more aggressive.


3 Answers

Because of Short-circuit evaluation, when x is 0, y and z don't really need to be evaluated since 0 && ANYTHING is 0.

Once x is incremented to 0, the result is 0, and that's what y gets.

z remains unchanged (-1).


 x  | y  |  z 
----+----+-----
 -1 | -1 | -1   //x = y = z = -1;  
  0 | -1 | -1   //++x && ... Now the whole expression is evaluated to 0
  0 |  0 | -1   //y = ++x && ++y && ++z;
like image 185
Maroun Avatar answered Sep 28 '22 19:09

Maroun


I only can think about that && evaluates in short circuit: given A && B, if A evaluates false then B is not evaluated.

So:

X becomes 0. && ++y && ++z does not evaluates since X/0/false && ...

y=0 as assigned from y = x/0/false

z remains unmodified since ++z does not get executed.

like image 35
Alfonso Nishikawa Avatar answered Sep 28 '22 18:09

Alfonso Nishikawa


&& operator is evaluated pairwise, therefore I'm guessing C is evaluating

((++x && ++y) && ++z)

now, ++x will return zero therefore the first && will fail as will the second one without the need to evaluate ++y or ++z.

y = 0 since that is the result of the expression.

z is not touched

like image 39
John Greenall Avatar answered Sep 28 '22 18:09

John Greenall