IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Control Instructions - Discussion

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

Read more:

"We cannot solve our problems with the same thinking we used when we created them."
- 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

Answer: Option C

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.

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!