C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 8)
8.
What will be the output of the program?
#include<stdio.h>
int main()
{
unsigned int i = 65536; /* Assume 2 byte integer*/
while(i != 0)
printf("%d",++i);
printf("\n");
return 0;
}
Answer: Option
Explanation:
Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.
Step 1:unsigned int i = 65536; here variable i becomes '0'(zero). because unsigned int varies from 0 to 65535.
Step 2: while(i != 0) this statement becomes while(0 != 0). Hence the while(FALSE) condition is not satisfied. So, the inside the statements of while loop will not get executed.
Hence there is no output.
Note: Don't forget that the size of int should be 2 bytes. If you run the above program in GCC it may run infinite loop, because in Linux platform the size of the integer is 4 bytes.
Discussion:
16 comments Page 2 of 2.
Heena said:
8 years ago
I agree with @Vanaja.
Please explain.
Please explain.
Bhavya said:
7 years ago
I think since the range of unsigned int is 0-65535.
65536 may be taken as 0.
i.e is why there is no output?
If 'm wrong, please correct me.
65536 may be taken as 0.
i.e is why there is no output?
If 'm wrong, please correct me.
Ankit said:
5 years ago
I think that the answer is correct due to the 2-byte size of the int, that is why the code shows no output.
Sahii said:
5 years ago
@All.
I Run Above Program in Dev-C++, It Going To Infinite Loop.
So, I Think The Answer Is A.
Correct Me,If I Wrong,.
I Run Above Program in Dev-C++, It Going To Infinite Loop.
So, I Think The Answer Is A.
Correct Me,If I Wrong,.
(1)
Ritesh tripathi said:
2 years ago
The given answer is wrong because if i=65536.
The loop is run infinitely if you are using a 2-byte compiler the i++.
Is any negative value and it's going infinite.
The loop is run infinitely if you are using a 2-byte compiler the i++.
Is any negative value and it's going infinite.
(2)
Muhammad Ahsan Ullah Umer said:
2 years ago
The answer is infinite loop (A).
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers