Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C: Weird getchar() behaviour

Name: Edward 2006-07-30 19:04

The following segment of code is supposed to get characters and repeat them back at me until EOF is entered, variable c has been declared as int.

    while((c = getchar()) != EOF)
    {
        printf("Debug:: c = %d\n", c);
    }

However the debug output shows that getchar() is acting weird:
1
Debug:: c = 49
Debug:: c = 10
2
Debug:: c = 50
Debug:: c = 10

It seems like the loop is running once more everytime I hit enter. Is this a problem with:
a) This use of getchar in a while() condition (K&R says this is okay)?
b) A problem with gcc? (running on OS X)
c) Something else?

Please help...

Name: Anonymous 2006-08-01 18:52

>>10

Still valid code.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List