Saturday, 18 February 2017

C Programming Questions with Explanation

Q:: What will the output of following?

           for(i=0;i<10;++i)
                   printf("%d",i&1);
(a)0101010101 (b)0111111111
(c)0000000000 (d)1111111111

Answer :: (A)
Explanation::

Binary representation of 1 is 0000000000000001 in 16 bit

In first iteration i=0

     i=0000000000000000
    1=0000000000000001
i&1=0000000000000000=(0)

In second iteration i=1

     i=0000000000000001
    1=0000000000000001
i&1=0000000000000001=(1)

In third iteration i=2

     i=0000000000000000
    1=0000000000000010
i&1=0000000000000000=(0)
In fourth iteration i=3

     i=0000000000000011
    1=0000000000000001
i&1=0000000000000001=(1)

So it will produce 0 when 'i' is even
and  '1' if 'i' is odd
Output will be 0101010101

No comments:

Post a Comment