Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing

# C Programming - Control Instructions - Discussion

@ : Home > C Programming > Control Instructions > General Questions - Discussion

"Everything should be made as simple as possible, but not simpler."
- Albert Einstein
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;
}
``````

 [A]. Infinite times [B]. 255 times [C]. 256 times [D]. 254 times

Explanation:

The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.

 Karthi said: (Sat, Dec 11, 2010 12:29:10 PM) Initial value of j is 1 and the last condition of the while statement is j=225. so the loop get executed 225 times. It does not change according to declaration.

 Vivek Kkumar said: (Wed, Apr 6, 2011 08:05:01 AM) I could not understand.

 Nayana said: (Tue, Jun 7, 2011 09:23:12 AM) Hi vivek in the program the initital value of j=1 the statement in the while condition will execute until j value will become 255. so the statement will execute 255 times

 Ravitheja said: (Sat, Jul 2, 2011 12:22:11 AM) Thnks to all. Karthi please chek your values in this discussion.

 Rachit said: (Fri, Jul 29, 2011 08:41:19 PM) Loop will execute from j=0 to 224 at j=225 loop will terminated so it will execute upto 225 times from 0 to 224. I think it makes it more clear.

 Fanta said: (Mon, Aug 8, 2011 11:44:01 PM) The body of the while loop will execute 255 times. But the while condition will execte one more time to became condition false. It takes 256.

 Dhronan said: (Mon, Sep 5, 2011 11:25:49 PM) @fanta you are right but see they didn't say while statement, they said while loop, so you ought consider the whole loop statements.

 Jaya said: (Mon, Sep 12, 2011 09:13:30 AM) In printf, why there are using %c. I can't understand please explain..

 Asha said: (Mon, Sep 12, 2011 05:10:44 PM) I can't understand using of %c please explain.

 Swathi said: (Tue, Oct 4, 2011 01:43:46 PM) %c %d means short integer, according to this concept it won't affect while. Hence while loop get executed by 255 times...understand.

 Arun said: (Tue, Oct 11, 2011 11:00:18 AM) Good answer.

 Manas said: (Fri, Oct 14, 2011 01:09:26 AM) 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: (Wed, Oct 26, 2011 03:57:12 PM) I coudnt undrstand "%c %d". and also this "j,j" . why two js are used ? have anyone checked output using compiler?

 Gowrishankar said: (Wed, Dec 7, 2011 05:57:09 AM) Thank you for discussion saswati and navana.

 Gowri said: (Wed, Dec 7, 2011 05:59:50 AM) %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

 Shalini said: (Thu, Dec 8, 2011 11:22:01 PM) 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: (Thu, Dec 15, 2011 09:33:21 PM) 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: (Wed, Dec 28, 2011 01:45:58 PM) Thank you for swathi and karthi.

 Mady said: (Thu, Jan 5, 2012 08:27:15 PM) Thank you swathi.

 Pavani said: (Mon, Jan 16, 2012 12:26:16 PM) 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.

 Satya Vaag said: (Thu, Jan 19, 2012 03:57:32 PM) THANKS TO ALL

 Manoj said: (Wed, Feb 1, 2012 03:40:21 PM) Thank you karthi.

 Lalitha said: (Wed, Feb 15, 2012 01:46:36 PM) 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

 Santhosh said: (Tue, Mar 27, 2012 12:57:55 PM) Hi, please have a look on the following program , I guess it is very easy. #include 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: (Thu, May 31, 2012 07:02:07 AM) 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: (Wed, Jul 11, 2012 11:40:10 AM) 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: (Sun, Jul 15, 2012 02:45:47 PM) What happens if in the above question 'short int' is replaced by 'long int' ? Please help.

 Sangeetha said: (Sun, Aug 26, 2012 10:05:52 AM) When this is short int. It again wrap around to zero so definitly infinite loop.

 Diana said: (Fri, Aug 31, 2012 04:21:09 PM) 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: (Fri, Sep 7, 2012 05:41:32 PM) 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.

 Dhanush said: (Mon, Sep 10, 2012 02:32:00 PM) @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.

 Viplov said: (Wed, Oct 24, 2012 11:12:31 AM) 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.

 Rahul said: (Fri, Apr 5, 2013 07:02:35 PM) #include 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?

 Vishal Naik said: (Fri, May 10, 2013 07:43:46 PM) Do it manually for a shorter number, ex while(j <= 3), you will get the answer as 3. Similarly when while(j <= 255), Answer is 255.