C code snippets explanation - c

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

Related

Problem while defining conditions for multiple variables in C

I'm trying to write a program in C that receives three integers and uses This Method
But I only want the inputs to be in the range of 1-100 (including 1 and 100)
#include <stdio.h>
int main(){
int a , b , c = 50;
do{
scanf("%d %d %d", &a, &b, &c);
}
while((a > 100 || a < 1)&& (b > 100 || b < 1) && (c > 100 || c < 1));
if (a+b > c && a+c>b && b+c>a){
printf("%d\n", a+b+c);
}
else {
printf("invalid\n");
}
And it somehow doesn't consider the conditions
for example if I enter something like 1000 -5 4 It won't reprompt me for a new input
But the weird thing is that if i only consider one of the conditions it will work fine with that one variable! for example if i put something like while((a > 100 || a < 1); in there, it will then reject an input like 1000 2 4
How do I make it that it considers the conditions for all the variables?
Any help would be appreciated!
To check for any of the variable for range, change
while((a > 100 || a < 1)&& (b > 100 || b < 1) && (c > 100 || c < 1));
^^
// Will produce a FALSE value if 'a' is within expected range, and due to
// short-circuit, it'll not evaluate other conditions.
to
while((a > 100 || a < 1) || (b > 100 || b < 1) || (c > 100 || c < 1));
That said, couple of other points:
int a , b , c = 50; only initializes c, other variables are uninitialised and contain indeterminate values. Be a little more explicit, declare and define each variable in separate line (not a technical requirement, sake of readability and maintainability while being technically correct).
Always check for the success of scanf(), before using the scanned values. Better, do away with scanf() for user inputs and use fgets() instead.
more readable :
while(!( 1 <= a && a <= 100 && 1 <= b && b <= 100 && 1 <= c && c <= 100));
Your loop will only continue if all three of a, b, and c are out of range - in your example of 1000 -5 4, the condition (c > 100 || c < 1) evaluates to false (0), so the whole expression evaluates to false and the loop exits.
You need to change the sense of the test, such that it will continue if any of a, b, or c are out of range. There are a couple of ways to do this:
while ( (a < 1 || a > 100) || (b < 1 || b > 100) || (c < 1 || c > 100) );
Changing the && to || means that the expression will evaluate to true and the loop will continue if any one of a, b, or c are out of range.
Another option:
while ( !(1 <= a && a <= 100) && (1 <= b && b <= 100) && (1 <= c && c <= 100 ) );
This will cause the loop to exit if all three of a, b, and c are in range.
You should also check the result of scanf to make sure you read all 3 inputs:
do
{
if ( scanf ( "%d %d %d", &a, &b, &c ) != 3 ) )
{
if ( feof( stdin ) || ferror( stdin ) )
{
fputs( "Error or EOF on input, exiting", stderr );
return EXIT_FAILURE;
}
else
{
fputs( "Bad input detected, clearing input stream", stderr );
/**
* Read and discard everything up to the next newline character
*/
while ( getchar() != '\n' )
; // empty loop
/**
* Unconditionally branch back to the beginning of the loop since
* at least one of a, b, and c wasn't read at all (i.e., don't
* rely on the test since what you're testing isn't valid).
*/
continue;
}
}
} while ( (a < 1 || a > 100) || (b < 1 || b > 100) || (c < 1 || c > 100 ) );

Connect Four Check Winner

So to change my question. It refuses to recognize that there are four twos in a row. It recognizes that there are four ones in a row but that happens after the four twos. Why is this happening?
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 1 1 1 1 0 0
1 2 2 2 2 0 0
int checkFour(int a, int b, int c, int d){
if (a == b == c == d){
return 1;
}
else{
return 0;
}
return 0;
}
//check for the horizontal win
int checkHorizontal(){
for(int i=0; i < rows; i++){
for(int j=0; j < column - 3; j++){
if ((board[i][j] != 0) && (board[i][j+1] != 0) && (board[i][j+2]!= 0) && (board[i][j+3] != 0)){
if (checkFour(board[i][j],board[i][j+1],board[i][j+2],board[i][j+3]) == 1){
printf("Game Over\n");
exit(0);
}
}
}
}
}
What am I doing wrong?
if (a == b == c == d){ does not work the way you might think. The result of a comparison in C is a boolean value of 0 or 1. Given that == operator has left-to-right associativity, your statement can be re-written as:
if ((((a == b) == c) == d)
This appears to give correct results when they are all 1. This is because it ends up comparing the values (1) to the result of the comparison operation, also (1).
(((a == b) == c) == d) a == b -> 1
((1 == c) == d) 1 == c -> 1
(1 == d) 1 == d -> 1
The correct way is to use logical AND.
if (a == b && a == c && a == d)
All three comparisons need to evaluate to true for the entire statement to be true.
Note that there are other combinations that work. Ex:
if (a == b && b == c && c == d)
By the way, you can shorten the entire function to
int checkFour(int a, int b, int c, int d){
return a == b && b == c && c == d;
}
The problem is that you misunderstood the mechanism of C. the code if (a == b == c == d) wouldn't take if abcd are all values equal then return 1. because C computes from left to right(same priority), so it would compute a == b first, the result is 1 or 0, then take this result to compare with c, the second result also is 1 or 0, finally take second result to compare with d and the final result is come out.
The right code is like this:
if ((a == b) && (d == c) && (b == c))
return 1;
else
return 0;

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

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"

how does "==" operator work in an expression?

#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
In the above code,could any one please explain to me how d = b + c == a works?
Because of operator precedence, it is parsed as
d = ((b + c) == a);
b + c is 10, which is equal to a, so d receives the value of 1, which is how C represents true comparisons.
Based on precedence of operators, binary + has higher precedence than ==. So the statement will be grouped as,
d = ( b + c ) == a;
Which becomes,
d = ( ( b + c ) == a ); // ==> d = ( 10 == 10 );
So, d holds the truth value based on the comparison (b+c) == a which is 1 because in C comparison operators will return 1 for true and 0 for false.
Its works like this
d = (b+c) == a --> (5+5) == 10 ---> 1
Which returns 1
+ operator has higher precedence than ==.So d=b+c==a; parsed as d=((b+c)==a);. b+c is 10.
so (10==a) evaluates true .So d=1;

Boolean Abstraction C Program

I'm trying to compute the abstraction of the following C code fragment, with the predicate: b: { x >= 0 }
1. if( x > 5 )
2. x = x - 2;
3. else
4. x = abs( x ) + 6;
5. assert( x >= 0 );
so far I abstracted:
1. if( * ) // not sure if I should put if( b ) here
2. assume( b ); b = true;
3. else
4. assume( true ); // ? don't know how to abstract further
5. assert( b )
Any ideas how to do this ?
I dont know whether I am understanding you correct or not, but for the set of input predicate {x>=0} or b (used alternatively).It should be:-
{x>=0}=unknown() //unknown function is used to generate true or false non-deterministically
if(*)
{
assume({x>=0});
{x>=0}=true;
}
else
{
assume(!{x>=0});
{x>=0}=false;
}

Resources