What is if (c != EOF) used for in reverse Polish calculater -
i'm learning c programming language.
in below code
#include <ctype.h> int getch(void); void ungetch(int); int getop(char s[]) { int i, c; while((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; if(!isdigit(c) && c != '.') return c; = 0; if (isdigit(c)) while (isdigit(s[++i] = c = getch())) ; if(c == '.') while (isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if(c != eof) ungetch(c); return number; } #define bufsize 100 char buf[bufsize]; int bufp = 0; int getch(void) { return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetch(int c) { if (bufp >= bufsize) printf("ungetch: many characters\n"); else buf[bufp++] = c; }
i think statement if(c != eof)
in getop()
unnecessary.
in standard input, each line consists of 0 or more characters followed newline character.
when statement executes,what c has fetched follows digit character or .
,in situation c newline character or other characters except eof
.
it clear c
not eof
without testing.
what if(c != eof)
used for?
sorry if trivial question. in advance.
in standard input, each line consists of 0 or more characters followed newline character.
not necessarily; input arbitrary byte stream , not have end newline character.
when statement executes,what c has fetched follows digit character or .,in situation c newline character or other characters except eof.
it eof, also.
it clear c not eof without testing.
no isn't; in fact, clear c might eof.
what if(c != eof) used for?
to prevent ungetch(eof), undefined because eof can't crammed buf.
p.s. program has undefined behavior if read eof, or if input stream contains values outside of positive part of range of char ... argument isdigit should int, it's char here.
Comments
Post a Comment