I new to C and I'm learning how to get input for strings.I showed this on a other forum and they said I did it wrong I corrected it but they really didn't explain what was wrong with it.
here is my code
Code:
#include<stdio.h>
int main()
{
char userinput[256];
printf("Enter a string:");
fgets(userinput,sizeof userinput,stdin);
printf("%s",userinput);
return 0;
}
And here is the corrected one can someone please explain why I flush stdout and put a if there
Code:
#include<stdio.h>
int main ( void )
{
char userinput[256];
printf ( "Enter a string: " );
fflush ( stdout );
if ( fgets ( userinput, sizeof userinput, stdin ) != NULL )
printf ( "%s", userinput );
return 0;
}