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.

Dani AI

Generated

Brief clarification and practical tips that build on what and have already said. The loop in the original post stops only when the input-reading function signals the stream has ended (EOF) or when the program explicitly breaks. Typing the characters - and 1 into a terminal does not automatically end the input stream; it supplies characters. Two reliable ways to stop the loop are to (a) let the stream actually end (file redirection or a terminal EOF key sequence) or (b) make the program look for a sentinel input (for example the text "-1") and break when it appears.

A couple of common mistakes and how to avoid them: do not store the return value of getchar/fgetc in a plain char variable (those functions return int so they can return the EOF sentinel). Also, when passing characters to ctype.h functions, cast to unsigned char to avoid undefined behavior for negative values. Example of a robust single-character loop:

int ch;
while ((ch = fgetc(stdin)) != EOF) {
    if (isalpha((unsigned char)ch)) {
        /* process letter */
    }
}

If the goal is to let the user type -1 and have the program stop, read lines and test for that sentinel instead of expecting an EOF. Reading lines also avoids terminal buffering surprises and makes parsing safer:

char buf[64];
while (fgets(buf, sizeof buf, stdin)) {
    char *endptr;
    long v = strtol(buf, &endptr, 10);
    if (endptr != buf && (*endptr == '\n' || *endptr == '\0') && v == -1) break;
    /* otherwise process the input line */
}

Quick tips: test EOF behavior by redirecting input from a file (program < input.txt) to confirm the loop exits at real end-of-file; after a read loop use feof/ferror to distinguish EOF from an I/O error; prefer sentinel-line parsing for interactive programs that should stop on a typed command.

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.