Why does it print 1 at the end? - c

I don't understand why it prints 1 at the end.
#include <stdio.h>
int main(){
printf("%d\n", -2&&2);
return 0;
}

&& is a Boolean operator, not an integer operator. In C any non-zero value is interpreted as true when applied to a Boolean operation, while zero is the only integer value regarded as false when applied to a Boolean operation.
So:
(<non-zero> && <non-zero>) == true
then conversely when a true result is treated as an integer (as the %d format specifier dictates in this case), true is represented by 1, while false is zero. So in this case true becomes 1.
If you want to print a Boolean result, then:
printf( "%s\n", (-2 && 2) == 0 ? "false" : "true" ) ;
Strictly the expression:
-2 && 2
is equivalent to:
(-2 != 0) && (2 != 0)
Which has strict type agreement since != has a Boolean result from integer operands, and so && is presented with Boolean operands only with no implicit conversion.

Because -2&&2 is evaluated to true = 1 (when you AND two numbers together you will get true unless one or both of the numbers is 0).
Try changing -2&&2 to true && false; it will print 0.

The expression -2&&2 resumes to 1 or true (true is always != 0, while false == 0) based on the boolean arithmetic opration.
And here is how it resumes to 1:
&& represents the logical AND, not bitwise AND, which is &, in C. So you are logical ANDing the values of -2 and 2.
Let´s consider the following sentence as one of the statements for the Logical AND (&&)-operation:
If both the operands are non-zero, then the condition becomes true.
-2 is non-zero, equals 1.
2is also non-zero, equals 1.
So the result is also 1 (1AND 1 = 1) or true represents 1.

The && operator is for logical and, not bitwise and.
The expression in your program evaluates as
printf("%d\n", (-2 != 0) && (2 != 0));
Which is 1 because boolean expressions have value 0 for false and 1 for true in C.
Conversely, if you had written -2 & 2, you program would have implementation defined behavior, depending on the representation of negative integers used for the target system. On modern systems with two's complement representation, this bitwise and expression evaluates to 2. Same value for sign-magnitude representation, but on rare systems with ones' complement representation, the value would be 0.

Related

Explanation of shortcuts in C

Can someone explain what exactly is happening in these two statements listed below:
1) int enabled = val == 0; //val is an int
2) int sync = param || (foo->delay == NULL); //param and delay is both int
int enabled = val == 0;
read as
int enabled = (val == 0);
and
(val == 0)
will be either 0 or non zero if val is 0 or not. enabled will then be initialized with that value
Equivalent to:
int enabled;
if(val == 0)
{
enabled = 1;
}
else
{
enabled = 0;
}
now you do that same analysis on the second one
You will set the variable enabled to 1 if val is equal to 0 and 0 otherwise.
sync will be equal to 1 if param is non-zero and in case it is 0 then it will be 1 if foo->delay is NULL else it will be 0.
From standard §6.5.9p3 backing up what I said:
The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.108) Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.
Also in case || there is standard saying the evaluation logic: from §6.5.14
Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares unequal to 0, the second operand is not evaluated.
Also in the same section the rule which dictates what will be the result if param is non-zero.
The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.
C only adopted a true boolean type from C++ with C99, though it named it _Bool so it wouldn't conflict with pre-existing code and provided the header <stdbool.h> to make it nicer.
The prior convention that only 0 is considered falsey (a literal zero, which in a pointer-context is a null-pointer), and anything else truthy was not changed. (That's the reverse to how command shells do it by the way.)
All boolean operators and conversion from _Bool use canonical values of 0 and 1.
That should be enough history and details to understand the code.

Does "true" in C always mean 1?

Please consider these piece of C code:
if ((value & 1) == 1)
{
}
Assuming value equals 1, will (value & 1) return 1 or any unspecified non zero number?
§6.5.8 Relational operators
Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.) The result has type int.
§6.5.9 Equality operators
The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.) Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.
§6.5.13 Logical AND operator
The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.
§6.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.
Does “true” in C always mean 1?
No. Any expression that evaluates to a non-zero value is "true" in C. For example,
if (-1) {
}
if (5-10) {
}
if( "string literal") {
}
are all "true" and pass the condition.
In your specific example, the relational operator == yields 1 which is also "true"(the same holds for all other relational operators as noted by Govind).
If you are really asking about whether the bit-wise AND (&) yields 1 when value is 1, then yes, value & 1 yields 1 (assuming value is an integer type -- & operator requires its operands to be integers).
In fact, you can probably try to understand the individual parts and (generally how the & and == operators behave) by using a simple program:
#include <stdio.h>
int main(void)
{
int value = 1;
printf(" value & 1 = %d\n", value & 1);
printf(" 2 & 1 = %d\n", 2 & 1);
printf("((value & 1) == 1) = %d", (value & 1) == 1);
}

Short Circuiting Logical OR Behavior

I was under the impression that in the C language, the logical OR operator || is a short circuit operator that doesn't evaluate the rhs if the lhs is false. I've run into a problem when comparing OR'ed values. Can someone explain to me why the following code (on gcc 5.3) evaluates to true? I'm getting the same behavior in clang.
#include <stdio.h>
int main() {
int attr = 2;
if( ((attr & 2) != 2) || ((attr & 4) != 4) ) {
printf("No short circuit?\n");
};
printf("%i %i\n",attr,(attr & 2));
};
output:
No short circuit?
2 2
((attr & 2) != 2) || ((attr & 4) != 4) evaluates to true because
attr & 4 == 0 and (attr & 4) != 4 is 1 because attr & 4 is not equal to 4. (N1570 6.5.9 Equality operators)
Because at least one of the operand is not zero, the expression evaluates to 1. (N1570 6.5.14 Logical OR operator)
The if statement excutes the first substatement when the controlling expression is not zero (N1570 6.8.4.1 The if statement), and you will call it "the expression evaluated to true".
Logical OR operator won't evaluate rhs if the lhs is true because the value will be true in both case the rhs is true or false.
Logical AND operator won't evaluate rhs if the lhs is false because the value will be false in both case the rhs is true or false.
Quote from N1570:
6.5.13 Logical AND operator
[...]
3 The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it
yields 0. The result has type int.
4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation;
if the second operand is evaluated, there is a sequence point between the evaluations of
the first and second operands. If the first operand compares equal to 0, the second
operand is not evaluated.
6.5.14 Logical OR operator
[...]
3 The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it
yields 0. The result has type int.
4 Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the
second operand is evaluated, there is a sequence point between the evaluations of the first
and second operands. If the first operand compares unequal to 0, the second operand is
not evaluated.
Code that you had:
((attr & 2) != 2) || ((attr & 4) != 4) )
What happens:
(attr & 2) evaluates to 2. (0010 AND 0010 = 0010 which is 2)
(2 != 2) evaluates to 0, or false.
Now the right side of the OR is evaluated (If the left side was true, then the right side would not be executed/evaluated, i.e. "Short circuit". But in this example it is false, so the right side is evaluated).
(attr & 4) is the bitwise and operation, e.g. 0010 & 0100. It evaluates to 0000, or 0.
(0 != 4) evaluates to 1, or true, so the body of the if statement executes.
The rest of the program (the last statement) then executes and the program terminates.
Remember:
& is bit-wise AND.
&& is Boolean AND.
| is bit-wise OR.
|| is Boolean OR.
Here's two ways you can get the short circuiting behavior in C:
unsigned int bitwise_logical_or(unsigned int p, unsigned int q) {
return (p | (q & ((p != 0) - 1)));
}
unsigned int numerical_logical_or(unsigned int p, unsigned int q) {
return p + (p == 0) * q;
}
As mentioned in other answers, the || operator returns true (0x01) or false (0x00).
bitwise_logical_or works as follows:
If bitwise or is performed on integers p and q, it will work only so long as p is zero. If p is not zero, bitwise or will intermingle p and q in an unpredictable way. If q could be set to zero if p is greater than zero we’ll get the result we want. Therefore, when p is non-zero, we’d like to perform a bitwise and between q and zero. The trick is that simply testing p == 0 will return (in 8 bit) 0x00 or 0x01. We need it to return 0xff or 0x00 appropriately. We accomplish this by checking p != 0, this will return 0x01 when p > 0 and 0x00 when p == 0. Subtract 1, and in the case that p > 0, you get 0x00 (false). When p == 0, you already have 0x00 (all zeros), there’s no where to go, so the integer wraps around (underflows) and you get 0xff (all ones).

What is !0 in C?

I know that in C, for if statements and comparisons FALSE = 0 and anything else equals true.
Hence,
int j = 40
int k = !j
k == 0 // this is true
My question handles the opposite. What does !0 become? 1?
int l = 0
int m = !l
m == ? // what is m?
Boolean/logical operators in C are required to yield either 0 or 1.
From section 6.5.3.3/5 of the ISO C99 standard:
The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0.
In fact, !!x is a common idiom for forcing a value to be either 0 or 1 (I personally prefer x != 0, though).
Also see Q9.2 from the comp.lang.c FAQ.
§6.5.3.3/5: "The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int."
The other logical operators (e.g., &&, ||) always produce either 0 or 1 as well.
Generally, yes, it'll become 1. That said even if that is guaranteed behavior (which I'm not sure of) I'd consider code that relied on that to be pretty awful.
You can assume that it's a true value. I wouldn't assume anything more.
The Bang operator (!) is the logical not operator found commonly in C, C++ and C#, so
!0 == 1
!1 == 0
This is based on the language characteristic of what is interpreted to be either true or false... in more modern languages it would be like this
!false == true
!true == false
See DeMorgan Law concerning truth tables...
!x will be expand to (x==0) so:
if x=0 -> !x take value from (0==0) = TRUE (value 1)
if x!=0 -> !x take value from (x==0) = FALSE (value 0)

What is the point of the logical operators in C?

I was just wondering if there is an XOR logical operator in C (something like && for AND but for XOR). I know I can split an XOR into ANDs, NOTs and ORs but a simple XOR would be much better. Then it occurred to me that if I use the normal XOR bitwise operator between two conditions, it might just work. And for my tests it did.
Consider:
int i = 3;
int j = 7;
int k = 8;
Just for the sake of this rather stupid example, if I need k to be either greater than i or greater than j but not both, XOR would be quite handy.
if ((k > i) XOR (k > j))
printf("Valid");
else
printf("Invalid");
or
printf("%s",((k > i) XOR (k > j)) ? "Valid" : "Invalid");
I put the bitwise XOR ^ and it produced "Invalid". Putting the results of the two comparisons in two integers resulted in the 2 integers to contain a 1, hence the XOR produced a false. I've then tried it with the & and | bitwise operators and both gave the expected results. All this makes sense knowing that true conditions have a non zero value, whilst false conditions have zero values.
I was wondering, is there a reason to use the logical && and || when the bitwise operators &, | and ^ work just the same?
You don't need logical XOR, I have forgotten the SO question, but it's similar to what you're thinking, basically we don't need XOR, it's equivalent to != anyway
FALSE XOR FALSE == FALSE
FALSE XOR TRUE == TRUE
TRUE XOR FALSE == TRUE
TRUE XOR TRUE == FALSE
FALSE != FALSE == FALSE
FALSE != TRUE == TRUE
TRUE != FALSE == TRUE
TRUE != TRUE == FALSE
I'll search my favorites, and paste here the link later...
The bitwise operators do not work "just the same" as the && and || operator. For a start, && and || perform short-circuited evaluation, whereas the the bitwise operators do not. In other words, you can't do something like this with the bitwise operators:
int * p = 0;
(p != 0) && (*p = 1);
because if you said:
(p != 0) & (*p = 1);
both subexpressions would be evaluated, and you would dereference the null pointer.
Bitwise XOR does not work as would a logical XOR when its operands are integer values:
2^4 ? "Valid" : "Invalid"
gives "Valid" but should give "Invalid"
In C arguments of logical operators are treated as Boolean values - anything zero is treated as "false", and anything else (yes, negative values too) are "true". Bitwise ops work on individual bits, and as Neil already noted, are not subject to short-circuit evaluation as logical ops are.
In your example the results are totally valid and expected since bit-wise xor between two ones is zero.
If you want a logical xor operator in C, then you can use this:
#define xor != 0 ^ !!
It works by converting both sides of the expression to booleans and xoring them.
You can use it as if you were using && or ||, like this:
if (a xor b)
AFAICT, there aren't any problems with it.

Resources