Thread: strange error

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Location
    London
    Posts
    5
    Rep Power
    0

    strange error


    Hi, after some time I'm trying to return to programming.
    Using 'The C Programming Language' by Kernighan & Ritchie book to study.

    the following simple code works as it should : outputs the input.

    Code:
    main() 
    {
        int c;
        
        c = getchar();
        while (c != EOF)
        {
           putchar(c);
           c = getchar();
        }
    }
    However, when changed to:

    Code:
    main() 
    {
        int c;
        
        c = getchar();
        while ((c = getchar()) != EOF)
        {
           putchar(c);
        }
    }
    The output truncate the first char from the input string.

    Any idea why it is happening?
    Many thanks in advance!
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,792
    Rep Power
    508
    Outstanding book Stick with it.

    get a character
    get the next character
    write that character
    surprise! the first character vanished


    Code:
    #include <stdio.h>
    
    int main() 
    {
        int c;
        
        c = getchar();  /* c is the first character of stdin */
    
        while ((c = getchar()) != EOF) /* replace c by the next character of stdin */
        {
          putchar(c);  /* write a character */
        }
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Location
    London
    Posts
    5
    Rep Power
    0
    So I heard about the book, really happy I've got it. Thanks mate!

    But why is it happening?
    BTW, the error stops occurring after the fist copy!
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,792
    Rep Power
    508
    The program essentially takes this action because of the precedence rules. Please observe that the first character is never written.


    read first character


    read second character
    If the second character is EOF, terminate
    write second character


    read third character
    If the third character is EOF, terminate
    write third character
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Location
    London
    Posts
    5
    Rep Power
    0
    Thank you for taking your time to explain it so well!!!

    Wow it is so simple after you've explained it.

    I guess it is lack of my experience of 'rolling' the code in my head, I need to practice that.

    Thank you again!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Location
    London
    Posts
    5
    Rep Power
    0
    ...and I fixed it based on your explanation:


    int main()
    {
    int c;

    c = getchar();
    putchar(c);
    while ((c = getchar()) != EOF)
    {
    putchar(c);
    }
    }
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Location
    London
    Posts
    5
    Rep Power
    0
    or even:

    Code:
    int main() 
    {
        int c;
        
        c = getchar();
        putchar(c);
        while ((c = getchar()) != EOF)
        {
          putchar(c);
        }
    }


    following the guideline in your signature ;)
    Sorry for spamming, but this looks so much better and easy to read
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    95
    Rep Power
    42
    You don't actually need to do the getchar+putchar before the loop. That is, you can remove lines 5 and 6 and there will be no difference in behavior except one:

    Currently when reading from an empty stream (i.e. the first call to getchar already returns EOF) you will print the EOF value and then call getchar again. If you remove the lines, you will instead handle an EOF at the beginning just like an EOF anywhere else and stop printing and reading right there, which is the more correct behavior.

IMN logo majestic logo threadwatch logo seochat tools logo