0

The following C code takes so long to compile and execute.

#include <stdio.h>

int main()
{
    int max;
    printf("Enter a value for max: ");
    scanf("%d", &max);

    for (int i=0; i<max; i++)
    {
        printf("%d\t", i);
    }

    return 0;
}

But, If I initialize the max variable with some number like

int max = 0;

The Compilation and execution is almost instantaneous. Can someone explain why?

Edit:

When I printed the value of the variable max before my input, it showed 2203648 (some garbage value). Instead of "int max = 0", if i assign

int max = 2203648;

the compilation and execution takes the same long time. But, as mentioned earlier, if i assign max say

int max = 200;

the compilation and execution is instantaneous. Does it have to do anything with the pre-assigned garbage value?

Also, this problem occurs only in windows computers, I tested with ubuntu, and the compilation and execution is instantaneous in both version of the code.

In Windows 10:

compilation and execution, as of "Enter a value for max: " appears on screen:

without variable initialization = around 8 seconds

with variable initialization = instantaneous

compiler - gcc

  • So what is the difference in time? – Andrey Chernukha Feb 10 '21 at 15:33
  • With what compiler? Executing how? How long is "so long"? – underscore_d Feb 10 '21 at 15:33
  • The compiler is gcc. Without variable initialization, the compilation takes around 8 seconds. With vairable initialization, its almost instantaneous. – Surya Narayanan Feb 10 '21 at 15:35
  • 2
    @SuryaNarayanan: This sounds very strange. Can you reliably reproduce the problem? Or could it be coincidence? Do you have maybe other programs running that cause frequent slowdowns of your computer? Can you reproduce the problem on more than one computer? – Andreas Wenzel Feb 10 '21 at 15:43
  • 5
    I would guess that you have a pre-compiled binary for one version or the other sitting in your project folder. And then your IDE notices that one version does not contain any changes compared to the one that generated said binary. Are you using any IDE and if so which one? – Lundin Feb 10 '21 at 15:44
  • I used VS Code. – Surya Narayanan Feb 10 '21 at 16:15
  • 1
    You say "compilation and execution" is affected, but you only report compilation time. Is also execution affected? – Jack Feb 10 '21 at 16:16
  • I have tested several times and I was able to reliably able to reproduce in several Windown PCs. – Surya Narayanan Feb 10 '21 at 16:16
  • Yes execution also takes some considerable time before "Enter the value for max: " shows up on screen. – Surya Narayanan Feb 10 '21 at 16:17
  • 4
    Run the compile command manually, remember to clean between each run. I believe the time should be almost be the same. However when running the compiled binary then the antivirus blocks one of your exe file so it starts much more slowly. I've experienced that myself a lot – phuclv Feb 10 '21 at 16:29
  • Is the code that you posted an **exact copy** of the actual code you are using? Or did you maybe make a slight modification (which you consider insignificant) before posting it, without actually testing the modified code? – Andreas Wenzel Feb 11 '21 at 17:41
  • @Andreaswenzel This is the exact copy without any modifications. Also, what I wonder is, why this problem doesn't occur in Ubuntu. It only occurs in Windows computers. – Surya Narayanan Feb 13 '21 at 00:31

2 Answers2

0

The scanf is failing. Check the return value.

i.e.

   if (scanf("%d", &max) != 1) {
      fprintf(stderr, "Unable to read max");
      exit(1);
   }

max is probably some large value hence the large amount of time

EDIT

The delay to see the prompt is that the printf is in a buffer and will not be displayed until the loop completes

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • 1
    As you have said, max is pre-assigned a very huge garbage value. Is that the reason why the compilation and execution might have taken so long? – Surya Narayanan Feb 10 '21 at 17:06
  • Most likely I guess. Simply print it out before the loop – Ed Heal Feb 10 '21 at 17:27
  • But you _can't_ print or otherwise read an uninitialised variable, as that always yields UB, and UB can include the mere act of adding a print to make the value _seem_ like zero. Basically, one must simply avoid UB, instead of trying to reason about it. – underscore_d Feb 10 '21 at 17:36
  • @underscode_d In C you can print an uninitialized variable. It is not UB the print. Its value is some random value. It still has a value. – Ed Heal Feb 10 '21 at 19:25
  • This answer would explain a delay of 8 seconds after the prompt `"Enter a value for max:"` appears. However, OP stated that the delay is 8 seconds before the prompt appears. Therefore, this answer does not answer OP's question (assuming that the information provided by OP is accurate). – Andreas Wenzel Feb 11 '21 at 17:39
  • 1
    See edit for an explanation of that – Ed Heal Feb 11 '21 at 17:45
  • @EdHeal: I highly doubt that this is the reason for the behavior described by OP. However, you are right that it is possible, as [§7.21.3 ¶3 of the ISO C standard](http://port70.net/~nsz/c/c11/n1570.html#7.21.3p3) only recommends (not does not require) that `stdout` is flushed when input is requested from `stdin`. This could easily be tested by OP, by inserting an `fflush(stdout);` statement before the `scanf` statement. – Andreas Wenzel Feb 11 '21 at 18:38
-1

I think variable initialization of max=0 doesn't make that great deal of a difference, but god knows why in ypur case its taking 8seconds. I think you should reinstall your GCC Compiler set your path variables correctly once more, and try the above code on a different IDE.

Vivek Hotti
  • 69
  • 2
  • 9