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.
Mady said:
1 decade ago
Thank you swathi.
Eswari said:
1 decade ago
Thank you for swathi and karthi.
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..
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.
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
Gowrishankar said:
1 decade ago
Thank you for discussion saswati and navana.
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?
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...
Arun said:
1 decade ago
Good answer.
Swathi said:
1 decade ago
%c %d means short integer, according to this concept it won't affect while.
Hence while loop get executed by 255 times...understand.
Hence while loop get executed by 255 times...understand.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers