in Programming in C retagged by
452 views
1 vote
1 vote

What will be the output of the following C program? Justify your answer. Negative numbers are represented in $2$'s complement,

#include<stdio.h>
int main() {
    if (-~-1) printf("COVID"); 
    if ((~7 & Ox000f ) == 8) printf (“19”);
    printf ("*");
    }
in Programming in C retagged by
by
452 views

1 comment

first if return $-0$, second if condition is true that is $8==8:1$, print $19*$ as output.
0
0

1 Answer

0 votes
0 votes

~ is bit wise complement.

{ Copied from stack overflow: When using a decimal constant without any suffixes the type of the decimal constant is the first that can be represented, in order (the current C standard, 6.4.4 Constants p5):

  • int
  • long int
  • long long int

Evaluating first if condition:

-~-1 = -(~(-1))

 -1 = 11...1 (32 times in 2’s complement).

~-1 = 00...0 = 0

-0 = 0 

Therefore first if condition is: if(0) printf(“COVID”);

Evaluating second if condition:

7 = 0..0111 and 0x000f = 0..01111

~7 =1...1000.

~7 & 0x000f = 0...01000 = 8

Therefore second if condition is: if( 8 == 8) printf(“19”);

Second if condition is true, hence, printf(“19”) gets executed and then printf(“*”) gets executed.

Output: 19*

Related questions