tags:

views:

198

answers:

3

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);  
}
+3  A: 

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.

mjv
I am not really sure never seen such stuff
RageZ
You appear to be correct. You can confirm this explanation by adding a couple of extra newlines to the beginning of the printf string like this: printf("\n\n%d\n", nl);The shell prints the ^D, and the printf overwrites the caret with its output.
Stephen Van Dahm
@RageZ: neither had I before now, but testing on MacOS X (10.5.8) shows exactly that behaviour - much to my surprise.
Jonathan Leffler
A: 

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.

Mark Rushakoff
Mark, on Windows it's the ctrl+z that sends EOF.
Nick D
+1  A: 

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.

indiv
Do you happen to know if there's an `stty` setting that controls this?
Jonathan Leffler