0

I have the following code snippets, and was somewhat confused by the output, based on my knowledge of how logical operators work. From what I know(based on truth tables in electronics),

  1. For logical AND, TRUE and TRUE gives a value of TRUE and all other combinations of the truth table gives FALSE.
  2. For logical OR, only FALSE and FALSE gives a value of FALSE. While all other combinations of the truth table gives TRUE.

Based on this knowledge, this first code snippet-

void main( )
{
int i = -1, j = 1, k ,l ;
k = i && j ;
l = i || j ;
printf ( "%d %d\n", i, j ) ;
printf("%d %d", k,l);
}

gives the output-

-1 1
1 1

I am confused here because according to truth tables of logical AND and OR, the value of k should be -1. This comes from the fact that value of i is -1(which is FALSE) and j is 1(which is TRUE), so TRUE AND FALSE should equal to FALSE, that is, -1.

However, since the output was 1 for both k and l, I'm thinking that C processes logical AND and OR based on only 1 and 0 where 1 would be TRUE and 0 would be false. According to this logic, any non-zero value would be TRUE, so even -1 would be TRUE.

In that case, k=i&&j would mean -1&&1. Since -1 and 1 are both TRUE, so the expression k= i&&j evaluates to TRUE, ie, 1. Following the same logic, l=i||j also evaluates to TRUE.

Am I correct in thinking that the second approach is the correct way in which logical operators work in C?

My second question is about the next code snippet-

void main( )
{
int j = 4, k ;
k = !5 && j ;
printf ( "\nk = %d", k ) ;
}

which produces the output k=0

This has really got me stumped, because I can't figure out how a Logical NOT works within a Logical AND operator. Here j is 4, but what is the value of k, and how does this compare with j? I'm thinking since k is not 5, it could be -5? But in that case, -5 and 4 both evaluate to TRUE, so the value of k should be 1.

Please help.

Manish Giri
  • 3,562
  • 8
  • 45
  • 81
  • 4
    your assumption is incorrect. any non-zero value in C is true and zero is false, which includes -1 (treated as true) – Ahmed Masud Jul 21 '14 at 18:16
  • Related to: [Do negative numbers return false in C/C++?](http://stackoverflow.com/questions/18840422/do-negative-numbers-return-false-in-c-c/18840464#18840464) and [Logical Operators in C](http://stackoverflow.com/a/16654155/1708801) – Shafik Yaghmour Jul 21 '14 at 18:18

2 Answers2

5

Am I correct in thinking that the second approach is the correct way in which logical operators work in C?

Yes. You are going right.
Remember that in C, a non-zero value is treated as true. -1 is also true and it will be treated just like 1 for logical operations.
In

k = !5 && j ;

! is a logical negation operator. !5 is false (there is a different operator ~ in C for Bit-wise negation). k will become 0.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Understood, thanks a ton. However, I am still unclear about the second code snippet, where a logical NOT is used inside a logical AND. Could you please help about it? – Manish Giri Jul 21 '14 at 18:19
  • 1
    Short-circuiting is only important for side-effects (like invoking UB in the skipped part of the expression). `~` is not negation but bitwise-complement, a bit-operation... – Deduplicator Jul 21 '14 at 18:26
  • @Deduplicator;I think bitwise NOT, complement and negation are same thing. http://en.wikipedia.org/wiki/Bitwise_operation#NOT – haccks Jul 21 '14 at 18:28
  • @Deduplicator; Why not ? – haccks Jul 21 '14 at 18:32
  • Well, when you add "bitwise", it does. Anyway, we are programmers, using exactly the same terms for three operations we can reasonably perform in the same context is ... somehow bad. – Deduplicator Jul 21 '14 at 18:33
  • Why is a non-zero value treated as being true in C? A value is true in C if ANY of the bits in the variable are set. If NONE of the bits in the variable are set then it's just all zeroes: 000...000, which is 0, which is false. – Galaxy Jun 27 '18 at 04:21
  • @Galaxy; Do you know any value which is non zero and all of its bits are set to 0? – haccks Jun 27 '18 at 04:58
  • @haccks; Not in standard representation of integers in modern day computers, but of course the bits themselves could have any arbitrary meaning assigned to them. – Galaxy Jun 28 '18 at 06:57
4

Regarding your second question about (logical) negation:

int j = 4, k ;
k = !5 && j ;

Since any non-zero value is true, the NOT is negating a boolean value, not an int, so the next steps go like this:

k = !true && true
  = false && true
  = false
Michelle
  • 2,830
  • 26
  • 33