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 3 of 6.
SATYA VAAG said:
1 decade ago
THANKS TO ALL
Manoj said:
1 decade ago
Thank you karthi.
Lalitha said:
1 decade ago
Simple,
It is in case of <= increment by 0ne of right side of the condition and then decrement it by left value;
1<=255 True
So,
(255+1)-1=255
It is in case of <= increment by 0ne of right side of the condition and then decrement it by left value;
1<=255 True
So,
(255+1)-1=255
Santhosh said:
1 decade ago
Hi, please have a look on the following program , I guess it is very easy.
#include<stdio.h>
int main()
{
int j=1;
int count=1;
while(j <= 225)
{
//printf("%c %d\n", j, j);
j++;
count++;
}
printf("%d",count++);
return 0;
}
The count value will be 225.
#include<stdio.h>
int main()
{
int j=1;
int count=1;
while(j <= 225)
{
//printf("%c %d\n", j, j);
j++;
count++;
}
printf("%d",count++);
return 0;
}
The count value will be 225.
Ivo said:
1 decade ago
This program prints the ASCII characters from 1 to 255 and their respective number. That is why there is a "%c %d" in the printf, one for the char representation of whatever number 'j' is and the number itself.
Singu hemanth said:
1 decade ago
While compilation we get output "The output size is too large (infinite loop or larger text output) ". But the answer was 255 how its possible I can't understood. Help me how its was came.
Kanika joshi said:
1 decade ago
What happens if in the above question 'short int' is replaced by 'long int' ? Please help.
Sangeetha said:
1 decade ago
When this is short int. It again wrap around to zero so definitly infinite loop.
Diana said:
1 decade ago
Sangeetha,the range of short signed int is from -32768 to +32767.So,it won't wrap around.You are confusing it with char whose range is from -128 to +127.
Prem said:
1 decade ago
If int is not mentioned signed or unsigned then we take default signed.here we are showing character too so it will execute upto +127 then it goes to -127,-126.....,0,1,.....127 that mean it will execute infinite times.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers