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

Pages: 1-

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-07-30 19:43

The output is producing exactly what it should. You seem confused on the difference between EOF and a line feed.

The actual string you entered is "1\n" (i.e. 1 and then enter); thus getchar() parses that by first returning 1 (that's 49), then \n (that's the line feed you get from pressing the enter key, i.e. 10), then EOF (which exits the loop).

Name: Anonymous 2006-07-31 1:32

C sucks sure there's an ebuild for C but it just get dropped to /opt, it's statically linked, and it's CLOSED SOURCE, which means that it is a BINARY package.

Name: Anonymous 2006-07-31 1:39

Copypasta sucks sure there's an ebuild for copypasta but it just get dropped to /opt, it's statically linked, and it's CLOSED SOURCE, which means that it is a BINARY package.

Name: Anonymous 2006-07-31 2:25

Ruby sucks sure there's an ebuild for ruby but it just get dropped to /opt, it's statically linked, and it's CLOSED SOURCE, whitch means that it is a BINARY package

Name: Anonymous 2006-07-31 2:30

/prog/ sucks sure there's an ebuild for /prog/ but it just get dropped to /opt, it's statically linked, and it's CLOSED SOURCE, whitch means that it is a BINARY package

Name: Anonymous 2006-07-31 4:08

("%s sucks sure there's an ebuild for %s but it just get dropped to /opt, it's statically linked, and it's CLOSED SOURCE, whitch means that it is a BINARY package", string1, string2);

Name: Anonymous 2006-07-31 13:25

>>7
("%s sucks sure there's an ebuild for %s but it just get dropped to /opt, it's statically linked, and it's CLOSED SOURCE, whitch means that it is a BINARY package", string1, string1);
fixed

Name: __Guido van Rossum__ 2006-08-01 17:48

"%(name)s sucks sure there's an ebuild for %(name)s but it just get dropped to /opt, it's statically linked, and it's CLOSED SOURCE, whitch means that it is a BINARY package" % {"name": string1};
fixed

Name: Anonymous 2006-08-01 17:48

>>9
Shit I left the semicolon

Name: Anonymous 2006-08-01 18:52

>>10

Still valid code.

Name: Anonymous 2006-08-02 9:14

>>11
Yeah but less Pythonic

Name: Anonymous 2009-01-14 13:13

LISP

Name: Anonymous 2009-03-06 9:28

About the place Women   and kids should   stay the fuck   would you take   a remedial course   That regex is   pretty funny Unlike.

Name: Trollbot9000 2009-07-01 11:03


That out vip quality That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality  That was VIP  quality That was  VIP quality That  was VIP quality.

Name: Anonymous 2010-06-27 12:53

ur gay

Name: Anonymous 2010-06-28 10:55

beware the army of 12 year old autistics

Name: Anonymous 2011-02-03 1:13

Name: Anonymous 2011-02-03 2:25

Name: Anonymous 2011-02-03 6:04

Name: Sgt.Kabu䘑갧kimanꗘ䚲 2012-05-28 19:31

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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