LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2009, 04:14 PM   #1
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
EOF counter in C problem


Hello there,

I am reading a book about C "The C programming language" and I am implementing an example code to count the input characters.

Code:
#include <stdio.h>

/* count characters in input; 1st version */
main()
{
    long nc;
    nc = 0;
    while (getchar() != EOF)
      ++nc;

  printf("%ld\n", nc);
}
The problem is that I don't know how to terminate this program and get the result. I know that the value of EOF = -1, but giving -1 as the input the program takes it as another input and continues to prompt me for the next input.

Any idea how I can terminate this program??

Thanks in advance.
 
Old 11-27-2009, 04:23 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Ctrl+D
 
1 members found this post helpful.
Old 11-27-2009, 04:27 PM   #3
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
That worked! Thank you very much.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
EOF and input redirection in C on OS X problem vdogvictor Programming 11 05-18-2009 09:21 PM
nail.rc/mailx problem, unexpected EOF on smtp connection? trey85stang Linux - Server 1 04-29-2009 07:34 PM
Problem displaying the value of a counter dgiles79 Programming 3 10-14-2008 09:13 PM
Weird, inconsistent Counter-strike problem Micro420 Linux - Games 3 07-10-2004 01:36 PM
Counter-Strike Install Problem Nefarious Linux - Software 9 04-23-2003 12:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration