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 1 of 6.
Abhijeet Dhumal said:
3 years ago
Answer is 255.
I agree with the given answer.
I agree with the given answer.
(3)
Abdulla.S said:
5 years ago
Suppose the size of the short int is 5 bytes, then what would happen? please explain to me.
(2)
Priya said:
9 years ago
int a;
for(a=1;a<=32767;a++)
printf("%d",a);
Why it results in an infinite loop? Please explain.
for(a=1;a<=32767;a++)
printf("%d",a);
Why it results in an infinite loop? Please explain.
(2)
Dhanush said:
1 decade ago
@prem.
We are using character only in printf statement. not in condition. in while(j<=255) j is an integer variable. So in printf statemnt for %c it will print from 1,2.....127,-128,-127.....0 so exactly after 255 times of the execution of the loop. The condition fail and loop will be terminated.
We are using character only in printf statement. not in condition. in while(j<=255) j is an integer variable. So in printf statemnt for %c it will print from 1,2.....127,-128,-127.....0 so exactly after 255 times of the execution of the loop. The condition fail and loop will be terminated.
(1)
Nagabhushan said:
9 years ago
int k=1;
while (k<n)
{
k=3k;
}
How many times the loop repeats?
Please tell me the answer.
while (k<n)
{
k=3k;
}
How many times the loop repeats?
Please tell me the answer.
(1)
Aditya Gupta said:
7 years ago
But if we put char j instead of short int j. Why it runs infinite times then, When the both acquire same space of 2 bytes?
(1)
Vikas kumar said:
9 years ago
Answer is an infinite loop like from range of 0 to 128 then from -1to 128 and goes on repeating and after compiling and executing the program it gives an infinite loop.
(1)
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.
Viplov said:
1 decade ago
J=1 and while (j <= 255).
J start from 1 to 255 and run only 255 times, it goes to 255 because of <= if this sign is only < then it goes to only 1 to 254 and run only 254 times.
J start from 1 to 255 and run only 255 times, it goes to 255 because of <= if this sign is only < then it goes to only 1 to 254 and run only 254 times.
Rahul said:
1 decade ago
#include<stdio.h>
int main()
{
char result[50];
float num = 23.34;
sprintf(result, "%f", num);
printf("\n The string for the num is %s", result);
getchar();
}
This program shows an error output too large. Can anyone tell me why this error is occuring?
int main()
{
char result[50];
float num = 23.34;
sprintf(result, "%f", num);
printf("\n The string for the num is %s", result);
getchar();
}
This program shows an error output too large. Can anyone tell me why this error is occuring?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers