the mystery of <ctrl-d>

Discussion in 'C Programming' started by chandanlinster, Apr 29, 2007.

  1. /* if the program is executed as shown below:
    * $./a.out
    * ges<ctrl-d><ctrl-d>
    *
    * OUTPUT:
    * Number of characters = 3
    *
    * Question: What happens to the first and second <ctrl-d>?
    */

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    int j;

    for (j = 0; getchar() != EOF;j++)
    ;
    fprintf(stdout, "Number of characters = %d\n", j);

    exit(EXIT_SUCCESS);
    }
     
    chandanlinster, Apr 29, 2007
    #1
    1. Advertisements

  2. chandanlinster

    Flash Gordon Guest

    chandanlinster wrote, On 29/04/07 11:31:
    <snip correct program for counting characters>

    It's the way your system handles indicating EOF from the keyboard and
    nothing to do with C. For full details you should ask in a group
    dedicated your your system, so a Linux or Unix group probably, but check
    the FAQ first. Thsi simple answer is your system, on seeing ^d^d decided
    to tell your program it was the end of file instead of sending the ^d^d
    to your program.
     
    Flash Gordon, Apr 29, 2007
    #2
    1. Advertisements

  3. chandanlinster

    Bill Pursell Guest

    It seems weird to exit() here instead of simply returning a value...

    Your question isn't really specific to C, but the answer might be:
    the first ^D flushes the readline buffer (ie, your program is
    probably blocking on the first getchar until you hit ^d, at which
    point it receives all three characters. ) When you hit ^d the
    2nd time, there is nothing in the buffer, so an EOF is sent instead.
    In other words, the behavior of typing ^D is described by the
    following
    pseudo-code:
    if (buffer is empty)
    write EOF
    else
    flush buffer
     
    Bill Pursell, Apr 29, 2007
    #3
  4. chandanlinster

    Joe Wright Guest

    What happens to ^D? It is a low-level control and never makes it into
    the character stream. If it is the first thing you type on a new line,
    it may be converted to EOF and presented to the stream.

    How it really works is system specific and you'll have to read your
    documentation. C has no requirements for ^D or ^Z.
     
    Joe Wright, Apr 29, 2007
    #4
  5. [...]

    Why? Within the main() function, "exit(EXIT_SUCCESS)': and
    "return EXIT_SUCCESS;" are (almost) exactly equivalent.
     
    Keith Thompson, Apr 29, 2007
    #5
  6. chandanlinster

    Bill Pursell Guest

    Just because it seems odd. It seems cleaner to return
    than to exit, but there's no reason for that other than
    aesthetics. exit() feels like something that should be
    reserved for an error condition.
     
    Bill Pursell, Apr 29, 2007
    #6
  7. Ok, so it's a matter of style -- and one on which the authors of the
    standard disagree with you, or they wouldn't have included the
    EXIT_SUCCESS macro.
     
    Keith Thompson, Apr 29, 2007
    #7
  8. chandanlinster

    Al Balmer Guest

    There is one reason. It prevents the compiler complaining that the
    function doesn't return a value :)
     
    Al Balmer, Apr 30, 2007
    #8
  9. Bill Pursell said:
    Al Balmer already pointed out that a good compiler will diagnose the
    absence of a return statement from a function declared as returning a
    value.
    I wouldn't dream of using it for that purpose. That's what:

    return EXIT_FAILURE;

    is for. In fact, I can't think of any use whatsoever for exit().
     
    Richard Heathfield, Apr 30, 2007
    #9
  10. terminating the program somewhere other then main().

    As a one -exit-per-funtion person I suspect you wouldn't approve.
    :)
     
    Nick Keighley, Apr 30, 2007
    #10
  11. Nick Keighley said:
    And I can't think of any use whatsoever for that, either.
    I couldn't possibly comment. :)
     
    Richard Heathfield, Apr 30, 2007
    #11
  12. [/QUOTE]
    If C had a reasonable exception mechanism I might agree, but writing
    all your functions to return and check errors is too tedious in cases
    where there's nothing sensible to do about the error other than exit.

    -- Richard
     
    Richard Tobin, Apr 30, 2007
    #12
  13. chandanlinster

    Kevin Handy Guest

    Ok, I admit it! I stole them! I'm really sorry!!

    Look, it's been a really really tough month, and I am seriously
    behind on my ctrl-D's. My boss is chewing on my a**, and I have
    all these TSP reports to do. I didn't think that anyone would
    really miss them, so I took them, all right!
     
    Kevin Handy, Apr 30, 2007
    #13
  14. chandanlinster

    Ben Pfaff Guest

    This is really a Unix question, not a C question. The best
    formulation of the answer (that I'm aware of) was published in
    Kernighan and Pike, _The Unix Programming Environment_, toward
    the beginning of chapter 2, "The File System".
     
    Ben Pfaff, Apr 30, 2007
    #14
  15. CBFalconer said:
    The question seems ambiguous, so I'll answer for each of the two
    alternate meanings that I spotted.

    If you are talking about the run-time system's behaviour after main()
    has terminated, the answer is that I don't care, since as far as I'm
    concerned my job is done when main returns.

    If, on the other hand, you are asking how I terminate a program from
    some user-defined function other than main, the answer is that I don't.

    And if you meant something else, I can't guess what it is.
     
    Richard Heathfield, Apr 30, 2007
    #15
  16. chandanlinster

    Eric Sosman Guest

    The `return(EXIT_FAILURE);' function, of course!
     
    Eric Sosman, May 1, 2007
    #16
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.