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 1 of 2.
Sandeep said:
2 decades ago
I think the answer to this question should be A; because when i run it on code block IDE it went in an infinite loop;
In case i am wrong plz do correct me!
In case i am wrong plz do correct me!
Sundar said:
1 decade ago
@Sandeep
Don't forget that the size of int should be 2 bytes. If you run the above program in 32 bit platform, for eg. GCC under Linux, it may run infinite loop, because in Linux platform the size of the integer is 4 bytes.
To test the above program, just use Turbo C, you will understand.
I have tested it in both Turbo C and GCC.
Don't forget that the size of int should be 2 bytes. If you run the above program in 32 bit platform, for eg. GCC under Linux, it may run infinite loop, because in Linux platform the size of the integer is 4 bytes.
To test the above program, just use Turbo C, you will understand.
I have tested it in both Turbo C and GCC.
Priyanka.s said:
1 decade ago
What will be the output if that while loop became true ?
Dheeraj bajpai said:
1 decade ago
In my point of view answer is<A> with Borland C++ compiler.
Kiran Kumar Reddy P said:
1 decade ago
When I simulated using gcc in linux. It results into infinite loop printing all the values.
RAHUL said:
1 decade ago
When I run this program answer is A.
Sangram said:
10 years ago
Here in while loop if condition is like while (++i!=0) then answer will be option D i.e. no output.
Kavi said:
10 years ago
Output is zero, on 2nd iteration it gets false.
Vanaja said:
9 years ago
Whether it is 2 byte or 4 in while loop if anything is 0 then it fails right.
So can anyone please tell me the variance of bit system and also I'm thinking the answer would be 0.
Because in the program it checks (i!=0) so (65536!=0) and then it incremented to 0. So now the condition failed and why can't the answer is 0?
please reply me!
So can anyone please tell me the variance of bit system and also I'm thinking the answer would be 0.
Because in the program it checks (i!=0) so (65536!=0) and then it incremented to 0. So now the condition failed and why can't the answer is 0?
please reply me!
Raj shamra said:
9 years ago
Yeah. Actually, I run this piece of code in terminal and the output what I get is an infinite loop.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers