In C, the statement fflush(stdin)
will invoke undefined behavior, which means that anything can happen. It generally does not guarantee any specific behavior. Even if this works as intended on one platform, you cannot be sure that it will also work on another platform. See this question for further information.
For this reason, you should not use fflush(stdin)
, but you should rather examine whether the result of fgets
contains a newline character. That way, you can determine whether the entire line was read in or not. If not, then you can read and discard the remainder of the line, for example by calling getchar()
in a loop. In contrast to fflush(stdin)
, this is guaranteed to work on all platforms.
The line
if (strlen(buffer) > 9) {
is not meaningful, because buffer
is unable to store a string whose length is larger than 9
. Therefore, this condition will always be false. Instead, you should check whether buffer
contains a newline character or not, as already mentioned above.
Also, your posted code is designed in such a way that the program will terminate when the user enters end-of-file in the console/terminal. However, this requires the user to press a special key combination which depends on which operating system is being used. In the comments section, you stated that you instead want the user to enter "end"
when the program should terminate.
Here is an example program which implements everything that I mentioned above:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void get_line_from_user( const char prompt[], char buffer[], int buffer_size );
int main( void )
{
char buffer[10];
for (;;) //infinite loop, equivalent to while(1)
{
get_line_from_user(
"Please enter up to 9 characters: ",
buffer, sizeof buffer
);
if ( strcmp( buffer, "end" ) == 0 )
{
printf( "Ending program.\n" );
exit( EXIT_SUCCESS );
}
printf( "Input: %s\n", buffer );
}
}
//This function will read exactly one line of input from the
//user. It will remove the newline character, if it exists. If
//the line is too long to fit in the buffer, then the function
//will automatically reprompt the user for input. On failure,
//the function will never return, but will print an error
//message and call "exit" instead.
void get_line_from_user( const char prompt[], char buffer[], int buffer_size )
{
for (;;) //infinite loop, equivalent to while(1)
{
char *p;
//prompt user for input
fputs( prompt, stdout );
//attempt to read one line of input
if ( fgets( buffer, buffer_size, stdin ) == NULL )
{
printf( "Error reading from input!\n" );
exit( EXIT_FAILURE );
}
//attempt to find newline character
p = strchr( buffer, '\n' );
//make sure that entire line was read in (i.e. that
//the buffer was not too small to store the entire line)
if ( p == NULL )
{
int c;
//a missing newline character is ok if the next
//character is a newline character or if we have
//reached end-of-file (for example if the input is
//being piped from a file or if the user enters
//end-of-file in the terminal itself)
if ( (c=getchar()) != '\n' && !feof(stdin) )
{
printf( "Input was too long to fit in buffer!\n" );
//discard remainder of line
do
{
if ( c == EOF )
{
printf( "Error reading from input!\n" );
exit( EXIT_FAILURE );
}
c = getchar();
} while ( c != '\n' );
//reprompt user for input by restarting loop
continue;
}
}
else
{
//remove newline character by overwriting it with
//null character
*p = '\0';
}
//input was ok, so break out of loop
break;
}
}
This program has the following behavior:
Please enter up to 9 characters: Happy
Input: Happy
Please enter up to 9 characters: Happy11111
Input was too long to fit in buffer!
Please enter up to 9 characters: Happy1111
Input: Happy1111
Please enter up to 9 characters: end
Ending program.