In C, does (x==y==z) behave as I'd expect? - c

Can I compare three variables like the following, instead of doing if((x==y)&&(y==z)&&(z=x))? [The if statement should execute if all three variables have the same value. These are booleans.]
if(debounceATnow == debounceATlast == debounceATlastlast)
{
debounceANew = debounceATnow;
}
else
{
debounceANew = debounceAOld;
}

No, it does not.
x == y is converted to int, yields 0 or 1, and the result is compared to z. So x==y==z will yield true if and only if (x is equal to y and z is 1) or (x is not equal to y and z is 0)
What you want to do is
if(x == y && x == z)

No. The equality check associates from the left and the logical result is compared as a number, so that the expression 2 == 2 == 1 parses as (2 == 2) == 1, which in turn gives 1 == 1 and results in 1, which is probably not what you want.

You can actually type something like this:
int main()
{
const int first = 27,
second = first,
third = second,
fourth = third;
if (!((first & second & third) ^ fourth))
return 1;
return 0;
}

Related

What does a : mark in C mean?

void main()
{
int x,y;
scanf("%d", &x); <-1
y=(x>5?3:4);
printf("%d",y); ->4
}
What is the use of: in y=(x>5?3:4); ?
It is known as ternary operator. This:
y= (x>5 ? 3 : 4);
means if x is greater than 5, to y will be assigned the value 3, otherwise (x is less or equal to 5) the value 4 will be assigned to y. That code is semantically the same as:
if(x > 5)
y = 3;
else
y = 4;
you can find a more in-depth explanation here.
"What is the use of : in y=(x>5?3:4); ?"
: represents the shorthand version of the else clause of an if-then-else expression when using the C ternary operator. So in your example:
y = (x>5 ? 3 : 4);
The English expression could be stated as:
if x is greater than 5, then set y equal to 3, else set
y equal to 4.
The traditional long version in C syntax would use the expression sequence:
if(x > 5)
{
//true path
y = 3;
}
else
{
//false path
y = 4;
}
It's a ternary operator or conditional operator.

Can someone explain me why I am getting two different answers for the below codes?

#include <stdio.h>
int main()
{
int x = 2, y = 0;
int m = (y |= 10);
int z = y && m;
printf("%d\n", z);
return 0;
}
Above program gives me output as 1. Below code is giving me output 0 but what is the reason for different outputs here?
#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z = y && (y |= 10);
printf("%d\n", z);
return 0;
}
In
int z = (y |= 10);
y is masked with 10 so set to 10, so y && m is a boolean worth 1 because both y and m are non-zero, assigned to z
Now, in
int z = y && (y |= 10);
y == 0 so && short-circuits, not evaluating the right hand part and not changing the value of y. Therefore, z is set to 0.
Had you used:
int z = y & (y |= 10);
this would have depended on how/in which order the compiler evaluates the operands (implementation defined behaviour to get 0 or 10)
note that && short-circuiting doesn't evaluate the second parameter if the first is zero for a very good reason:
if ((pointer != NULL) && pointer->value == 12) { do_something(); }
this condition checks if the value is 12 but only if the pointer is non-NULL. If the second expression was evaluated first, this could crash.

How does 'if((x) || (y=z))' work?

I don't quite understand how the if statement in this case works. It evaluates the x != 0 statement and when that is not true anymore, it assigns z to y and then breaks the if statement?
int main()
{
int x, y, z, i;
x = 3;
y = 2;
z = 3;
for (i = 0; i < 10; i++) {
if ((x) || (y = z)) {
x--;
z--;
} else {
break;
}
}
printf("%d %d %d", x, y, z);
}
Let's decompose that into smaller bits.
if (x) is the same as if (x != 0). If x != 0, then you know the condition is true, so you don't do the other portion of the if.
If part 1. was false, then y = z assigns z into y and returns the final value of y.
From point 2., we can understand that if (y = z) is equivalent to y = z; if (y != 0)
Thus, from points 1. and 3., we can understand that :
if ((x) || (y = z)) {
doSomething();
}
else {
doSomethingElse();
}
Is the same as :
if (x != 0) {
doSomething();
}
else {
y = z;
if (y != 0) {
doSomething();
}
else {
doSomethingElse();
}
}
It's true it's not particularly readable code though.
No. if ((x) || (y = z)) {
in C-English is basically:
if x is nonzero, evaluate the following code.
if x is zero, set y to z.
if y is nonzero, evaluate the following code.
otherwise, break out of the loop.
If x is zero or y is zero, it breaks out of the loop.
int main()
{
int x = 3;
int y = 2;
int z = 3;
unsigned int i;
for (i = 0; i < 10; i++)
if (x != 0) {
x = x-1;
z = z-1;
}
else {
y = z;
if (y != 0) {
x = x-1;
z = z-1;
}
else {
break;
}
}
}
printf("%d %d %d", x, y, z);
}
In C, there is short-circuiting, so the statement y=z will not be evaluated until x becomes zero.
When x == 0, since z also decrements the same way, z == 0. Hence y will also be zero at that time due to the assignment. The statement y=z also returns y at this point which will be evaluated as a condition, and since that is also 0, the else break will be hit.
Hence I believe the answer should be 0 0 0.
When you use assignment in an if statement, the result of the assignment is returned. so when you write :
if (x = y)
It will be always true unless the value of y is 0, so 0 is returned as the result of assigning and the if statement is not executed.(anything except 0 is considered as true.)
So when you write :
if ( x || (x = y))
The if statement doesn't execute only if x is 0 & y is 0.
Here
if ((x) || (y = z))
there are two condition
one condition is
if ((x)) and another condition is if ((y = z))
if one of them is true then if portion is execute otherwise else condition work
only and only when both condition are false then else execute.

find the least number in loop and verify if it is equal to certain integer value in groovy

I am relatively new with groovy, so i am not sure if i am doing this right
I am running my query in a loop to find the least value , if that least value is equal to say 12 , then print it
connection.eachRow(query) {
def errorstate = false
if (y < x && y !=0)
{
errorstate = true
x = y ---> if y is less than x then set x = value of y
}
/* if y is greater then the value of x will not change */
if (x == 12)
printf ("%-30s\t%-20s\t\n" , name,y)
}
Thanks in advance !!
Try this:
connection.eachRow(query) {
def errorstate = false
if (y != 0)
{
errorstate = y < x
x = Math.min(x, y)
}
}
/* if y is greater then the value of x will not change */
if (x == 12)
printf "%-30s\t%-20s\t\n" , name, y
This assumes that eachRow will call the closure it's given on each row before returning. I don't know if that's the case because you did not post which library you're using to do this.
It could well be that eachRow will run the closure in a separate thread, returning immediately before running any loop. Check the documentation of eachRow to see if that's the case.

multiply two numbers using only bit operations

While learning Bit operations in c,I was searching for code to multiply two numbers using only bit operations , I found the following code!. I am unable to understand how ternary operator is working in the following scenario and producing the correct o/p.
#include<stdio.h>
static int multiply (int x, int y)
{
return y==0?0:((y&1) ==1?x:0)+multiply(x<<1,y>>1);
}
int main()
{
printf("%d",multiply(2,3));
return 0;
}
Can someone please explain how is the above code working?.
That is not using "only bit operations", since it's using + to add numbers.
Maybe indenting can help break up the complicated expression:
return (y == 0 ? 0
: (y & 1) == 1 ? x
: 0)
+ multiply(x << 1, y >> 1);
Basically it's a recursive addition, that stops when y reaches 0. If the least significant bit of y is set, x is added to the result, else it is not. On each recursion, one bit of y is dropped so that it eventually will reach 0. The value of x is shifted to the left, very much like when doing multiplication by hand.
For instance if x = 3 (binary 11) and y = 6 (binary 110), it will compute
0 * 3 + 1 * 6 + 1 * 12 = 18
And of course 18 is 3 * 6.
Each recursion step is written as a * b where a is the least significant bit of y at that step (reading from the left, you get 0, 1, 1 which is the bits of y starting with the least significant bit) and b is the value of x at that step.
If y is odd, x * y = x + (x * 2) * (y / 2)
If y is even, x * y = (x * 2) * (y / 2)
With the logic above, and use recursion until y = 0.
If you are struggling understanding a complex nested use of the conditional operator, then simply expand it to an if statement:
static int multiply (int x, int y)
{
if (y==0)
return 0;
else
return ((y&1) ==1?x:0)+multiply(x<<1,y>>1);
}
And then expand the inner conditional operator:
static int multiply (int x, int y)
{
if (y == 0)
return 0;
else if ((y&1) == 1)
return x + multiply(x<<1, y>>1);
else return
return multiply(x<<1, y>>1);
}
Once you've expanded it like this, it should be clear what the expression is doing.

Resources