C

Asked • 06/20/19

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.

Patrick B.

You can optionally replace the last character with zero, which is the NULL TERMINATOR.
Report

06/21/19

2 Answers By Expert Tutors

By:

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.