C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - General Questions (Q.No. 2)
2.
How many times the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h>
int main()
{
int j=1;
while(j <= 255)
{
printf("%c %d\n", j, j);
j++;
}
return 0;
}
Answer: Option
Explanation:
The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.
Discussion:
59 comments Page 5 of 6.
Manu said:
1 decade ago
Hai friends some of you are telling the answer as 225 how you people got that I can't will you explain me. Because am confused in that.
Anyone help me for that.
Anyone help me for that.
Mukesh Kumar said:
1 decade ago
What is the use of and meaning of "short int" here can anybody explain me?
Ptuan said:
1 decade ago
Short int is the signed int and it usually used as "int".
Default of system declare "int" has 4 byte. But the question declare short int has 2 byte wide, so the range from: -2^8 -> 2^8 - 1.
This mean j only from: -256 -> 255.
So the while loop will be repeat infinite times. Be cause it's never has value 256 to break the loop.
When counter from j = 1 to j=255; j++ will be: j = -256, and the loop continue. Program will never be exit.
I'm got this experience when I'm test Pascal program in high school. So please think about this carefully.
Default of system declare "int" has 4 byte. But the question declare short int has 2 byte wide, so the range from: -2^8 -> 2^8 - 1.
This mean j only from: -256 -> 255.
So the while loop will be repeat infinite times. Be cause it's never has value 256 to break the loop.
When counter from j = 1 to j=255; j++ will be: j = -256, and the loop continue. Program will never be exit.
I'm got this experience when I'm test Pascal program in high school. So please think about this carefully.
Ahmedation said:
1 decade ago
It will be infinite number of loops truly but if only the variable type was signed/unsigned char.
DEEPak said:
1 decade ago
In question 1, while loop will execute 256 times. 1 time also for false condition. But the body will execute only 255 times only.
Amandeep Kaur said:
1 decade ago
Answer is infinite times, how it will be execute 255 times?
Amol wavare said:
1 decade ago
Answer is finite times. How it will be 256>255?
Abhishek said:
1 decade ago
char i;
for(i=0;i<255;i++)
{
printf("%c",i);
}
I want to ask that after 255. What will happen? It will go to 0 and continue to run from 1, 2 or will run infinite times?
for(i=0;i<255;i++)
{
printf("%c",i);
}
I want to ask that after 255. What will happen? It will go to 0 and continue to run from 1, 2 or will run infinite times?
Pankaj said:
1 decade ago
Its signed so how 255 could be the answer?
Vibhan said:
10 years ago
Sorry friends but get excited 256 because it check while loop at the time when j value is 256. So while statement execute 256 times.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers