I am trying to optimize some code and I was wondering if the return value of a condition like
(1>0)
is always 1 in c99 ? I couldn't find the answer on the web and the few tests I made with gcc seems to indicate this is true. But is this part of the langage specification ?
The exact code (marching squares algorithm) :
to_run->data[(y / 2) * (my_grid->width / 2) + (x / 2)] =
(up[0] > level) +
(up[1] > level) << 1 +
(down[0] > level) << 2 +
(down[1] > level) << 3;
Yes.
C99 §6.5.8 Relational operators
Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int
Yes.
The draft C99 spec says:
Each of the operators < (less than), > (greater than), <= (less than or equal to), and
>= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it
is false. The result has type int.
Related
This question already has answers here:
Chaining multiple greater than/less than operators
(6 answers)
if(x==y==z) works, if(x!=y!=z) does not
(4 answers)
Closed 1 year ago.
Anyone can enlighten me on why -5<-2<-1 returns 0 in C when I would expect it to return 1(True)?
printf("%d", -5<-2<-1);
This expression
-5<-2<-1
is equivalent to
( -5<-2 ) < -1
because the operator < evaluates left to right.
As -5 is less than -2 then the value of the sub-exoression
( -5 < -2 )
is integer value 1. So you have
1 < -1
and the result of this expression is 0 that is logical false.
From the C Standard (6.5.8 Relational operators)
6 Each of the operators < (less than), > (greater than), <= (less than
or equal to), and >= (greater than or equal to) shall yield 1 if the
specified relation is true and 0 if it is false. The result has type
int.
It seems you mean - 5 < -2 && -2 < -1
Please consider these piece of C code:
if ((value & 1) == 1)
{
}
Assuming value equals 1, will (value & 1) return 1 or any unspecified non zero number?
§6.5.8 Relational operators
Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.) The result has type int.
§6.5.9 Equality operators
The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.) Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.
§6.5.13 Logical AND operator
The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.
§6.5.14 Logical OR operator
The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.
Does “true” in C always mean 1?
No. Any expression that evaluates to a non-zero value is "true" in C. For example,
if (-1) {
}
if (5-10) {
}
if( "string literal") {
}
are all "true" and pass the condition.
In your specific example, the relational operator == yields 1 which is also "true"(the same holds for all other relational operators as noted by Govind).
If you are really asking about whether the bit-wise AND (&) yields 1 when value is 1, then yes, value & 1 yields 1 (assuming value is an integer type -- & operator requires its operands to be integers).
In fact, you can probably try to understand the individual parts and (generally how the & and == operators behave) by using a simple program:
#include <stdio.h>
int main(void)
{
int value = 1;
printf(" value & 1 = %d\n", value & 1);
printf(" 2 & 1 = %d\n", 2 & 1);
printf("((value & 1) == 1) = %d", (value & 1) == 1);
}
What does this warning mean (i and j are not constants):
I have been trying to Google this but it does not give me any results.
warning: comparison of constant 10 with boolean expression is
always true [-Wtautological-constant-out-of-range-compare]
if ((0<=i<=10)&&(0<=j<=10)){
In my program, i and j are not constant values and they do change.
In C, chaining of relational operators like this are not valid design. Thus,
(0<=i<=10)
is not doing what you think it should be doing. it is getting evaluated as
((0<=i) <= 10 )
which is basically either
0 < = 10, producing 1 (considered TRUE value)
1 < = 10, also producing 1 (considered TRUE value)
sadly, both of which are way out than the expected path.
Solution: you need to break down your condtion check like
(0 <= i) && ( i<=10)
The other answers have already explained the core problem. You can use:
if ( ( ( 0 <= i) && (i <= 10)) && ( ( 0 <= i) && (i <= 10)) )
to resolve your problem.
My recommendation will be to wrap that logic in a function.
int isInRange(int x, int lower, int upper)
{
return (lower <= x && x <= upper);
}
and use
if ( isInRange(i, 0, 10) && isInRange(j, 0, 10) )
I believe you need to understand what's going on in your statement at a deeper level.
0<=i is a boolean expression, it will become true or false.
The result of that expression is then compared with 10.
So you end up with true <= 10 or false <= 10.
I think you meant to write
if ( ( 0 <= i ) && ( i <= 10 ) )
You cannot connect clauses together the way you did.
It means that the expression can be evaluated at compile time, and that the evaluation will not be compiled because it's true (ie. non-zero) at compile time.
Usually this happened to me when I accidentally used && instead of &. Which is not the case here of course, but it gives the same error message.
Also note that gcc makes a distinction between logical and bitwise:
Logical means bool which is either 0 meaning false or non-zero meaning true and the operator that yields these is && with keyword and.
Bitwise means boolean logic and the operator is & with keyword bitand.
&& Always yields a bool, which is true unless it's 0. If operating on bools they're equivalent, but on anything else & and && test different things ie. "Is it nonzero" vs "return only the matching bits".
Assume we have an expression like
(x > 5)
in C language. Is there any guarantee given by the language / standard that the expression will be evaluated to 0 when it's false and to 1 when it's true?
Yes, it is guaranteed by the standard.
As per the C11 standard document, chapter 6.5.8, paragraph 6, [Relational operators]
Each of the operators < (less than), > (greater than), <= (less than or equal to), and >=
(greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is
false. The result has type int.
Update: Same chapter and paragraph for C99 standard also.
In gcc, it will be evaluated as one and zero.
Consider following program
#include <stdio.h>
int main(void)
{
int a = 3;
int b = 4;
if((a > b) == 0)
printf("a > b is false\n");
if((a < b) == 1)
printf("a < b is true\n");
return 0;
}
It gives output
a > b is false
a < b is true
Can I assume that in C, the "==" operator will always evaluate to 1 if the two values are equal or it can evaluate to other "true" values?
struct ss {
int id;
};
struct os {
int sid;
int state;
};
int count(struct ss *s, int state)
{
int num = 0;
// foreach o (of type os*) in a hash table
num += o->state == state && (s ? o->sid == s->id : 1);
return num;
}
So o->sid == s->id will return always 1 or 0, or it can return other values?
Can I assume that in C, the "==" operator will always evaluate to 1 if the two values are equal or it can evaluate to other "true" values?
Yes, and so does != > < >= <= all the relational operator.
C11(ISO/IEC 9899:201x) §6.5.8 Relational operators
Each of the operators < (less than), > (greater than), <= (less than or equal to), and >=
(greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is
false.107) The result has type int.
From the standard :
6.5.8 Relational operators
Each of the operators < (less than), > (greater than), <= (less than or equal to), and >=
(greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.
6.5.9 Equality operators
The == (equal to) and != (not equal to) operators are analogous to the relational
operators except for their lower precedence. Each of the operators yields 1 if the
specified relation is true and 0 if it is false. The result has type int. For any pair of
operands, exactly one of the relations is true.
For logical operands (&&, || ) :
6.5.13 Logical AND operator ( or 6.5.14 Logical OR operator )
The && (or ||) operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.
You can check the last draft here : http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
Conclusion :
All the equality and relational operator (==, !=, <, >, <=, >=) return 0 for false and 1 for true.
The logical operators (==, ||, !) treat 0 as false and other values as true for their operands. They also return 0 as false and 1 as true.
The comparison (equality and relational) operators (==, !=, <, >, <=, >=) all return 0 for false and 1 for true — and no other values.
The logical operators &&, || and ! are less fussy about their operands; they treat 0 as false and any non-zero value as true. However, they also return only 0 for false and 1 for true.
Can I assume that in C, the "==" operator will always evaluate to 1 if the two values are equal or it can evaluate to other "true" values?
Yes, for a standard compliant compiler, this assumption is correct:
Programming languages — C, §6.5.9 Equality operators (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):
Each of the operators yields 1 if the specified relation is true and 0
if it is false. The result has type int.