-2

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.

Mike
  • 4,041
  • 6
  • 20
  • 37
  • 2
    What did you expect? – Sourav Ghosh Jun 10 '20 at 06:51
  • If you don't understand that, you have to study what the `!` operator does, simple as that. And also study other utterly fundamental things like how an `if` statement works. This site isn't an interactive beginner tutorial replacing studies. – Lundin Jun 10 '20 at 06:51

1 Answers1

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261