Basic C problem(code snippet)

Member
Posts: 63
Joined: 2005.04
Post: #1
Why does this return the number of lines with a D beside it?

Code:
#include <stdio.h>

// count lines in input

main()
{
    int c, n1;
    
    n1 = 0;
    while ((c = getchar()) != EOF)
    if (c == '\n')
        ++n1;

    printf("%d\n", n1);
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
er, it doesn't?

if you're in interactive mode and use ctrl-D to input an EOF, it seems to overwrite the line that says "^D", meaning that if you've got a single digit of output you'll still see the D.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
It doesnt, try %i instead maybe?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #4
Works fine for me.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
I ran it using "cat basic.c | ./basic.o" and it worked, how are you running it?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #6
.o is an unlinked object file; you shouldn't be naming true executables with the .o extension. Executables usually don't have an extension. (Just FYI)

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #7
oh its basic.out sorry

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 63
Joined: 2005.04
Post: #8
I get it... the EOF stops the process but simultaneously prints D...Sorta?

I don't even think itsa problem Blush
LOL

reread OSC's post, makes sense now
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #9
EOF is entered by hitting ctrl-D, which shows up in the Terminal as a ^D, much as a break (ctrl-C) shows as ^C. Your shell prints out the ^D sequence, and then your program writes out a numberin the same position.
^D becomes
9D or something, if you print a 9.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Problem Weak-linking a framework in Code Warrior 9.6 lord_nemesis 2 2,653 Nov 25, 2005 01:00 AM
Last Post: lord_nemesis