#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;
Related
This question already has answers here:
Short-circuit evaluation evaluating if( (a = 4) || (b = 6) || (c = 7) || (d = 8) )
(4 answers)
Closed 1 year ago.
Why is the output showing d=4 instead of d=8 in the first printf statement
#include <stdio.h>
int main() {
int a = 3, b = 4, c = 3, d = 4;
int y = (c = 5) || (d = 8);
printf("a=%d, b=%d, c=%d, d=%d\n", a, b, c, d);
}
|| short circuits, so in:
y = (c = 5) || (d = 8);
The d = 8 is never evaluated.
That is, since (c = 5) evaluates as true, there is no reason to evaluate the (d = 8) to determine the truthiness of the expression; so it is not evaluated.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a , b ,c ;
printf("Enter values for a and b: ");
scanf("%d%d",&a,&b);
a = a + b-- ;
if (a<b){
c = -1;
printf("\n\t%d %d %d\n\n",a,b,c);
}
else {
c = 0;
printf("\n\t%d %d %d\n\n",a,b,c);
}
}
Lets assume the value of the input for a and b are 2 (for both of them).
I studied the above program, but when it comes to the output it will be 4 1 0, a=4,b=1,c=0.
But, the calculation part above said that a=a+b-1 which will be the value of a is 3, now the new value of a is 3. But for b the value is still 2 because we didn't assign a new value to it.
I am very confused about the output.
There is a difference between a+1, a++ and ++a. Details here.
Therefore, when you say
a = a + b--;
You are actually saying
a = a + b;
b = b - 1;
If you say
a = a + --b;
It becomes
b = b - 1;
a = a + b;
And if you say
a = a + (b-1)
It does what you think: a = a + b - 1. The value of b doesn't change afterwards.
At the beginning, a and b are both 2
Then, you execute a = a + b--;.
The decrement operator is located after the b, so it evaluates to:
a=a+b;
b=b-1;
After this, a will be 4 and b will be 1.
a is not smaller than b, so c will be 0.
Note:
If it would be a = a + --b, it would evaluate to
b=b-1;
a=a+b;
Because the -- is executed at the beginning of the evaluation.
#include <stdio.h>
int main() {
float a,c,d;
int b;
printf("Enter the float number: "); scanf("%f", &a);
a * 100 == b ;
b % 100 == c ;
c + a == d ;
printf("%f", d);
}
It prints 0.00.
Why is it doing this?
These statements:
a * 100 == b ;
b % 100 == c ;
c + a == d ;
are a series of comparisons. == is the equality comparison operator, while = is the assignment operator. Also, % is the modulus operator, and can only be used for integral operands. Perhaps you meant something more like
b = (float) ((int) (a * 100) % 100); /* fraction */
c = (float) ((int) a % 100) / 100.0; /* mantissa */
d = b + c;
Note that this is not necessarily good style, but should work.
To start with == is a comparison operator. To assign a value, you need to use =.
Assignment syntax in C is written as:
b = a * 100;
Try reading some tutorials on assignment operations and you should see where you are going wrong.
Here's one to get you started.
Consider the following code:
#define P1(x) x+x
#define P2(x) 2*P1(x)
int main()
{
int a = P1(1) ? 1 : 0;
int b = P2(a)&a;
return 0;
}
Now, I thought that the compiler first replacing the macros with their values and so int b = 2*a+a&a; (and since a=1 then b=3). Why isn't it so?
This is because & has lower precedence than that of addition + operator. The grouping of operands will take place as
int b = ( (2*a + a) & a );
Therefore, (2*a + a) = 3 and 3 & 1 = 1 (011 & 001 = 001).
There's no precedence in your operation (it is just a textual substitution) thus, as you've noted,
#define P1(x) x+x
#define P2(x) 2*P1(x)
int a = P1(1) ? 1 : 0; // 1
and since & has lower precedence than +, it is equivalent to
int b = ((2 * a) + a) & a;
i.e. only the rightmost bit is set on b.
((2 * a) + a) 011 &
a 001 =
--------------------
b 001
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