Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C putchar() and getchar()

  1. Jul 31, 2008 #1
    Hello.

    So I am trying to write a simple copy program. For whatever reason, the program will never stop running! Even if I directly enter the value of EOF (-1 in my case), it still goes.


    Code (Text):

    #include <stdio.h>

    main()
    {
        int c;
        c = getchar();

        while ( c != EOF)  {
            putchar(c);
            c = getchar();
        }
    }
     
     
  2. jcsd
  3. Jul 31, 2008 #2
    If you're running that in a dos box under Windoze, then EOF is defined as Control Z on standard in.

    Try that.
     
  4. Jul 31, 2008 #3
    I'm using putty and compiling with gcc over ssh. I tried control z and it outputed "suspended" and ended the program, however that was not what I wanted as it did not continue the program after the while loop (if say, I added printf("done\n"); after the loop). Any suggestions?
     
  5. Jul 31, 2008 #4

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    On unix, EOF on a console is ctrl D
    Ctrl Z puts the program into the background, which isn't what you want if it is taking input from stdin.
    To get the program back type 'fg' then kill it with ctrl C
     
  6. Jul 31, 2008 #5
    Wow that worked! Thank you very much mgb_phys! Would someone mind explaining in more detail why that works--I mean, if EOF is simply the integer -1, then why is it that letting c = -1 does not end the file. What does control D actually input into the program?
     
  7. Jul 31, 2008 #6

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Thats what the operating system is for.
    It's job is to read a file off disk and present it to the program a byte at a time, or in your case, read the keyboard convert it into ascii characters and send them to your program You obviously need a way of signalling that you have finished entering your text. One way would be to have a special key 'end' on the keyboard but Unix was designed to work on many different machines with different keyboards so uses ctrl+letter for all these extra characters.

    This is also the reason getchar() returns an int not a char. If it only handled chars there wouldn't be any room for the extra commands like EOF.

    ps Don't rely on EOF being -1, it can be anything the library and OS choose, always compare it to EOF.

    pps. I didn't want to confuse things before, but the more common way to write the code would be:
    while ( (c=getchar()) != EOF ) {
    putchar(c);
    }
    Then you don't need the initial getchar() outside the loop. This is a common source of bugs, if it gets removed or other code gets called between it and the test.
     
    Last edited: Jul 31, 2008
  8. Jul 31, 2008 #7

    KTC

    User Avatar

    Or
    Code (Text):
    while ( (c=getchar()) != EOF )  {
        putchar(c);
    }
     
  9. Jul 31, 2008 #8

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Thanks KTC, I fixed the typo.
     
  10. Jul 31, 2008 #9
    Interesting. Thank you very much for the help.
     
  11. Jul 31, 2008 #10

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Stick at Ja4Coltrane. Starting at this level is important if you ever want to really understand computers whatever language you use.
    Just hooking the Java input thing to the Java output thing and not knowing what is actually happening only gets you so far.
     
  12. Dec 5, 2009 #11
    genious.man..........mgb_phys
     
  13. May 1, 2010 #12
    Ja4Coltrane, your original post helped me finish one of my comp sci. projects!

    thanks for your post!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C putchar() and getchar()
  1. Getchar() loop (Replies: 8)

  2. Get and putchar in c (Replies: 19)

  3. C or C++? (Replies: 8)

Loading...