-5
#include <stdio.h>

int main()
{
   int i=4, j=-1, k=0, w, x, y, z;
   w = i||j||k;
   x = i&&j&&k;
   y = i||j&&k;
   z = i&&j||k;

   printf("w = %d, x = %d, y = %d, z = %d", w,x,y,z);
   return 0;
}

I was solving problems regarding C decision control with logical operators and I encountered a problem which I just cannot understand as the output was not something that I was expecting.

The output is:

w = 1, x = 0, y = 1, z = 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    Please, post code rather than images, what do you want to achieve and what is your issue? – CuriousPan Feb 10 '23 at 11:25
  • 1
    Welcome to StackOverflow. Please take a [tour] and see [ask]. Specifically post all your code, error messages etc. as copy-pasted text, **not** images/links. – wohlstad Feb 10 '23 at 11:25
  • 3
    Also please mention what did you actually expect and why. – wohlstad Feb 10 '23 at 11:27
  • @DurYodhan Snipes What you need is to read at last the description of the logical operators || and &&. – Vlad from Moscow Feb 10 '23 at 11:30
  • Remember that `&&` takes [precedence](https://learn.microsoft.com/en-us/cpp/c-language/precedence-and-order-of-evaluation?redirectedfrom=MSDN&view=msvc-170) over `||`. So `y = i || j && k;` is `y = i || (j && k);` These are boolean operations: a zero value is `false` and all other values are `true`. – Weather Vane Feb 10 '23 at 11:32
  • @WeatherVane thanks for explaining that to me but I'm still confused like i mean i know what boolean is and zero = false and nonzero = true but ( int i=4, j=-1, k=0, w, x, y, z; w = i||j||k;) here why is w = 1; – DurYodhan Snipes Feb 10 '23 at 12:06
  • All `||` means if any of the expressions in between is `!= 0` then the result is true – Peter Krebs Feb 10 '23 at 12:08
  • `true || true || false` evaluates to `true`. – Weather Vane Feb 10 '23 at 12:16
  • 1
    @PeterKrebs ok I get it now so like WeatherVane said these are Boolean operations so the variables w, x, y, z store values as true and false in form of 1 and 0. Thanks for the help I was so confused – DurYodhan Snipes Feb 10 '23 at 12:16
  • @WeatherVane i understand now dude it was storing boolean values in variables, thanks for the help I was super confused – DurYodhan Snipes Feb 10 '23 at 12:18

2 Answers2

4

There's a few things you need to know here:

  • Any expression in C that has the value 0 is regarded as false, and any other expression with any other value different than zero is regarded as true.

  • The result of boolean logical operators like &&, ||, == etc is always 0 (false) or 1 (true).

  • Operator precedence specifies how these expressions are parsed: that is, which operand belongs to which operator. For example in case of y = i||j&&k;, the operator precedence is && (highest) then || then = (lowest). Therefore that expression is equivalent to:

    y = ( i || (j&&k) );

  • The logical AND && and OR || operators are some of the few operators in C that have a well-defined order of evaluation. They are guaranteed to be executed from left to right. They are guaranteed to stop evaluation in case only the left operand is all that's needed to tell if the expression will become true or false.

    That's a common beginner FAQ, read all about it here: Is short-circuiting logical operators mandated? And evaluation order?

    For the previous example y = ( i || (j&&k) );, i is the left-most operand of || which is what matters. It is 4, therefore true. true || anything is always true, so the rest of the expression is not evaluated.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • thanks, for taking ur time to answer my problem i understand now the variables were storing boolean values in them as true and false (1 and 0), i was super confused – DurYodhan Snipes Feb 10 '23 at 12:20
2
w = i||j||k;
x = i&&j&&k;
y = i||j&&k;
z = i&&j||k;

is equivalent to:

w = ((i != 0) || (j != 0)) || (k != 0);

x = ((i != 0) && (j != 0)) && (k != 0);

y = (i != 0) || ((j != 0) && (k != 0));

z = ((i != 0) && (j != 0)) || (k != 0);

Some things to note:

  1. The value 0 represents false.

  2. Any value different from 0 represents logical true.

  3. Logical operators returns either true or false.

  4. Operators that have the same precedence are bound to their arguments in the direction of their associativity.¹

    The associativity of && is
    from left-to-right. So this statement:

    x = i&&j&&k;
    

    is parsed as:

    x = ((i != 0) && (j != 0)) && (k != 0);
    
  5. Remember that && takes precedence over ||. So

    y = i || j && k;
    

    is parsed as:

    y = i || (j && k);
    

    — @Weather Vane

  6. The logical-AND operator has type int and the value 1 if both lhs and rhs compare unequal to zero. It has the value ​0​ otherwise (if either lhs or rhs or both compare equal to zero).

    There is a sequence point after the evaluation of lhs. If the result of lhs compares equal to zero, then rhs is not evaluated at all (so-called short-circuit evaluation).²

  7. The logical-OR operator has type int and the value 1 if either lhs or rhs compare unequal to zero. It has value ​0​ otherwise (if both lhs and rhs compare equal to zero).

    There is a sequence point after the evaluation of lhs. If the result of lhs compares unequal to zero, then rhs is not evaluated at all (so-called short-circuit evaluation).³

[1] - [2] - [3] — cppreference.com

Harith
  • 4,663
  • 1
  • 5
  • 20