edited by
7,883 views
8 votes
8 votes

What is the output of the following C program?

#include<stdio.h>    
void main(void){
    int shifty;
    shifty=0570;
    shifty=shifty>>4;
    shifty=shifty<<6;
    printf("The value  of shifty is %o \n",shifty);  
}
  1. The value of shifty is 15c0
  2. The value of shifty is 4300
  3. The value of shifty is 5700
  4. The value of shifty is 2700
edited by

7 Answers

Best answer
13 votes
13 votes
Ans is D) 2700

If an integer constant starts with 0, it is considered an octal constant. So 0570 = 101 111 000 in binary as each octal digit can be directly converted to 3 bits. Now, >>4 (right shift 4) gives 010 111 and <<6 (left shift 6) gives 010 111 000 000 = 2700 in octal. If we use "%d" in printf, this would be 1024+64*7 = 1472.
selected by
3 votes
3 votes

shifty = 0570

 

0 starting means it will be considered as Octal no (Some C standard)

 

So (0570)₈ = (000 101 111 000) ₂

 

>>4  (4 times right shift) $\Rightarrow$ 000 000 010 111 

<< 6 (6 times left shift) $\Rightarrow$ 010 111 000 000 

 

Now as "%o" there in printf, so it will be print as OCTAL NO

 

So (010 111 000 000)₂ = (2700)₈

 

Option : D 

 

Note:

If %d there in place of %o, it would be 1472 (decimal form) 

[ Since 2700 = (2x8^3) + (7x8^2) + (0x8^1) + (0x8^0) = 1472 ] 

edited by
1 votes
1 votes

It is in octal(0570)8 Since it starts with octal, so take 3 bits in binary format. 

(0570)8=000 101 111 000

shifty>>4(left shift the bits 4 times)=100 000 010 111

shifty<<6(right shift the bits 6 times)=010 111 000 000=(2700)8

So answer is D

1 votes
1 votes

For anyone having difficulty visualizing the steps.

Answer: (D) 2700

Answer:

Related questions

6 votes
6 votes
3 answers
1
Sourabh Kumar asked Jun 22, 2016
5,554 views
How many lines of output does the following C code produce?#include<stdio.h float i=2.0; float j=1.0; float sum = 0.0; main() { while (i/j 0.001) { j+=j; sum=sum+(i/j); ...
2 votes
2 votes
4 answers
2
jenny101 asked Jun 25, 2016
4,887 views
The following three 'C' language statements is equivalent to which single statement?y=y+1; z=x+y; x=x+1z = x + y + 2;z = (x++) + (++y);z = (x++) + (y++);z = (x++) + (++y)...
7 votes
7 votes
9 answers
3
pooja14 asked Jun 22, 2016
8,033 views
What is the output of the following C program? #include<stdio.h #define SQR(x) (x*x) int main() { int a; int b=4; a=SQR(b+2); printf("%d\n",a); return 0; }14361820
3 votes
3 votes
2 answers
4
ajit asked Sep 23, 2015
5,115 views
Which of the following is true with respect to Reference?A reference can never be NULLA reference needs an explicit dereferencing mechanismA reference can be reassigned a...