Is this the right way to use fgets when its for non file input.

#include<stdio.h>

int main()
{
    char userinput[256];
    
    printf("Enter a string:");
    fgets(userinput,256,stdin);
    printf("%s",userinput);
    
    return 0;
}

>Is this the right way to use fgets when its for non file input.
Yup.
And if you use sizeof instead of the literal array size, you can change
at any time how big userinput can be, and still work.

Thanks :-)

>Is this the right way to use fgets when its for non file input.
No, you forgot to check the return value, and you need to flush stdout before calling fgets when the prompt doesn't have a newline. This is mostly correct:

#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;
}

>This is mostly correct
So the answer is still no?.
Since you decided to go the whole nine yards with it, why to stop in mostly?. Could you explain why you said is mostly?. Also could you explain what's the harm of not using an if to check for the return of the fgets function?

>So the answer is still no?
Probably.

>Since you decided to go the whole nine yards with it, why to stop in mostly?
I didn't go the whole nine yards. What I gave was the bare minimum for using fgets.

>Could you explain why you said is mostly?
Sure, that program doesn't take into account "successful but failed" calls where the buffer is filled but no newline is stored. What do you do with the unread characters? How do you guarantee that the output will be consistent even if the buffer doesn't have a newline? These things have less to do with fgets and more to do with the solution as a whole, which is why I left it out.

>Also could you explain what's the harm of not using an if to check for the return of the fgets function?
That's an easy one:

#include<stdio.h>

int main ( void )
{
  char userinput[256];
    
  printf ( "Enter a string: " );
  fflush ( stdout );

  fgets ( userinput, sizeof userinput, stdin );
  printf ( "%s", userinput );
    
  return 0;
}

Run this, but when prompted, signal EOF. The harm is that now you're using an uninitialized string. You can avoid it by initializing the buffer to an empty string, but the OP didn't think to do that, and you didn't notice that it was a problem. Subtle bugs like that are best avoided through good practices like always checking for success.

Very much appreciated!. I really needed to know these concepts.
One more inquire:
If the array would have been initialized to '\0' as Mr. S.O.S. always points out.
char userinput[256] = { '\0' };
Would that have made it mostly correct?.

>Would that have made it mostly correct?
If all you're doing is printing the string, sure. But for pretty much any other program you want to know if fgets failed and be able to react differently. For that, the easiest way is to check the return value and/or the error state.

Alright. Many thanks.

Member Avatar for iamthwee

Why don't you just read the tutorials?

>Why don't you just read the tutorials?
...Let me see...I, I am almost sure that one your thinking of I have read...No, that one no, the other one. Yeap!. I'll take a look as soon as possible. Thanks!.

Why don't you just read the tutorials?

Probably because Narue, with a little prodding, gave a more compete answer than most tutorials. And you never know which tutorials only give you part of the information... :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.