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

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    2

    Character counting increments by 2


    #include <stdio.h>

    /* count characters in input; 1st version */
    main()
    {
    long nc;

    nc = 0;
    while (getchar() != EOF)
    ++nc;
    printf("%ld\n", nc);
    }

    The character is counted as 2,4,6,8,etc.
    Why is this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    8
    After pressing Enter a new-line character is appended. Modify your loop:
    Code:
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF)

    Comments on this post

    • eramit2010 agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    2
    Thanks bro.
    You perfectly cleared my doubt.
    I tried many ways to find this.
    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo