Why Not operator for negative number gives this output [duplicate] - c

This question already has answers here:
C not operator applied to int? [duplicate]
(3 answers)
Closed 2 years ago.
Can somebody please explain output of below code-
int a =-3;
printf("%d",!a);
The output is 0.
I cannot understand why i get output as 0.

Quoting C11, chapter 6.5.3.3
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).
In your case, it can be seen as
printf("%d", (a == 0)); // where a is -3
which evaluates to a falsy result, thereby printing 0 as the result.

Related

How does this trick work? bool && printf() [duplicate]

This question already has answers here:
What is short-circuit evaluation in C?
(3 answers)
Closed last year.
How does this work?
int x = 0;
x && printf("Hello World!");
This will output nothing on terminal, but
int x = 1;
x && printf("Hello World!");
will output "Hello Wolrd!"
It is called short circuit evaluation of a logical expression. Logical expressions are evaluated until the result is undetermined.
In this case:
if x==0 logical AND will be false despite the other side of the
expression. printf will not be called.
if x==1 the result of AND is still undetermined. printf will be called.
The result of this AND operation is not used and compiler will warn you.
The C standard specifies the behavior of && in clause 6.5.13. Paragraph 4 says:
Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares equal to 0, the second operand is not evaluated.

how do we interpret the `||` and `&&` in an assignment statement? [duplicate]

This question already has answers here:
Evaluation of C expression
(8 answers)
Closed 7 years ago.
I have been coding from a long time though I'm still a student programmer/ I'm usually good at programming but when questions like the one below are asked I get stuck. What will be the output and why of the following program?
int main()
{
int i=4,j=-1,k=0,w,x,y,z;
w=i||j||k;
print("%d",w);
return 0;
}
output:
1
why this result? what does the statement w=||j||k; means?
i || j || k is evaluated from left to right. It does that:
i == 4, which is true, so ORing it with any other value will yield true. That's it1.
The rest of the statement is not evaluated because || and && are short-circuit operators, that is, if in your statement i != 0, neither j nor k will be evaluated because the result is guaranteed to be 1. && works similarly.
That's important to remember if you have something like f() || k(), where k has some side effect like an output to screen or a variable assignment; it might not be executed at all.
The bitwise OR operator | really ORs the bitwise representations of the values instead; it evaluates all its operands.
1 Thanks to #SouravGosh on that!
In your code,
w=i||j||k;
is equivalent to
w= ((i||j) || k);
That means, first the (i||j) will be evaluated, and based on the result (if 0), the later part will be evaluated.
So, in your case, i being 4, (i||j) evaluates to 1 and based on the logical OR operator semantics, the later part is not evaluated and the whole expression yields 1 which is finally assigned to w.
Related quotes, from C11 standard, chapter §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.
then, regarding the evaluation of arguments,
[...] If the first operand compares unequal to 0, the second operand is
not evaluated.
and regarding the grouping,
[...] the || operator guarantees left-to-right evaluation;
The result of boolean operators yields an int value of 0 or 1.
See 6.5.13 and 6.5.14, paragraph 3.
The [...] operator shall yield 1 [or] 0. The result has type int.

How does the C compiler interpret the following code sequence [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
I have this C code line:
int a;
a = (1, 2, 3);
printf("%d", a);
Why the value 3 is printed? (the last one).
The comma operator evaluates all its "members" but returns the value of the last expression.From the C11 standard:
The left operand of a comma operator is evaluated as a void
expression; there is a sequence point between its evaluation and that
of the right operand. Then the right operand is evaluated; the result
has its type and value.

Why is the output of following C code 10? What I mean is why is printf giving x as output? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
C comma operator
(4 answers)
Closed 8 years ago.
Compiled using GCC on a Fedora 20 Desktop, the following code outputs 10.
int x=10;
int y=5;
printf("%d",(y,x));
In C, (y,x) means evaluating x and y and returning only x. EG. From wikipedia:
the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).
That´s the "comma operator" (not to be confused
with the comma when separating function parameters.)
The "result" is only the last part, but if the other parts are functions etc.,
they will be executed too.
In C (exp1,exp2)
First exp1 is evaluated, then exp2 is evaluated, and the value of exp2 is returned for the whole expression.
(exp1, exp2) is like (exp1 && exp2) but both exp1 and exp2 will
always be evaluated, whatever exp1 returns.
(exp1, exp2) is like { exp1; exp2; } but it can be used as an
expression in a function call or assignment.

Why the expression a==--a true in if statement? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
#include <stdio.h>
int main()
{
int a = 10;
if (a == a--)
printf("TRUE 1\t");
a = 10;
if (a == --a)
printf("TRUE 2\t");
}
Why is the second if statement true?
Output is:
TRUE 1 TRUE 2
Is this happening due to Undefined behavior because I am comparing same variable with its decremented value?
Correct, the condition evaluates to true because you see undefined behavior: if a variable with an operator that has side effects is used in an expression, it is illegal to use the same variable again in an expression that has no sequence points (== does not have sequence points).
This is because the compiler is free to apply the side effect of -- at any time that it wishes, as long as the value that is used when evaluating the expression is correct (i.e. the value before the decrement for postfix notation, or the value after the decrement for the prefix notation).
a-- decrements a after the expression has evaluated, effectively meaning when a == a is calculated, it's 10 == 10, which is true.
--a decrements a before the expression has evaluated, effectively meaning when a == a is actually calculated, a has already been decremented, so it's 9 == 9, which is again true.
Pre and post decrements can't happen in the middle of expressions. They happen before or after them.
This is because associativity of == operator is left to right.
a==a--
In this expression, first both sides will be compared (10==10), and then a is decreased to 9
a==--a
in this expression first the value of a is decreased to 9 then compared to a (which has become 9)
So, both are true.

Resources