0

Possible Duplicate:
carriage return by fgets

I have this code:

char arch[80];
fgets (arch, sizeof(arch), stdin); 

When I access the value of arch it shows the value I have entered from stdin followed by an interrogation mark, why is this happening?

Community
  • 1
  • 1
cracq
  • 81
  • 1
  • 9

2 Answers2

4

fgets() stores the trailing newline:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

If you are inspecting the variable in some kind of debugger, it's possible that the newline gets displayed as "?", or whatever you mean by "interrogation mark".

nibot
  • 14,428
  • 8
  • 54
  • 58
3

My guess is that it represents the \n line terminator in whatever you use to examine the value of arch. SO has a lot of questions regarding the line termination, I'm sure you can easily find a number of ways to deal with it.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • what is SO? I thought fgets would be better than scanf... I'm trying to get a file name from the standard input and i dont want a '?' on it. – cracq Oct 23 '12 at 13:49
  • SO is stack overflow. Stripping the newline is as easy as `arch[strlen(arch)-1]=0`, provided you know it's there. – Michael Krelin - hacker Oct 23 '12 at 20:19