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 2 of 6.
Arun said:
1 decade ago
Good answer.
Manas said:
1 decade ago
Its answer is wrong because character is 127 to -128 so after going to 127 next j++ will take it to -128 so ..loop continues...
Saswati said:
1 decade ago
I coudnt undrstand "%c %d". and also this "j,j" . why two js are used ? have anyone checked output using compiler?
Gowrishankar said:
1 decade ago
Thank you for discussion saswati and navana.
Gowri said:
1 decade ago
%c is used to read character variable means 'g' or 'h' etc
wheare as %d is used to read integer example 22 44 etc
i checked it
wheare as %d is used to read integer example 22 44 etc
i checked it
Shalini said:
1 decade ago
I think the answer is infinite. Loop continues because in case of characters after 127 next value will be -128. Hence the loop will never come to an end.
Keerthana said:
1 decade ago
In printing statement we have %c to print the j. as u told -128 to +127 ,but the given value is 255. But it is beyond the character limit. So whether here wrap around process will take place here can pls any one explain me clearly..
Eswari said:
1 decade ago
Thank you for swathi and karthi.
Mady said:
1 decade ago
Thank you swathi.
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