2

I set stdin full-buffered as follows:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char buffer[BUFSIZ];
    if (setvbuf(stdin, buffer, _IOFBF, BUFSIZ) != 0) { // here
        printf("an error occured.\n");
        exit(1);
    }
    int number;
    scanf("%d", &number);
    printf("number : %d\n", number);
    return 0;   
}

However, the result suggests that stdin is still line-buffered. Why is that?

3
number : 3
  • 4
    It's the *terminal program* that controls the "buffering" here. You can possibly change the settings for the terminal, but how to do it depends on your platform (Windows, Linux/macOS/other POSIX, or other OS type). – Some programmer dude Apr 24 '18 at 12:36
  • How do you know that `stdin` is not fully buffered? – JeremyP Apr 24 '18 at 12:46
  • @JeremyP I entered two characters `3\n`, and `BUFSIZ` is `8192` in my implementation. – eca2ed291a2f572f66f4a5fcf57511 Apr 24 '18 at 12:47
  • 1
    It is not the buffering here but the fact that the terminal puts the data into the `stdin` only on new lines. If you instead pipe the input into your `stdin`, you can see that you can read a character at a time too (before encountering a `\n`) – Ajay Brahmakshatriya Apr 24 '18 at 13:06
  • @P.P. Sorry but this is not a duplicate of the question you referenced. The questioner here wants to make the input *fully buffered* **not** *unbuffered*. Nominated for reopening. – JeremyP Apr 24 '18 at 16:23
  • @b1sub What are you expecting to happen then? Do you think it should hang in the `scanf`? – JeremyP Apr 24 '18 at 16:25
  • @JeremyP Yes. I assumed that `scanf` won't return unless the buffer is full. – eca2ed291a2f572f66f4a5fcf57511 Apr 25 '18 at 03:05
  • @b1sub I don't have an answer, but my guess is that `scanf` has access to the internal buffer and simply reads it as soon as there are enough characters in it. You might find that "fully buffered" doesn't mean anything much for input. Why is it that you are hoping to delay any processing until you have a full buffer anyway? – JeremyP Apr 25 '18 at 08:30
  • @JeremyP Just a pure curiosity. =D Thank you for the comments!! – eca2ed291a2f572f66f4a5fcf57511 Apr 25 '18 at 08:34

0 Answers0