Why does this following program print "Yes" instead of "No"? - c

Why does this following program print "Yes" instead of "No"?
None of the variables is initialized to 2.
bool hello = 0;
int a = 1;
int b = 3;
int c = 4;
int d = 5;
if (a || b || c || d == 2) {
hello = 1;
}
if (hello == 1) {
printf("Yes");
}
if (hello == 0) {
printf("No");
}
return 0;
}

The statement
if (a || b || c || d == 2)
is equivalent to:
if (a != 0 || b != 0 || c != 0 || d == 2)
The equality comparison does not automatically distribute across all the variables. If you want to do that, you need to perform all the comparisons explicitly:
if (a == 2 || b == 2 || c ==2 || d == 2)

The expression (a || b || c || d == 2) evalutates to true because it treats a, b, c as booleans, and any non-zero integer is true.

You have given logical operator in the expression It means that if non zero value came then the expression is true. Then hello=1 is set and in next f statement it prints YES

You just meet the short circuit behavior of logical expressions OR.
The order of evaluation of logical OR || is left to right.
So in the following expression:
left || right
if left = true then right will never going to be executed (short circuit). In your code exactly same happened.
As you know, any non zero value treated as true in C, hence, a which is 1 is true. So, take a look:
if (a || b || c || d == 2)
if (true || bla bla bla) //rights are not even checked!
if (true)
hello = 1;
Tada! So the program print "Yes"!
None of the variables is initialized to 2.
Yes of course! But your if condition is not going to check that. To do so, try this:
if (a == 2 || b == 2 || c ==2 || d == 2) {
//...

because if judge num is not zero , if think this is true. so your code
if (a || b || c || d == 2)
like
if ( true || true || true || false)
the result is true, programe print "YES"

Related

Why no brackets needed among conditions in if sentence?

int num;
scanf("%d", &num);
if (num % 4 == 0 && num%100 != 0 || num % 400 == 0)
printf("%d", 1);
else
printf("%d", 0);
In this logic, I found I do not need to do () in AND condition which is in front of OR condition.
if (*(num % 4 == 0 && num%100 != 0)* || num % 400 == 0)
It's only needed if (num % 4 == 0 && num%100 != 0 || num % 400 == 0) without () in front of OR condition.
so, it seems (A && B || C) works like ((A && B) || C)
but it seemed it could work as (A && (B || C)) condition.
Why is () not needed in this situation? A and B condition are automatically grouped from the beginning?
All operators in C (and in fact all languages) have what's called operator precedence which dictates which operands are grouped first.
The logical AND operator && has a higher precedence than the logical OR operator ||, which means that this:
A && B || C
Is the same as this:
(A && B) || C
So if you want B || C to be grouped together, you need to explicitly add parenthesis, i.e.:
A && (B || C)
Parentheses decide order of operations, if you move the parentheses around you may change what the output is. In the same way that (A + B) / C is different from A + (B / C) but still a valid equation.
See Order of Operations in C
Logical AND has higher priority than OR:
https://en.cppreference.com/w/c/language/operator_precedence
dg

C code snippets explanation

I'm trying to convert following C code snippets to assembly, but the problem is I can't even understand these simple C code. They are written in abnormal way I think. so I just can't transfer to assembly. please help me
a = (a >= c);
b = (c < d) || (b > d);
a = (a != d) && (b != c)
Look if a>=c then it return true. So a=1 otherwise a=0.
Now in second case if c<d(c is less than d ) or b>d(b is greater than d) any one of them is true b=1. If both are false b=0.
If a is not equal to d and b is not equal to c then a=1 otherwise it is false(0).
A && B = 1 if A!=0 and B!=0
= 0 if A=0 or B=0 or (A=0 and B=0)
A || B = 1 if A!=0 or B!=0
= 0 if A=0 and B=0
Note: if (A && B) and A is found to be 0 then B will not be checked.
if (A || B) and A is found to be 1 then B will not be checked.
if B is an expression then that will not be executed as per the information stated above. This is called Short circuit evaluation.
Statements like a>=c return true ( 1 ) or false ( 0 ).
So in your case, a=(a>=c); will assign the value 1 to a if a >= c, otherwise, it will assign 0 to a.
This can also be read as
if( a >= c )
a = 1;
else
a = 0;
Next is b=(c<d)||(b>d);. Since we have ||, if any one of the two conditions return true then b is assigned 1, otherwise it is assigned 0. That is if ( c < d ) or ( b > d ), b will be assigned the value 1, and if both of the conditions return false, then b is assigned 0 .
Now this is similar to the if statement
if( ( c < d ) || ( b > d ) )
b = 1;
else
b = 0;
And for a=(a!=d)&&(b!=c), both the conditions have to return true for a to get the value 1. That is, both ( a != d ) and ( b != c ) must return true for a to be assigned the value 1. If anyone of those two conditions is not satisfied, then a is assigned the value 0.
This is equivalent to
if ( ( a != d ) && ( b != c ) )
a = 1;
else
a = 0;
Hope you understand it now. :)
They are boolean expressions and return a value of 1 when true.
(a >= c) returns 1 if a >= c, and 0 if not.
Also a && b will return 1 if both a and b are non-zero

comparison between 3 images

I wanna compare 3 images at 3 different buttons, Xcode is allowing me to compare 2 images at a time but when i write code for 3 it gives a warning
"comparison between pointer and integer"
-(void)compare
{
if (b1.currentImage==b2.currentImage==b3.currentImage)
{
b1.enabled=NO;
b2.enabled=NO;
b3.enabled=NO;
NSLog(#"%#",b1.currentImage);
NSLog(#"%#",b2.currentImage);
}
else
{
UIImage *btnImage = [UIImage imageNamed:#"card.png"];
[b1 setImage:btnImage forState:UIControlStateNormal];
}
}
Generally spoken when you write :
a == b == c
that actually boils down to
(a == b) == c
thus you are comparing the result of the comparision of a and b (which is of type bool regardless of the types of a and b) to c which is of whatever the type of c is.
So in your case you compare b1.currentImage==b2.currentImage (which is of type int) to b3.currentImage which is a pointer type.
If yo want to compare three values for equality you have to write:
(a == b) && (a == c) instead of a == b == c
Try this small program and watch what it prints out:
int main()
{
int a = 2, b = 2 , c = 2 ;
if ((a == b) && (a == c ))
{
printf ("(a == b) && (a == c ) is true") ;
}
if ((a == b == c))
{
printf ("(a == b == c) is true") ;
}
return 0 ;
}
it will print
(a == b) && (a == c ) is true
because (a == b == c) is evaluated like this
1. (a == b) == c
2. (2 == 2) == 2
3. 1 == 2
4. false

C while loop logic ( y != ( 1 || 0) )

I have the below code to choose sin or cos to be integrated,
while( x !=1 || y !=(1||0) ){
printf("Sin (1) or Cos (0)?\n");
x = scanf("%d",&y);
_flushall();
if(y==1){
printf("Sin set\n");
}
else if(y==0){
printf("Cos set\n");
}
}
However the
y!= (1||0)
never evaluates to true for y == 0 , can someone explain what's wrong here? Thanks.
You need (y != 1 && y != 0) (or similar, it depends on what you really mean to express there). The || operator is being applied to the operands 1 and 0. Put another way, y != (1 || 0) means "Do (1 || 0) then do y != result".
You are attempting to effectively code directly Boolean algebra, and C doesn't accept it in the manner you've provided.
while( x !=1 || y !=(1||0) )
should be
while( (x!=1) || ( (y!=1) || (y!=0) ) )
Never underestimate the value of using excess parentheses in C. The optimizer will likely optimize the code to be more efficient anyways.
The part of code that generates this error evaluates as follows:
LHS (left hand side), RHS (right hand side)
LHS = y
!= (1||0) [definition given]
!= (1) [b/c (1||0) = (1)]
y != (0||1)
is equivalent to
y != 1
since 0||1 is 1. You'll need two comparisons if you want y != 0 or y != 1.

Operator precedence and ternary operator

I have a problem in C.
#include<stdio.h>
int main()
{
int a = 10, b = 0, c = 7;
if (a ? b : c == 0)
printf("1");
else if (c = c || a && b)
printf("2");
return 0;
}
This code prints 2 but I think a?b:c returns b=0 and 0==0 returns 1. Can you explain the code?
The conditional operator (?:) has one of the lowest precedences. In particular it is lower than ==. Your statement means this:
if(a ? b : (c == 0)) { ... }
Not this:
if((a ? b : c) == 0) { ... }
Your conditions are not properly written.
In the first if-statement:
if (a ? b : c == 0)
if you put the values, then it becomes
if(10 ? 0 : 7 == 0)
means, it will always return 0.
That's why control goes to the else part and there, it becomes
else if (7 = 7 || 10 && 0)
since you used the "=" operator here (c = c), it will be always true, therefore it prints "2".
Now you want that code should return "1", then change your if statement in this way.
if( (a ? b:c) == 0){...}
because "==" operator has higher precedence than ternary operator.

Resources