Your terminal operates in something called "canonical mode" (also called "line-buffered mode"). That means that stdin is actually buffered -- your program isn't aware that anything is on stdin until the user presses enter (this is the trigger for canonical terminals). This is a more efficient way of handling input than non-buffered input.
Because of this, when using functions like scanf
, you won't get any data from stdin until you press enter, at which point you'll get the entire string.
while(scanf("%s",s) != EOF) // get a line of input from stdin until input is finished
{
int i;
for(i = 0; s[i] != '\0'; i++) // scan each character of that line
{
// check the value of s[i] and do some action
}
}
Note that this will keep going until an EOF character is sent instead of a line of text. That can happen in two scenarios: if input is being piped from a file, it happens at end of file, and if input is coming from the keyboard at the terminal, it happens when you send an interrupt (ctrl+C).