K
kernelxu
hi, everyone.
now, I'am confused on such a problem:
function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0)
can set the stadard input stream unbuffered.
however, why does my program work like buffered?
the program is below
#include <stdio.h>
int main(void)
{
char c1 = '\0';
char c2 = '\0';
setbuf(stdin, NULL);/***************/
printf("\n begin \n");
c1 = getchar();
printf("c1 = %d\n", c1);
c2 = getchar();
printf("c2 = %d\n", c2);
printf("\n over \n");
/*system("pause");*/
return 0;
}
result
c2 = 10
If the input stream is unbuffered, where does the 'new line' character
come from?
but, if I change the program into the following one:
#include <stdio.h>
int main(void)
{
char c1 = '\0';
char c2 = '\0';
printf("\n begin \n");
c1 = getchar();
printf("c1 = %d\n", c1);
setbuf(stdin, NULL);/**********************/
c2 = getchar();
printf("c2 = %d\n", c2);
printf("\n over \n");
/*system("pause");*/
return 0;
}
everything seems OK.
why did this happen?
I think setbuf() should be called before any operations on the
stream,and would be enough to be called only once in a source file.
can anybody give me some reasons?
best regards!
now, I'am confused on such a problem:
function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0)
can set the stadard input stream unbuffered.
however, why does my program work like buffered?
the program is below
#include <stdio.h>
int main(void)
{
char c1 = '\0';
char c2 = '\0';
setbuf(stdin, NULL);/***************/
printf("\n begin \n");
c1 = getchar();
printf("c1 = %d\n", c1);
c2 = getchar();
printf("c2 = %d\n", c2);
printf("\n over \n");
/*system("pause");*/
return 0;
}
result
c2 = 10
If the input stream is unbuffered, where does the 'new line' character
come from?
but, if I change the program into the following one:
#include <stdio.h>
int main(void)
{
char c1 = '\0';
char c2 = '\0';
printf("\n begin \n");
c1 = getchar();
printf("c1 = %d\n", c1);
setbuf(stdin, NULL);/**********************/
c2 = getchar();
printf("c2 = %d\n", c2);
printf("\n over \n");
/*system("pause");*/
return 0;
}
everything seems OK.
why did this happen?
I think setbuf() should be called before any operations on the
stream,and would be enough to be called only once in a source file.
can anybody give me some reasons?
best regards!