This question already has answers here:
Why do logical operators in C not evaluate the entire expression when it's not necessary to?
(5 answers)
Closed 4 years ago.
In the following code why does the value of j remains zero even after the statement j=i+10.
#include <stdio.h>
int main()
{
int i = 10, j = 0;
if (i || (j=i+10))
printf("%d",j);// j=0
}
Why does value of j not change? Because i is non-zero and logical OR (||) properties is that if 1st operand is true don't check 2nd operand.
if ( i || (j=i+10))
| |
10(True) not evaluated
Related
This question already has answers here:
How "a<b<c" or "a>b>c" are evaluated in C
(2 answers)
Chaining multiple greater than/less than operators
(6 answers)
Compound condition in C: if (0.0 < a < 1.0)
(4 answers)
Closed 1 year ago.
The following code supposed to print true. But it is printing false. Does anyone know why is it so?
int main(void)
{
int a=15,b=10, c=1;
if(a>b>c)
{
printf("true");
} else
{
printf("false");
}
}
In C, a>b>c means (a>b)>c. It does not mean (a>b)&&(b>c).
The value of a>b is either 0 or 1 (false or true, respectively). Since c is 1, neither of those possible values can be greater than c, so the comparison is always false.
This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 5 years ago.
#include"studio.h"
void main() {
int i , j= 0, k;
k = i = 2 , j; // what's meaning of int , int ?
printf ( "%d", k );
}
can anyone please explain what's up with that int , int ..?
The comma operator allows you to evaluate a second expression. (a, b) returns the value of expression b.
However, because the = has a higher priority than the comma operator, the values of i, j and k are respectively 2, 0 and 2.
This question already has answers here:
Precedence of && over || [duplicate]
(4 answers)
Closed 9 years ago.
I was going through a C objective book where a question comes:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
i=j=k=1;
k=++i||++j&&++k;
printf("%d %d %d",i,j,k);
return 0;
}
The output is:
2 1 1
In my view:
k is incremented.
j is incremented.
i is incremented.
k&&j will happen.
i|| (k&&j)
So the output should be i=2,j=2,k=1. What am I missing?
The expression k=++i||++j&&++k; causes undefined behaviour. You are trying to assign to k twice without an intervening sequence point.
Even if the assignment were to a different variable, your steps would be inaccurate - the logical operators have short-circuiting behaviour.
Edit: OP says he changed the expression to a=++i||++j&&++k. I'm going to rewrite it fully parenthesized and with some spaces:
a = ++i || (++j && ++k);
In this case, only the ++i is evaluated, due to short-circuiting behaviour of the || operator.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
Code in C language.
#include<stdio.h>
#define PRODUCT(x) ( x * x * x)
int main()
{
int i =5,k;
k = PRODUCT( ++i );
printf("i is :%d ",k);
return 0;
}
My Question is why i is : 392? According to me output should be 336. (because 6 * 7 * 8 = 336)
Am I really messed up here??
Preprocessed code will have
( ++i * ++i * ++i)
which have Lack of sequence point between the two execution on same variable resulting Undefined behaviour.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(C) What is the difference between ++i and i++
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“%-4d”,a[i][j]);
}
printf(“\n”);
}
In the above case, you won't recognize it.
But it's essentially the following:
int x = a[i++] first reads from à afterwards, increases i
int x = a[++i] first increases i, afterwards reads from a
++i Increments $i by one, then returns $i.
i++ Returns $i, then increments $i by one.
i++ is a post-increment operator ie., the current value of i is used for the operation and the value is incremented by 1 after the operation.
++i is pr-increment operator ie., the value of i is incremented and the new value of is used in the operation.
int i = 2;
int a = ++i; // a is 3, i is 3
int b = i++; // b is 3, i is 4
++i is a pre-increment, while i++ is a post-increment.
In that situation, because the type is an int and it happens in a for-loop, nothing; there is no performance benefit to either.