retagged by
468 views
1 votes
1 votes

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 ("*");
    }
retagged by

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

1 votes
1 votes
3 answers
2
admin asked Aug 18, 2022
556 views
What does the following function compute for $x \neq 0?$float isi1(float x, int y) { if (y==0) { return 1; } else if (y>0) { return isi1(x,-y); } else { return isi1(x, y+...
1 votes
1 votes
1 answer
3
admin asked Aug 8, 2022
437 views
Simplify the following Boolean function in product-of-sums form: $$ F(A, B, C, D)=\sum(0,1,2,5,8,9,10) . $$
0 votes
0 votes
0 answers
4
admin asked Aug 25, 2022
316 views
Consider the following state diagram of a sequential circuit, where each of a, b, c, d, e, f and g represents a state. Represent thestate diagram with minimum number of s...