some C puzzles

Discussion in 'C Programming' started by madhav_a_kelkar, Sep 1, 2004.

  1. I was going through the following code:

    The following is a piece of C code, whose intention was to print a
    minus sign 20 times. But you can notice that, it doesn't work.

    #include <stdio.h>
    int main()
    {
    int i;
    int n = 20;
    for( i = 0; i < n; i-- )
    printf("-");
    return 0;
    }

    Well fixing the above code is straight-forward. To make the problem
    interesting, you have to fix the above code, by changing exactly one
    character. There are three known solutions. See if you can get all
    those three.



    one sol is to use n-- instead of i, so it will print minus sign 20
    times.
     
    madhav_a_kelkar, Sep 1, 2004
    #1
    1. Advertisements

  2. madhav_a_kelkar

    Dan Pop Guest

    This is not possible, in general, because the code must output a newline
    character after the last minus sign (otherwise it might not produce any
    output at all) and this cannot be achieved by changing exactly one
    character.

    Dan
     
    Dan Pop, Sep 1, 2004
    #2
    1. Advertisements

  3. madhav_a_kelkar

    Ash Guest

    1. we can change i-- to i++
    2. we can change i-- to n--
    3. ??
     
    Ash, Sep 2, 2004
    #3
  4. madhav_a_kelkar

    Flash Gordon Guest

    Please don't top post. Look at the posts within the last day or so for
    the arguments against it.

    On 2 Sep 2004 01:21:16 -0700
    That is changing 2 characters.
    Neither of your changes causes a new line to be output before the
    program terminates. As Dan correctly stated (and he is generally correct
    even if some find him abrasive) programs must output a newline before
    terminating if they want the last line to be guaranteed to be output.
     
    Flash Gordon, Sep 2, 2004
    #4
  5. madhav_a_kelkar

    Paul Guest

    simple one...
    for( i = 0; i + n; i-- )

    for( i = 0; -i < n; i-- )

    for( i = 0; i < n; n-- )

    for( i = 0; ~i < n; i-- )

    4 is enough, but why do you want this ;-)

    -Paul.
     
    Paul, Sep 2, 2004
    #5

  6. hi all,
    thanks for ur help. but i can't get what flash has said. when
    the c program exits, all the file handles are closed. this causes them
    to be autoflushed. so, even if we don't explicitly print the newline,
    the code should work. this is what i think.but please clarify me on
    this issue.
    Madhav.
     
    madhav_a_kelkar, Sep 3, 2004
    #6
  7. C99 7.19.2p2 says:

    A text stream is an ordered sequence of characters composed into
    lines, each line consisting of zero or more characters plus a
    terminating new-line character. Whether the last line requires a
    terminating new-line character is implementation-defined.

    C90 7.9.2p2 says the same thing.
     
    Keith Thompson, Sep 3, 2004
    #7
  8. madhav_a_kelkar

    Dan Pop Guest

    Concrete example:

    fangorn:~/tmp 85> zsh
    [fangorn] ~/tmp % prompt='> '
    #include <stdio.h>

    int main()
    {
    printf("Hello world");
    return 0;
    }
    I have explicitly set the prompt myself, to show that no magic was used
    inside to clear the line before displaying the prompting text. Other
    Unix shells behave differently, the last line of program output being
    concatened with the prompting text, giving something like:

    fangorn:~/tmp 86> ./a.out
    Hello worldfangorn:~/tmp 87>

    Dan
     
    Dan Pop, Sep 3, 2004
    #8
  9. What's really happening here (or at least what happened when I tried
    the same thing) is that the program prints "Hello world", and the
    shell then clears the line and writes its prompt in the same location.
    A sufficiently perverse shell could as easily erase the output of
    printf("Hello world\n"); (just as all the output of a console-based
    program is typically erased on Windows unless you take steps to keep
    the window open). As far as the C implementation is concerned, the
    trailing newline is not required; the output appears anyway.

    But it's certainly another good reason to provide a newline at the end
    of your output.
     
    Keith Thompson, Sep 3, 2004
    #9
  10. madhav_a_kelkar

    Old Wolf Guest

    As has been mentioned before (but not on this thread), this
    one doesn't work on 2's complement systems (which is the vast
    majority of all conforming systems).
     
    Old Wolf, Sep 6, 2004
    #10
  11. madhav_a_kelkar

    Dan Pop Guest

    Which, from the user's point of view, is undistinguishable from not having
    the last line displayed at all, as long as stdout is connected to an
    interactive terminal.

    Dan
     
    Dan Pop, Sep 6, 2004
    #11
    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.