2

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);  
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
deadguy
  • 21
  • 2

3 Answers3

4

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
  • 73,152
  • 14
  • 113
  • 156
  • I am not really sure never seen such stuff – RageZ Sep 20 '09 at 05:04
  • 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 Sep 20 '09 at 05:09
  • @RageZ: neither had I before now, but testing on MacOS X (10.5.8) shows exactly that behaviour - much to my surprise. – Jonathan Leffler Sep 20 '09 at 05:11
2

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
  • 17,306
  • 6
  • 61
  • 82
0

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
  • 249,864
  • 45
  • 407
  • 398