edited by
1,137 views
2 votes
2 votes

What is the output of folowing C codes . Justify
 

  •  for(i=0;i<10;i++)
    	     printf("%d",i>>1);
  • for(i=0;i<10;i++)
    	     printf("%d",i&1);
  • for(i=0;i<10;i++)
    	     printf("%d",i&&1);
  • for(i=0;i>0;i--);
    print("%d",i)
  • int i = 5;
      i = (i, ++i, 1) + 1;
      printf("%d\n", i);

OUTPUT

  • 0011223344
  • 0101010101
  • 0111111111
  • i was trying to make a wrap around question . failed . output 0
  • 2
edited by

2 Answers

Best answer
3 votes
3 votes

1.
for (i=0;i<10;i++) printf ("%d",i>>1);
i = 0, Shift i value one bit right. so it will print 0.
i = 1, Shift i value one bit right. so it will print 0.
i = 2, Shift i value one bit right. so it will print 1.
i = 3, Shift i value one bit right. so it will print 1.
Do same for all i values..
 

2. for(i=0;i<10;i++) printf ("%d",i&1);
Bitwise AND : C code used to check number is odd or not .. for each Odd number it will print 1 and for each even it will print 0.


3. for(i=0;i<10;i++) printf("%d",i&&1);
Used to Check i is NON zero or not. for Non zero i value it will print 1 and for i=0 it will print ).


4. for(i=0;i>0;i--); print ("%d",i)
for i = 0, i>0 condition failed so it will not execute print statement .. for all value after --i same condition will return false. even after i value become large (i Range exceeded) again i value reset to 0 and it will take forever i.e. Stackoverflow.



5. int i = 5; i = (i, ++i, 1) + 1; printf("%d\n", i);
here  i = (i, ++i, 1) value of i initialised to 1.
so finally i = (i, ++i, 1) + 1 will print 2. 
(Still i am in doubt it may gives UNDEFINED BEHAVIOR bcoz i value modified more than once in i = (i, ++i, 1) + 1)

selected by
1 votes
1 votes

0011223344

here shift operator shifts the data by k bits (here 1 bit)

in binary shifting by 2 means divison by 2.

so ,for i=0;i/2=0

for i=1,i/2=0

for i=2,i/2=1

for i=3,i/2=1

for i=4,i/2=2    i=00000100 ->  i>>1  -> 00000010    i.e. 2 

i=10               i=00001010      i>>1      00000101   i.er 5.

for 2nd ,

i &1 ANDS   00000000  AND 00000001  so answer is 00000000

here there is bitwise AND.

so for any i whose LSB is 1 will be anded as XXXXXXX1 AND 00000001

 and give o/p 1.

for 

printf("%d",i&&1);

you get 011111111 and output where you check if first operand of AND is zero or not.so this is not a bitwise AND.

always remeber bitwise shift never ever modifies your orignal data

edited by

Related questions

0 votes
0 votes
0 answers
1
pC asked Sep 7, 2018
534 views
#include <stdio.h>int main(void) { int i=10; printf(" %d %d %d",i==10,i=40,i>15 );} Conceptsundefined behaviorfloating point representation
1 votes
1 votes
3 answers
3
just_bhavana asked Jun 23, 2017
1,232 views
#include <stdio.h void f(int); int main() { int a=4; f(a); return 0; } void f(int n) { if(n>0) { f( n); printf("%d", n); f( n); } }Explain how function calls take place
2 votes
2 votes
1 answer
4
Akriti sood asked Jan 23, 2017
263 views
will there be no compile time error as we are initialising array greater than its size??