Reputation: 21
I have a very simple program that just prints the number of newlines as an integer and I get a "D" after every number.
Sample input:
d [enter]
e [enter]
f [enter]
Ctrl-D [enter]
Sample output:
3D
What am I doing wrong?
This is verbatim from The C Programming Language 2nd edition, pg. 19:
#include <stdio.h>
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
Upvotes: 2
Views: 434
Reputation: 258198
This works fine for me with GCC, whether I pipe in input or manually type it in and finish with ^D.
$ ./a.out 1 2 3 3 $ echo -ne "1\n2\n3\n" | ./a.out 3
It's probably like @mjv said, and the console is echoing the D back to you -- you're on Windows, right? I think that's normal for the Windows console.
Upvotes: 0
Reputation: 75125
I think the D comes from the Ctrl D. The console outputs ^D as its standard echo logic, prior to passing the corresponding char (or rather here lack of char, i.e. EOF status) to getchar(), however, and rightfully, not sending a cr/lf. The C program then sends its 3, et voila...
Try the program by typing more than 9 CR before exiting and the problem should "go away", i.e. not show.
Upvotes: 4
Reputation: 17856
Change the print line to this:
printf("\n%d\n", nl);
Then you'll see that when you hit ctrl-d, you get "^D" on the line. Only since you didn't press ctrl-D followed by Enter, then it's not on a newline in your original program. Not all systems will echo ctrl-d back to you, but it does on OS-X for example. So it ends up messing up the output if you print a one-digit number. You'll have to work around it.
Upvotes: 2