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 4 of 6.
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.
Sangeetha said:
1 decade ago
When this is short int. It again wrap around to zero so definitly infinite loop.
Kanika joshi said:
1 decade ago
What happens if in the above question 'short int' is replaced by 'long int' ? Please help.
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.
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.
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.
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
Manoj said:
1 decade ago
Thank you karthi.
SATYA VAAG said:
1 decade ago
THANKS TO ALL
Pavani said:
1 decade ago
The answer is 256 its not 255 because the while loop gets executed when j=255.that is it takes 255 loop.but the while condition is executed untill the condition is failed.
So it fails when j=256.
So it fails when j=256.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers