I read some code and came over this rather cryptic syntax:
size_t count = 1;
char *s = "hello you";
char *last_word = "there";
count += last_word < (s + strlen(s) - 1); #line of interest
Count is incremented, somehow. But I thought the < operator would return true or false.
What does this line do?
As per the operator precedance table, < binds higher than += operator, so your code is essentially
count += ( last_word < (s + strlen(s) - 1)) ;
where, the (A < B) evaluates to either 0 or 1 Note, so, finally, it reduces to
count += 0;
or
count += 1;
Note: related to the "1 or 0" part, quoting C11, chapter §6.5.8/p6, 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.
In C, relational operators always yield 0 or 1. So, this statement
count += last_word < (s + strlen(s) - 1);
adds either 0 or 1 to count depending on the comparison's result. It can be written as (and equivalent to):
if (last_word < (s + strlen(s) - 1)) {
count = count + 1;
} else {
count = count + 0;
}
(The else part is needless; added for explanatory purpose.)
C11 (draft N1548.pdf), Relational operators, §6.5.8, 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. 107) The result has
type int.
In C there is a the stdbool.h header which defines as true and false. Essentially you can think of the basic implemantation as:
#define bool int
#define true 1
#define false 0
true and false are defined as not zero and equal to zero respectively. So basically when last_word < (s + strlen(s) - 1), count is incremented by one.
Related
output is 0 when entered 1001 or something greater than it.
it should give 0 when the number isn't abundant and should exit if entered number is greater than the limit ,have tried using goto exit.
#include <stdio.h>
void main()
{
int n;
printf("Enter the number\n");
scanf("%d", &n);
int i, sum = 0;
if (1 <= n <= 1000)
{
for (i = 2; i < n; i++)
{
if (n % i == 0)
sum = sum + i;
}
(sum > n) ? printf("1") : printf("0");
}
else
return;
}
The condition in this if statement
if (1 <= n <= 1000)
is equivalent to
if ( ( 1 <= n ) <= 1000)
the result of the sub-expression 1 <= n is either 0 or 1. So this value is in any case less than 1000.
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.
You need to write
if (1 <= n && n <= 1000)
using the logical AND operator.
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
Why is the value of j zero after below code is executed?
int main()
{
int i = 0, j = 0;
if (i && (j = i + 10))
//do something ;
}
Why is the value 10 if I replace the logical and with bitwise and?
Also, why is the value of j again zero after execution of below code?
int i = 10, j = 0;
if (i || (j = i + 10))
//do something
;
Because logical and (&&) short-circuits. Consider:
a() && b() && c();
if a() returns false, by definition of the and operation, the whole result will be false, so there's no need to compute b() and c() (and in fact, C will not execute b() and c()). In your case, && gives up after noticing that i == 0 (which is false in C).
Similarly, logical or (||) short-circuits on first expression that evaluates to true. Consider:
a() || b() || c()
if a() still returns false (as per previous example), but b() returns true, then c() will not be evaluated as the value of the whole logical operation is already known after evaluating b().
Bitwise and (&) is something entirely different from logical and. You use logical and for control flow (deciding what code to run); you use bitwise and to calculate and operation on bits. Bitwise operations are not like logical operations, they're more like arithmetical operations - they behave similarly to e.g. + or *.
It would be easier to think on the && short-circuit as the following:
int main()
{
int i = 0, j = 0;
if (i){
if(j = i + 10)){
//do something ;
}
}
}
This is the equivalent to your example. If you replace the order (j = i + 10) && i as you mentioned the equivalence will be as the following:
int main()
{
int i = 0, j = 0;
if (j = i + 10){
if(i)){
//do something ;
}
}
}
You can see for yourself why the value of j will be zero in the first block of code and ten in the second.
Because i && (j = i + 10) represents a logical AND operation that short-circuiting behavior. That (j=i + 10) is not evaluated because i is logical 0 (false). Each expression must evaluate to a scalar logical result.
j = i + 10 is different from j == i + 10. The former assigns a value to j (in this case 10 since i is 0) and the latter compares j with i + 10.
When you have && the j = i + 10 is ignored because it already knows that i is 0 so the first part is false. When you make it a & it looks at both sides of the & operator and assigns a value to j. Which may or may not be what you're trying to do - it's not clear what you are trying to do with the code.
I prefer to look at it like this:
if (A && B)
{
DoSomething();
}
is the same as
if (A)
{
if (B) // So B is only evaluated when A results in TRUE
{
DoSomething();
}
}
And
if (A || B)
{
DoSomething();
}
is the same as
if (A)
{
DoSomething();
}
else if (B) // So B is only evaluated when A results in FALSE
{
DoSomething();
}
This question already has answers here:
Chaining multiple greater than/less than operators
(6 answers)
Closed 6 years ago.
im having trouble using multiple operators while programing a simple FizzBuz in C
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
if (0 < n < 10000){
for (int i = 1; i <= n; i ++) {
if (i % 15 == 0) {
puts("TikTak");
}
else if (i % 3 == 0) {
puts("Tik");
}
else if (i % 5 == 0) {
puts("Tak");
}
else {
printf("%d\n", i);
}
}
}else{
printf("-1");
}
}
Now the "if (0 < n < 10000)" comparison operators is ignored for some reason, but if I rewrite it as:
if (n < 10000 && n > 0){
it will work as intended.
Am I missing something? Ill be honest Im newbie in C programing. haha
EDIT: Thanks everybody, haha that was quite simple. I thought that might be the issue I just waned to make surebecause "0 < n < 10000" is litteraly how the assigment says it should look like.
Again, thanks!
Rewrite this condition
if (0 < n < 10000){
like
if (0 < n && n < 10000){
Otherwise the original condition looks like
if (( 0 < n ) < 10000){
and the result of the expression 0 < n is either 1 or 0. So in fact you are comparing 0 or 1 with 10000.
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.107) The result has
type int.
The expression 0 < n < 10000 is equivalent to (0 < n) < 10000 which means you check if 0 < n is less than 10000, which it will always be (the result of a comparison like 0 < n will be zero or one).
I have this part of an if statement and I'm getting a weird output.
int x = 10;
if(1 < x < 5){
printf("F\n");
}
Why does it print "F"? Logically isn't the if statement false because x is greater than 1 but not less than 5?
In C, you can't chain comparisons like that. The expression 1 < x < 5 is evaluated as (1 < x) < 5: so for x = 10, the expression is (1 < 10) < 5. (1 < 10) is true, which C represents as the value 1, so the expression reduces to 1 < 5. This is always true, and your printf() if executed.
As level-999999 says, in C you need to explicitly combine single comparisons with && and ||.
If you are using C, you should have broken down the condition into two arguments :
if ( x > 1 && x < 5) {
printf("F\n");
}
Let me be judged as a Noob in programming, I have been learning obfuscated way of programming in c/c++, as the c compiler compiles a statement from the right hand side towards the left hand side.
I have the following code:
int main(){
int x=5, y=20, z=1;
int k = x > y < z;
printf("%d", k);
return 0;
}
The output returned is 1, Does this means that
x > y < z = (x>y) < z
or
x > y < z = x > (y<z)
I would love if someone would give me link to work on these skills.
Thanks and Regards.
Change z=-1 or x=0 and find out. Also change int main() to the more correct int main ( void )
Changing z = -1 will ouput 0, whereas k will be 1 if you assign it x > y == z if z = 0. So in short:
k = x > y < z;
is the same as writing
k = (x > y) < z;
left-to-right.
According to the C grammar (6.5.8 Relational operators)
1 relational-expression:
shift-expression
relational-expression < shift-expression
relational-expression > shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression
1 The relational operators group left-to-right (C++ Standard :) )
And
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.107) The result has
type int.
Thus the initializer in this declaration
int k =x>y<z;
is equivalent to
int k = ( x > y ) < z;
As x is less than y then expression x > y yields 0 and as z is greater than 0 then the full expression yields 1.
The following operators have right-to-left-grouping:
unary operators
conditional operator
assignmnet and compound assignment operators
First you have to check operator precedence, which is easiest by looking at an operator precedence table. If such a table is decent, it will list the operators > and < in a group called relational operators. All operators in this group have the same operator precedence.
Since the operators > and < have the same precedence, the order in which the operands will get processed is determined by the associativity of that group of operators. For the relational operators, this is left-to-right. Therefore the expression is guaranteed to be processed as (x > y) < z
In C the boolean values are numerical values, so:
false -> 0
true -> != 0, hence any number that is not 0 will represent the boolean true.
So in case of your code:
int k = x > y < z;
can be split in the following way:
int k = (x > y) < z;
that will be:
int k = ( 5 > 20) < 1;
the first part evaluates to false (0 that is) then the equality will loook this way:
int k = 0 < 1;
which is true, hence
int k = 1;
k = ( 5 > 20 ) < 1= False (0)
k = (0 < 1) = True (1)
printf (%d, k) = 1
Maybe you should know the Operator precedence and associativity. You can google or see the book <<c++ primer>>. First the operator > and < have a same precedence. Then we should use the associativity, there the operator only have left associativity. So the answer is x> y< z= (x>y)<z). I hope this can help you. Remember: 1. precedence; 2. associativity.