1

I am a beginner in C and would like to know what's the problem about my code here :

#include "stdio.h"

int main(void)
{ 
    int a;

    printf("Please input an integer value: ");
    scanf("%d", &a);
    printf("You entered: %d\n", a);

    return 0;
}

My problem is that I have to type a value before having any consol output, for example if I type 7, I get this console output : Please input an integer value: You entered: 7

I tried the exact same code in another computer and it worked pretty well, I guess it's a buffer problem ? but I have no idea how to fix it.. Any ideas please ?

Christian Gibbons
  • 4,005
  • 1
  • 12
  • 27
  • 4
    you need to either print a newline character, or flush `stdout` since it is buffered: `fflush(stdout);` – Christian Gibbons Feb 18 '20 at 19:35
  • Thank you Christian, it works ! But can I have more details about it ? Why does it work in my friends computer and not in mine without flushing ? Will I have to do it each time ? – Mehdi Zayene Feb 18 '20 at 19:39
  • 5
    In some environments stdio buffers are flushed automatically on input functions like `scanf()` or `fgets()`, but this is nothing you can depend on. – Ctx Feb 18 '20 at 19:42
  • Okay, that's pretty clear! Thank you all ! – Mehdi Zayene Feb 18 '20 at 19:44
  • Some systems will flush the buffer when a newline is output, but you don't write one before the `scanf`. – Weather Vane Feb 18 '20 at 19:44
  • I believe you can force the stream to be unbuffered by way of `setvbuf` if you really wanted to. – Christian Gibbons Feb 18 '20 at 19:50
  • https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – yano Feb 18 '20 at 20:34

1 Answers1

1

As other already mentioned, to guarantee that that line is going to be printed at that point in your code you can flush the standard output like this,

#include "stdio.h"

int main(void)
{ 
    int a;

    printf("Please input an integer value: ");
    fflush(stdout);
    scanf("%d", &a); 
    printf("You entered: %d\n", a);

    return 0;
}

you can read this for more details, Why does printf not flush after the call unless a newline is in the format string?

updated thanks to @Osiris comments

myradio
  • 1,549
  • 1
  • 11
  • 23
  • Well, it doesn't really matter in this case. – myradio Feb 18 '20 at 20:17
  • No. That is not the case, the your input from `scanf()` you will see it anyways, to hide the input you have to use a function with masking. – myradio Feb 18 '20 at 20:21
  • The output itself you won't see until the `printf()`. Maybe what might be useful is to put another `fflush()` after the second printf inc ase the code grows. Or as mentioned in the link, just set the buffer to 0: `setbuf(stdout, NULL);` – myradio Feb 18 '20 at 20:23
  • You are totally right. Potentially the first printf could have occured after the `scanf()` asked for input. – myradio Feb 18 '20 at 20:27
  • Since my comments do not apply to the updated answer i will delete them. – Osiris Feb 18 '20 at 20:29