Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

#include <stdio.h>

int main()
{
    char c=48;
    int i, mask=01;
    for(i=1; i<=5; i++)
    {
        printf("%c", c|mask);
        mask = mask<<1;
    }
    return 0;
}

 

  • A.12400
  • B.12480
  • C.12500
  • D.12556

Answer: B

Correct answer: 12480

Trace the loop. c is 48, which is \( 110000_2 \), the character '0'. Each pass ORs it with the mask and prints the result as a character, then shifts the mask left.

Masks 1, 2, 4 and 8 set bits that 48 does not have, giving 49, 50, 52 and 56 - the characters 1, 2, 4 and 8.

The fifth mask is 16, and 48 is \( 32 + 16 \), so bit 4 is already set: OR changes nothing and 48 prints as 0. The output is 12480, and the final zero is the whole point - OR is idempotent.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.