0

In the program below, why is ~a printed in the output as 10? Why not -11?

#include <stdio.h>

int main()
{
    int a=10;
    ~a;
    printf("complement :  %d\n",a);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

5

Because you don't save the result of the complement operation anywhere.

If you do e.g.

a = ~a;

then you should get a different result.

Or you could simply print the result of the operation:

printf("complement :  %d\n", ~a);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621