Jason Kim
Jason Kim

Reputation: 19031

When using "%ld", "D" shows up after a number

I am currently reading The C Programming Language (1st ed). In section 1.5, under Character Counting, the book shows you how you can count the number of characters.

#define EOF -1

main()
{
    long nc;

    nc = 0;
    while (getchar() != EOF)
        ++nc;

    printf("%ld\n", nc);
}

When I type

abcdefghijkl
13

This is right. abcdefghijkl is 12 characters and \n is 1 character.

But when I do something like

qwer
5D

D pops up when I do character counts under 10. Where is D coming from?


edit 1: Not sure if it is relevant but I am using gcc compiler on Mac.

Upvotes: 1

Views: 189

Answers (1)

Cornstalks
Cornstalks

Reputation: 38218

It's just your terminal showing you your EOF character ^D (the 5 is just covering up ^, and when it's double digits it's covering up all of ^D). If you do:

echo qwer | ./myprogram

You'll see it's not there.

Upvotes: 3

Related Questions