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 1 of 6.

Murugesan said:   1 decade ago
@ Rahul & Nantu.

Your program will execute without error if you change like below,

#include<stdio.h>
#include<string.h>
void main()
{
char result[50];
float num = 23.34;
sprintf(result, "%f", num);
printf("\n The string for the num is %s", result);
getchar();
}

In your program you wanted to convert the float value to string [sprintf(result, "%f", num);]

So you have to include "string.h" header file.

If you are not give return type [return 0;] means you should mention void in main function instead of int main.

Hope you understand.

Ptuan said:   9 years ago
Short int is the signed int and it usually used as "int".

Default of system declare "int" has 4 byte. But the question declare short int has 2 byte wide, so the range from: -2^8 -> 2^8 - 1.

This mean j only from: -256 -> 255.

So the while loop will be repeat infinite times. Be cause it's never has value 256 to break the loop.

When counter from j = 1 to j=255; j++ will be: j = -256, and the loop continue. Program will never be exit.

I'm got this experience when I'm test Pascal program in high school. So please think about this carefully.

Santhosh said:   1 decade ago
Hi, please have a look on the following program , I guess it is very easy.


#include<stdio.h>
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.

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

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?

Nantu 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?

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

Ivo said:   1 decade ago
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.

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.

Ritesh yadav said:   1 decade ago
Do it for j<=60, you will find difference. Actually %c shows ascii characters and %d is a decimal value of it. But I am still confused for j<=255, whether this loop will be executed infinite or 255 times only.


Post your comments here:

Your comments will be displayed after verification.