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
  • 4 Try adding fflush(stdout); after your printf statement. Is the output faster? – R_Kapp Commented Jan 31 at 14:16
  • 1 If I understand your question then the problem is this printf("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
  • OT: As far as I know it's not required by the C standard but many systems will flush stdout when input is requested. Seems your system doesn't... – 4386427 Commented Jan 31 at 14:31
  • Anyway I do believe this is a FAQ and there should be a decent canonical duplicate somewhere. – Lundin Commented Jan 31 at 14:40
  • Are you running these programs inside an IDE, using its I/O window (by whatever name it calls that)? The behavior of such such IDE-based interactive I/O facilities sometimes seems anomalous, and although they generally try their best to simulate a terminal, they are notorious for not getting that completely right. – John Bollinger Commented Jan 31 at 14:51
 |  Show 3 more comments

1 Answer 1

Reset to default 4

Your (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