C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 6)
6.
What will be the output of the program?
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
return 0;
}
Discussion:
36 comments Page 1 of 4.
Rather said:
6 years ago
Hello All.
For people who thinks why garbage values get printed , let us look at this ststement
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
We have four %d's, means we should have four variables to print but here we have only two representing first two :ie 4>>1 & 8>>1, last two format specifiers have no variables listed to print , so in such cases, it will print them as garbage values.
For people who thinks why garbage values get printed , let us look at this ststement
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
We have four %d's, means we should have four variables to print but here we have only two representing first two :ie 4>>1 & 8>>1, last two format specifiers have no variables listed to print , so in such cases, it will print them as garbage values.
(3)
Sai said:
6 years ago
4>>1 means divide 4 by 2 power 1 and 8>>1 means divide 8 by 2 power 1 then you will get answer as 2 and 4 if it is less then less then multiply by 2.
(2)
Teja said:
1 decade ago
@Anusha,
Given code is
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
return 0;
}
First we got the output from 4>>1,8>>1 is 2,4 directly.
But in printf() four %d's are given.so 2,4 assigns to first two %d's and the remaining two %d's will show the garbage values.
Got it?
Given code is
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
return 0;
}
First we got the output from 4>>1,8>>1 is 2,4 directly.
But in printf() four %d's are given.so 2,4 assigns to first two %d's and the remaining two %d's will show the garbage values.
Got it?
(1)
Sachin said:
1 decade ago
@Gowda.
Arrange your number in three arrays see there is no number for 3rd row, 3rd column. So it's partially initialized which has default value zero.
Arrange your number in three arrays see there is no number for 3rd row, 3rd column. So it's partially initialized which has default value zero.
Mayur said:
1 decade ago
4 = 0100 now shift to right 1 time. It will become 0010 = 2.
Then << is print on screen (becoz every special symbol or character print as well as written in double cotation " " )
Then it will print
8 = 1000 now shift to right 1 time. It will become 0100 = 4.
Then print Garbage value >> Garbage value
So,
Output will
2 >> 4 Garbage value >> Garbage value
Then << is print on screen (becoz every special symbol or character print as well as written in double cotation " " )
Then it will print
8 = 1000 now shift to right 1 time. It will become 0100 = 4.
Then print Garbage value >> Garbage value
So,
Output will
2 >> 4 Garbage value >> Garbage value
Jay said:
1 decade ago
First it solve 4>>1 and then solve 8>>1.
Answer is 2 and 4.
Then it print the value.
But here %d is 4 times in printf("").
So it print only two values and other two are garbage value.
Answer is 2 and 4.
Then it print the value.
But here %d is 4 times in printf("").
So it print only two values and other two are garbage value.
Adam said:
9 years ago
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
printf("%d >> %d >> \n", 4 >> 1, 8 >> 1);
printf("%d %d \n", 4 >> 1, 8 >> 1);
return 0;
}
Try this.
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
printf("%d >> %d >> \n", 4 >> 1, 8 >> 1);
printf("%d %d \n", 4 >> 1, 8 >> 1);
return 0;
}
Try this.
Prat said:
9 years ago
Why garbage value? Please explain clearly.
Krutika said:
9 years ago
Thank you @Mayur.
Praba said:
9 years ago
What is mean by rest is garbage?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers