Hi!
I want to learn C language.
my question is: how dose this while loop wok.

while((c=getChar()) !=EOF)
      if(c>'0' && c<'9')
      nbr++;

How EOF(end of file ) works? EOF=-1;
How can I stop the program?
when I write (-1) I can not stop the program.

Recommended Answers

All 11 Replies

when you write '-1', you do not write the VALUE -1, but the character string { '-', '1' }

your program will not stop, except if you type ctrl+D under linux, or ctrl+Z+enter under windows.

>How EOF(end of file ) works?
EOF is simply a macro designating a negative value. It doesn't have to be -1. You can signal end-of-file from the command line with a control character, usually a combination of ctrl+z for Windows systems or ctrl+d for POSIX systems.

>while((c=getChar()) !=EOF)
Do you mean getchar()? Or is getChar a function that you wrote? Remember that C is case sensitive, so the two are completely different.

>if(c>'0' && c<'9')
Instead of doing this manually, include <ctype.h> and use isdigit. Your test will also exclude '0' and '9', which I imagine is not what you want:

while ( ( c = getchar() ) != EOF ) {
  if ( isdigit ( c ) )
    ++nbr;
}

Hi again!
Thank you very much for your help.
peter

explain how do work EOF

I'm not sure if I'm reading this correctly. Was a post answered in 2005 bumped to 2010 asking the exact same question as the original poster in the same exact words even though the post was already clearly answered? Does this happen often on this forum?

>Was a post answered in 2005 bumped to 2010 asking the exact same question as the
>original poster in the same exact words even though the post was already clearly answered?

Yes.

>Does this happen often on this forum?
Sadly, yes. The quality of Daniweb has been steadily decreasing over the years because new members like that are becoming the norm and driving away clueful members.

Effective moderation could be the solution, assuming that is possible on this forum. I would have deleted that post, issued a warning, and the database would have kept the post back in time where it belonged until someone bumped it with a better contribution or a serious follow up question.

>Effective moderation could be the solution, assuming that is possible on this forum.
Resurrecting a thread is a community faux pas, not a broken rule. Changing the rules to exclude frivolous resurrection while still allowing legitimate continuations of a thread introduces subjectivity where none presently exists. Subjectivity isn't a good thing when it comes to enforcing rules because then members have no clue what's expected of them.

I never said it was breaking any rules, only that a post as silly as "explain how do work EOF" in a thread called "How works EOF" answered in 2005 should be "cleaned up", especially since you respect the quality of DaniWeb.

Regardless, this isn't my business and talking about this on the thread is in itself faux pas as its a complete change of subject.

>I never said it was breaking any rules
"Effective moderation" was the phrase you used, and you clearly stated that the post should be deleted. However, on Daniweb moderators aren't allowed to use their powers except to enforce the rules. If no rule is broken, no action can be taken, so the only way to implement your solution is to introduce a rule that would have been broken in this case.

While you didn't explicitly say it was breaking any rules, the only interpretation is that you believe it should be treated as such.

You seem to like a good debate Narue! And while I could continue to spin a tornado with you on the semantics of our choice of words, the philosophy of moderation and the meaning of enforcement, for the sake of keeping this thread from being raped by our off topic conversation, i'll just say this:

The EOF macro is an integer value that is returned by a number of narrow stream functions to indicate an end-of-file condition, or some other error situation. Usually EOF is defined as -1 but in some C libraries it can be another negative number meaningful to those functions that return it. It is declared in "stdio.h".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.