1

I'm new here , asking questions at least. Always have been able to find good answers here. Trying to get back into programming and relearning C but ran into weird issue.

#include <stdio.h>

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

When I run it, after I type in any amount of characters and hit enter, it does not print the value of nc. After hitting enter, I can start typing again and well, same story. Really can't see what could be wrong. The only way it works is if I place both ++nc and the printf within brackets. But then when I hit enter, it gives the value 1-to-nc, which is not what I want. I just want nc. Needless to say the type is not the issue either. Thanks in advance

4
  • What makes you think EOF has anything to do with ENTER? Commented Jun 14, 2013 at 21:23
  • How do I get EOF?, hitting Control and D only gives me ^D. which does nothing Commented Jun 14, 2013 at 21:33
  • What OS are you using? Does Control-D after ENTER work as EOF? Commented Jun 14, 2013 at 21:39
  • Actually I just found Ctrl-Z seems to work as EOF on my system. I'm running windows 7 Commented Jun 14, 2013 at 22:00

4 Answers 4

7

Type Ctrl-D in your terminal to send EOF. You may want

while (getchar() != '\n')

instead if you want it to work with enter.

Sign up to request clarification or add additional context in comments.

3 Comments

That's an infinite loop if getchar returns EOF.
@Jens I know I was saying if he wanted it to work with pressing the enter key instead of EOF.
But how hard is it to fix this bug and make the program deal with real world input, which always has an EOF eventually?
3

You need to hit Ctrl-D to get a EOF.

3 Comments

Control d only seems to give me ^D
found a different solution, but could you explain the ^D pls thanks
I Figured it out, its Ctrl-Z on my system
1

try

while(getchar() != '\n') nc++;

Edit : Assuming the input taken from console, '\n' suffices.

3 Comments

That's an infinite loop if getchar returns EOF.
@Jens: as I am taking input manually from console, i can stop the program by hitting enter.
It's a bug nonetheless. What if someone runs the program with stdin redirected from a file? What if that file is empty?
0

If you just want to get nc printed inside the loop, you should include the print statement in the loop like so:

#include <stdio.h>

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

That prints nc once for each character after hitting [enter], maybe not what you want.

If you want to print nc once per line use scanf and do:

#include <stdio.h>
#include <string.h>

main()
{
    long nc;
    nc = 0;
    char buf[128];
    while (scanf("%s",&buf) > 0) {
      int len = strlen(buf);
      nc += len;
      printf("%ld \n", nc);
    } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.