Search code examples
cgcccompilationcodeblocks

My program takes too long to start when there is an input needed from the user


I'm using Code::Blocks with the GCC compiler to write C programs as a first-year CS student. I encountered a strange issue when using scanf() in my programs.

The program compiles normally, but when I run it, the terminal window opens immediately and takes about 15 seconds to display any messages or input prompts. However, if I stop the program and re-run it without rebuilding, it starts instantly. The issue reappears if I rebuild the program. When I remove the scanf() calls, the program runs without any delay.

I've tried different approaches but couldn't figure out why this happens. Any insights into what might be causing this delay or how to fix it?

Example of a program:

#include <stdio.h>

main() {
    int i, x, n;
    int s = 1;
    printf("entrer x \n");
    scanf("%d", &x);
    printf("entrer n \n");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        s = s * x;
    }
    printf("%d ^ %d = %d \n", x, n, s);
}

Solution

  • I just don't think this is a program problem. On Windows, calling CRT functions is not efficient, though. The main reason is because of viruses. Your system may take some checking before calling scanf().
    My suggestion is to try disable virus checking and so on.