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 2 of 6.
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?
Vishal Naik said:
1 decade ago
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.
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.
Ankit jain said:
1 decade ago
%c will print the character according to ASCII values and %d print the corresponding number.
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.
Akash said:
1 decade ago
I can't understand because of %c and j two times print.
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?
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?
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.
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.
Manu said:
1 decade ago
Hai friends some of you are telling the answer as 225 how you people got that I can't will you explain me. Because am confused in that.
Anyone help me for that.
Anyone help me for that.
Mukesh Kumar said:
1 decade ago
What is the use of and meaning of "short int" here can anybody explain me?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers