2
I was watching a tutorial of C
, and appeared a part about buffer cleaning using the functions fflush
and __fpurge
. So far so good, but when I tried applying with __fflush
, the GCC
returns me the following error:
warning: implicit declaration of function ‘__fpurge’; did you mean ‘__wur’? [-Wimplicit-function-declaration]
__fpurge(stdin); // limpa buffer de entrada
And in trying to use the fflush
, does not perform cleaning. How to proceed?
Code using fflush
:
#include<stdio.h>
int main(){
char letra1,
letra2;
// uso da função getc(metodo_de_entrada)
printf("Insira um caractere: \n");
letra1 = getc(stdin);
fflush(stdin); // limpa buffer de entrada
printf("Insira outro caractere: \n");
letra2 = getc(stdin);
printf("Voce digitou: '%c' e '%c'\n", letra1, letra2);
return 0;
}
Code using __fpurge
:
#include<stdio.h>
int main(){
char letra1,
letra2;
// uso da função getc(metodo_de_entrada)
printf("Insira um caractere: \n");
letra1 = getc(stdin);
__fpurge(stdin); // limpa buffer de entrada
printf("Insira outro caractere: \n");
letra2 = getc(stdin);
printf("Voce digitou: '%c' e '%c'\n", letra1, letra2);
return 0;
}
I’m using Ubuntu 18.04
and GCC 7.4.0
fflush has undefined behavior when applied to input streams (stdin). See issue 12.26a of http://www.faqs.org/faqs/C-faq/faq/. See: https://answall.com/questions/111697/limpar-buffer-em-c-com-fflush-ou-fpurge#111703
– anonimo
Just for the record, in the code you show in the question there is no need to clean the buffer.
– Isac