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;
}
Infinite times
255 times
256 times
254 times
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 4 of 6.

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

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...

Ashish said:   1 decade ago
Since the int is 2 bytes here, wouldn't it become zero and the loop continue indefinitely. Please Help to clear this doubt.

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)

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.
(2)

Tara said:   10 years ago
The loop will execute for infinity times, like 0 to 128.

Then -128 to -1 again from 0-128, the loop repeats.

Ahmedation said:   1 decade ago
It will be infinite number of loops truly but if only the variable type was signed/unsigned char.

Nagabhushan said:   9 years ago
int k=1;
while (k<n)
{
k=3k;
}

How many times the loop repeats?

Please tell me the answer.
(1)

Ankit jain said:   1 decade ago
%c will print the character according to ASCII values and %d print the corresponding number.

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)


Post your comments here:

Your comments will be displayed after verification.