Codrin H
Codrin H

Reputation: 27

c, how to read operators issue, basics

in "C Modern approach 2nd'ed " Are some exercises i can't understand the meaning. The result is 1, how do you read it ? Thank you.

#include <stdio.h>

int main(void)
{

    int i, j, k;

    i = 5;
    j = 0;
    k = -5;

    printf("%d", i && j || k);
    return (0);

}

Upvotes: 0

Views: 121

Answers (3)

user2600366
user2600366

Reputation: 68

The operators ||(or) &&(and) named are bolean operators.

They return 0 or 1.

In order x&&y will return 1, it's necessary that x and y will be any number but 0. If at least one of them is equal to 0, it returns 0.

The operator || returns 1 if at least one of them is diffrent then 0, and 0 if they both qual to 0.

Upvotes: 1

Santhosh Pai
Santhosh Pai

Reputation: 2625

It is operated as

 ((i && j) || k)

so

(5 && 0)=0

and

  0||-5=1

Upvotes: 0

ouah
ouah

Reputation: 145829

 i && j || k

is equivalent to

 (5 && 0) || -5

equivalent to

0 || -5

equivalent to 1.

Logical operators yield a value of 0 or 1.

Upvotes: 7

Related Questions