This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
double negation in C : is it guaranteed to return 0/1?
int main(void)
{
int i = 2, j = 1;
printf("%d", !!i +!j);
return 0;
}
From what I understand, the !! turns the expression into a bool, so is it saying since i not equal to 2 the value is 0 + j which is not equal to 1 the value is 0, and since 0 is equal to false it reads: false + false = true which represents the value of 1. Please help I am new to C programming.
C doesn't have a boolean type (well, C99 and newer do, but there's nothing in your program that uses it).
! is just a unary operator that turns 0 into 1 and anything else into 0. So in your case, since i is 2, !i is 0, and !!i is 1. j is 1, so !j is 0. That leaves !!i + !j to be be 1 + 0, and you're printing 1. Try out this example program to see it in action:
#include <stdio.h>
int main(void)
{
int i = 2, j = 1;
printf("i = %d, j = %d\n", i, j);
printf("!i = %d, !!i = %d\n", !i, !!i);
printf("!j = %d\n", !j);
printf("!!i + !j = %d + %d = %d\n", !!i, !j, !!i + !j);
return 0;
}
!x is 0 if x is true (i.e. not equal to 0) or 1 if x is false (i.e. equal to 0). Since your example i is 2, !i will be 0 and thus !!i will be 1. Likewise !j will be 0. So the result of the expression will be 1 + 0 = 1.
Note that there are no circumstances under which 0 + 0 (i.e. false + false) would equal 1.
Here's what the specification states about the ! operator (C99 §6.5.3.3/5). The terse last sentence is all that is really required to understand its behavior:
The result of the logical negation operator !is 0 if the value of its operand compares
unequal to 0, 1 if the value of its operand compares equal to 0.
The result has type int.
The expression !E is equivalent to (0==E).
We can use the transformation from the third sentence to evaluate your expression, !!i + !j. The transformation becomes:
(0 == (0 == i)) + (0 == j)
and we can evaluate it as follows:
(0 == (0 == 2)) + (0 == 1) // substitute the variable values
(0 == (0 )) + (0 == 1) // 0 == 2 is false, so it becomes 0
(0 == (0 )) + (0 ) // 0 == 1 is false, so it becomes 0
(1 ) + (0) // 0 == 0 is true, so it becomes 1
1 // 1 + 0 is 1
Related
I understand the basic concept of short-circuiting with operators, but why does
int i = 0, j = -1, k = 1, m;
m = !(i++ && ++j) || ++k;
printf("%d %d %d %d", i, j, k, m);
have 1 -1 1 1 as an output? Specifically, why is j == -1 instead of 0?
I know similar questions have been already asked, but I don't understand this specific example which I didn't find anywhere.
i = -1;
i++; // value of expression is -1
// side effect is changing i to 0
if (i++) ; // value of `i++` is zero; the if will not "trigger"
i = 0;
if (i++ && foo) ; // i++ has value of zero (false)
// so false && <anything> is false
// so foo is not evaluated
int i = 0, j = -1, k = 1, m;
!(i++ && ++j) || ++k; ==> only i++ will be evaluated, j and k will not be evaluated
lets just say we are substituting the values of variables, then the expression becomes as below.
!(0 && ++ -1) || ++1
step 1:
!(0 && ++ -1) ==> for && operator if left side operand is False then we dont need to check for right side operand, so -1 wont be incremented, so value of j will be -1 itself.
and hence the left side expression before || becomes !(0)
step 2:
!(0) || ++1
now !(0) will be 1 , so for || operator if left side operand is TRUE , then no need to go for right side operand, then ++k will not be executed.
m = 1 || ++1 ==> 1
since only i++ is evaluated , it will change its value to 1
so the out put is : 1 -1 1 1
The value of the postfix increment operator is the value of its operand before incrementing.
So the value of the expression i++ is equal to 0 because the variable i was initialized by 0.
So as the value of the sub-expression i++ is 0 then the sub-expression ++j in this expression
(i++ && ++j)
is not evaluated and the value of the expression itself is 0.
Applying the negation operator
!(i++ && ++j)
you will get 1 (logical true). So the sub-expression ++k of the expression
!(i++ && ++j) || ++k
will not be evaluated.
As a result the value of the whole expression is equal to 1 that is assigned to the variable m.
m = !(i++ && ++j) || ++k;
On the other hand as it was pointed out in the beginning the expression i++ was evaluated. So after this statement i will be equal to 1.
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.
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");
}
This question already has answers here:
What is the difference between prefix and postfix operators?
(13 answers)
Closed 7 years ago.
I don't understand how the results of this is :
2
2
2
Here is my code :
#include <stdio.h>
int main()
{
int a = 1, b = 1, x = 0, y = 0;
double w;
x = 1 + a++;
printf("x = %d\n", x);
printf("a = %d\n", a);
y = ++b;
printf("y = %d\n", y);
printf("b = %d\n", b);
}
Ok i understood the postfix and prefix but i still don't understand why a and b are 2 and not 1 .
They are not being saved anywhere
so when you say x=1+a++ and y=++b,
b becomes 2 and is saved in y . How does b keeps being 2 when not saved anywhere such as b=++b .
Sorry i am not sure if you guys follow what i am thinking.
You have to understand how the increment operator works.
You have two operations :
X++ => Return the value of X, then increment it by 1.
++X => Increment X by 1 then return it.
In your situation, the line with a problem is here : x = 1 + a++;
This translates into :
Return the value of a (1) and increment it (a becomes 2).
Set the value of x equal to the 1 + the value returned by a (1) (x becomes 2)
Hope this helps.
C Language Pre-increment and Post-increment Operators
#include <stdio.h>
int main()
{
int a = 1, b = 1, x = 0, y = 0;
double w;
x = 1 + a++;
printf("x = %d\n", x);
printf("a = %d\n", a);
y = ++b;
printf("y = %d\n", y);
printf("b = %d\n", b);
}
Pre-increment means increment variable before its value is used. Post-increment means increment variable after its value has been used. To understand how these operators work, let's look at the first case:
a = 1;
x = 1 + a++; // ++ on right means post-increment
First you're setting the value of a to 1. Then you're saying add a (value is 1) to x (value is 1). The result is x has a value of 2. And then, after the statement is done executing, as a side-effect, a is incremented, because you used the post-increment form of ++. So after x has been set to a value of 2, the value of a will become 2.
Instead, if you used a a pre-increment operator:
a = 1;
x = 1 + (++a); // ++ on left means pre-increment
Again, you start with a = 1, but this time, a is incremented before its value is used in the statement, because ++ on the left-side means pre-increment. In other words, first, a is incremented to the value of 2. Then a is added to x (whose value is 1), setting the value of x to 3.
In both the above cases, a starts out with a value of 1 and becomes 2. The difference is whether that happens before or after the value of a is used in the expression.
Can anyone explain me why this line of code is wrong?
int n = 0, y = 1;
y == 1 ? n = 0 : n = 1;
The error is "Lvalue required as left operand of assignment " for "n=1"
The statement
(y == 1 ? n = 0 : n) = 1;
is interpreted as because n binds with ?: operator due to its higher precedence.
= needs l-value as its left operand while ?: returns an r-value.
Try this instead
y == 1 ? n = 0 : (n = 1);
or
n = y == 1 ? 0 : 1;
You should use :
n = y==1 ? 0 :1
According to the C standard, the behaviour is undefined if an attempt is made to use the result of the conditional operator as an Lvalue.
You can use ternary operator for assigning for example:
int n = 0, y = 1;
n = y == 1 ? 0 : 1;