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

C programming

  1. Oct 12, 2011 #1
    Please explain what this C code will do :

    #include <stdio.h>
    /* count characters in input; 1st version */
    main()
    {
    long nc;
    nc = 0;
    while (getchar() != EOF)
    ++nc;
    printf("%ld\n", nc);
    }

    'The C Programming' Book says that this code counts characters but it doesn't do the same ... why ?
    Thanks
     
  2. jcsd
  3. Oct 12, 2011 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    The code should do what it says it will do. You need to be more specific about what your problem is.
     
  4. Oct 12, 2011 #3

    Borek

    User Avatar

    Staff: Mentor

    I assume you have compiled it and you have seen numbers printed by the program. What were they?

    Place your programs in [noparse]
    Code (Text):
     
    [/noparse] tags to make them easier to read:

    Code (Text):
    #include <stdio.h>
    /* count characters in input; 1st version */
    main()
    {
      long nc;
      nc = 0;
      while (getchar() != EOF)
        ++nc;
      printf("%ld\n", nc);
    }
     
  5. Oct 12, 2011 #4

    jtbell

    User Avatar

    Staff: Mentor

    Please tell us:

    1. a sample input to the program
    2. what you expected the output to be
    3. the actual output

    This is more precise and helpful for us, than a purely verbal description of your problem.
     
  6. Oct 12, 2011 #5
    When I type in anything, say, any number or any character, it just displays the character or whatever once again in the next line. I expect the program to count the number of characters I have typed. Like :

    If I type 'Physics', it must count the number of characters(7) in the word Physics. But it does do that....
     
  7. Oct 12, 2011 #6

    uart

    User Avatar
    Science Advisor

    Try typing some characters and ending it with control-z.
     
  8. Oct 12, 2011 #7

    Borek

    User Avatar

    Staff: Mentor

    It'll do, but you have to finish the input with EOF - end of file marker.

    Under Linux (or any other *nix) it means entering your text, then pressing Ctrl-D. I think in Windows command line it'll be Ctrl-Z.

    Edit: uart beat me. That's what happens when you google to check if it really is Ctrl-Z.
     
  9. Oct 14, 2011 #8
    For Windows , pressing f6 gives the same result (EOF marker).
     
  10. Dec 14, 2011 #9
    You can test what character is the program expect:
    Code (Text):

    main()
    {
    long nc;
    nc = 0;

    printf("%c\n",EOF);   //Here output nothing

    while (getchar() != EOF)
    ++nc;
    printf("%ld\n", nc);     //Never be executed
    }
     
    As we saw output EOF is nothing,so none of inputs would be terminated while loop.
    getchar() accept a character,which can be present as an ASCII code(One byte,Range:0~255).
    But,EOF means of "End Of File",the value is -1.which used to judge when reading a file.

    So,this program could be one part of file reading function,and exactly counts characters in file.
     
  11. Dec 14, 2011 #10
    I`m wrong,Ctrl-Z(or F6) worked.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C programming
  1. C Program (Replies: 5)

  2. C program (Replies: 4)

  3. C program (Replies: 4)

  4. C Program (Replies: 3)

  5. C program (Replies: 0)

Loading...