2
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 ( "\nw = %d x = %d y = %d z = %d", w, x, y, z ) ;
}

I'm just learning C and I came across this code. I honestly dont know what w, x, y and z are assigned to. Apparently the output is as follows:

w = 1 x = 0 y = 1 z = 1

How is 'w' equal to 1? I dont get it.

ST3
  • 8,826
  • 3
  • 68
  • 92
sentrlhack
  • 51
  • 1
  • 3
  • 1
    This previous post on logical operators may help: http://stackoverflow.com/questions/12332316/logical-operators-in-c – Shafik Yaghmour Jun 27 '13 at 16:40
  • 3
    It means you shouldn't hire the person who wrote that - or if it's a job interview, you should look elsewhere... ;) – Mats Petersson Jun 27 '13 at 16:42
  • 5
    `+1` for your *honestly dont know ...*; `-1` for not making any effort to figure it. – devnull Jun 27 '13 at 16:43
  • 2
    Welcome to StackOverflow. You will find this place full of very helpful people. They are especially good at questions like, "I've tried this and this bit doesn't work, what have I missed" ... how about posting where you've looked for an answer, and what you have learned from the reference site, and what bit you haven't quite got yet ... – Neil Townsend Jun 27 '13 at 16:58
  • @sentrlhack: What does your favorite book on C say about it? – AnT stands with Russia Jun 29 '13 at 06:37

5 Answers5

11

|| is the logical OR operator. From C11 s6.5.14 Logical OR operator

The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

...the || operator guarantees left-to-right evaluation;

Applying this to the calculation for w we get

w = i || j || k  == (i || j) || k
                 == (non-zero || non-zero) || 0
                 == 1 || 0
                 == 1

Calculations for x, y, z are similar. C11 s6.5.13.3 states that the result from the && operator shall be 0 or 1.

simonc
  • 41,632
  • 12
  • 85
  • 103
  • +1 for explaining how the operators are evaluated, useful information for sure, especially for writing conditions – qwwqwwq Jun 27 '13 at 17:09
6

In C there is no "strong" built-in type for Boolean values, so integers are used instead. Results of evaluating logical expressions, such as ones using || and &&, can be assigned to integer variables.

When a value is used in a logical operator, the Boolean interpretation is very straightforward: zeros are interpreted as false, while all non-zero values are interpreted as true.

Now you should be able to figure out the expressions for yourself:

  • i || j || k evaluates as 1, because i and j are not zeros
  • i && j && k evaluates as 0, because k is zero,
  • ...and so on.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

This is how conceptually it works:

w = i || j || k;

w = 4 || -1 || 0; //values replaced

w = true || 0; //4 || -1 evaluates to true

w = (true); //true is being assigned to integer

w = 1; //0 is false, 1 is for true

Anand Patel
  • 6,031
  • 11
  • 48
  • 67
2

|| is the logical OR.

w = i || j || k;

is equivalent to:

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

i is 4, which explains that w is true (which is evaluated to 1 as C uses integers to deal with booleans).

The same is applicable to && (the logical AND), etc.

Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
2

It is logical operations.

|| - means logical or, if at least one element is not 0, result is 1, otherwise its 0; && - means logical and, if all elements are 1, result is 1, otherwise its 0;

Logical and has higher priority, so:

x = 0 || 4 && 0;

Would be 0, because:

  1. 4&&0 = 0
  2. 0||0 = 0.

These operators are about full value of number. There are | and & operators connected with bits. Everything about working and priority is the same, just short example:

uint8_t x = 2 || 4 && 7;



1. 4=00000100 and 7=00000111
      00000100
   &  00000111
gives:00000100
 2. 2=00000010
      00000010
   |  00000100
gives:00000110, it is 6

I hope it is helpful.

ST3
  • 8,826
  • 3
  • 68
  • 92