admin管理员组文章数量:1313999
#include <stdio.h>
int main()
{
int i, count;
printf("Enter an integer: ");
scanf("%d", &count);
for (i = 1; i <= count; i++)
{
printf("%d\n", i);
}
return 0;
}
It is taking too long to print "Enter an integer: " when I assign user input (count) as an int type variable; but as a float type variable, it is running in short time. Like this:
#include <stdio.h>
int main()
{
int i;
float count;
printf("Enter an integer: ");
scanf("%f", &count);
for (i = 1; i <= count; i++)
{
printf("%d\n", i);
}
return 0;
}
Even when I am already assigning 'count' in the code as an int type variable, it runs smoothly. Like this:
#include <stdio.h>
int main()
{
int i, n = 10;
for (i = 1; i <= n; i++)
{
printf("%d\n", i);
}
return 0;
}
Can anyone explain, why this is happing?
#include <stdio.h>
int main()
{
int i, count;
printf("Enter an integer: ");
scanf("%d", &count);
for (i = 1; i <= count; i++)
{
printf("%d\n", i);
}
return 0;
}
It is taking too long to print "Enter an integer: " when I assign user input (count) as an int type variable; but as a float type variable, it is running in short time. Like this:
#include <stdio.h>
int main()
{
int i;
float count;
printf("Enter an integer: ");
scanf("%f", &count);
for (i = 1; i <= count; i++)
{
printf("%d\n", i);
}
return 0;
}
Even when I am already assigning 'count' in the code as an int type variable, it runs smoothly. Like this:
#include <stdio.h>
int main()
{
int i, n = 10;
for (i = 1; i <= n; i++)
{
printf("%d\n", i);
}
return 0;
}
Can anyone explain, why this is happing?
Share Improve this question asked Jan 31 at 14:13 Everyday LearnerEveryday Learner 111 silver badge3 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 4Your (now apparently deleted) comment from the staging ground revealed the probable answer.
printf (and other output functions) do not necessarily print to stdout (i.e. the terminal, in this case) immediately, because this is a relatively slow operation. Rather, they might (but are not required to) buffer the output from several printf calls and then print it all at once (which is faster). A complete description of what determines when the buffer will actually be printed would be a bit too complicated to be worth going into here, but a very common condition is to wait until either a certain number of characters have been accumulated, or a newline has been printed.
In this case, there's no newline and not many characters, so your prompt doesn't get printed. The solution is to tell it to flush the buffer immediately by adding a call to fflush(stdout);
.
As for why it chooses to flush faster when reading a float...no idea. It's permitted to do so, but it's weird that it chooses to.
本文标签: Taking too long to run code in loop in C when user input is int instead of floatStack Overflow
版权声明:本文标题:Taking too long to run code in loop in C when user input is int instead of float - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741915023a2404678.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
fflush(stdout);
after yourprintf
statement. Is the output faster? – R_Kapp Commented Jan 31 at 14:16printf("Enter an integer: ");
line!? If so I don't understand the last example as that line is deleted... – 4386427 Commented Jan 31 at 14:29