
Removing trailing newline character from fgets() input?
I am trying to get some data from the user and send it to another function in gcc. The code is something like this. printf("Enter your Name: "); if (!(fgets(Name, sizeof Name, stdin) != NULL)) { fprintf(stderr, "Error reading Name.\\n"); exit(1); }However, I find that it has a newline `\\n` character in the end. So if I enter `John` it ends up sending `John\\n`. How do I remove that `\\n` and send a proper string.
More
2 Answers By Expert Tutors

Patrick B. answered 06/21/19
Tutor
4.7
(31)
Math and computer tutor/teacher
#include <stdio.h>
int main()
{
char inbuff[255];
printf(" Please your name :>");
gets(inbuff);
printf(" HELLO >%s< !!!\n",inbuff);
}
===============================================
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
You can optionally replace the last character with zero, which is the NULL TERMINATOR.06/21/19