fgets question

  • Thread starter Bill Cunningham
  • Start date
F

fuzhen

I was talking with someone about fgets and he said that fgets puts the
\n in a string but not \0. I decided to test this assumption because my
documentation didn't say if fgets put \0 after a string literal. Strlen was
what I used to decide the \0 was not added. How can it be added for a
string? My code.

int main (void) {
char input[10];
fgets (input, sizeof(input), stdin);
printf ("%i\n", strlen(input));

}

One more character than what was typed printf reported.

Bill

if(input[strlen(input) - 1] == '\n')
{
input[strlen(input) - 1] = '\0';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top