Re: Token Parser



CBFalconer wrote:
"T.M. Sommers" wrote:
CBFalconer wrote:
"T.M. Sommers" wrote:

Use this instead:

          while ( (c = getchar()) != EOF && isdigit(c) )  {

This was an error in the code I posted earlier.  You don't want
to isdigit(EOF).

This is an error. He wants to find the non-digit operators also. There is no problem passing EOF to isdigit, it will reply 'no'.

You are quite right. I was under the impression that the argument had to be an unsigned char, but the standard also allows EOF.

The error part was that you changed the logic so that it could no longer return the various operators, skipblanks, etc. The other is unnecessary, rather than an error.

I cut and pasted from my own code, which used a different variable for the input character ('c' vice 'next_token'), which was an error, but other than that I don't see any error in the above line (except that one should perhaps guarantee that c can be cast to unsigned char). The loop controlled by the while is over digits after the first, and will stop when and only when c is not a digit. The only thing the EOF test changes is that isdigit() will not be called for EOF, which, as you pointed out, is unnecessary.


--
Thomas M. Sommers -- tms@xxxxxx -- AB2SB

.



Relevant Pages

  • Re: isdigit
    ... The isdigit() function takes an argument ... range of unsigned char or EOF. ... is in the range of unsigned char, ...
    (comp.lang.c)
  • Re: very much new to c need ur help
    ... variable and then compared to EOF does not constitute good advice. ... It may qualify as a quiz or a puzzle (how can you make this code ... unsigned int rdvalue{ ... You are passing an unsigned int to isdigit. ...
    (comp.lang.c)
  • Re: Segfault City
    ... Richard Heathfield wrote: ... of isdigit(), which might reasonably be used in a library targeted at an ... ASCII platform: ... Wouldn't you want to give back a 0 if c is EOF? ...
    (comp.lang.c)
  • isdigit() for characters greater than 127
    ... I read that the argument to isdigit() can be "an integer whose value is ... representable as an unsigned char, or the value of the macro EOF.". ...
    (comp.lang.cpp)
  • Re: isdigit() for characters greater than 127
    ... > representable as an unsigned char, or the value of the macro EOF.". ... > come out as true for isdigit. ... Perhaps you are writing this ...
    (comp.lang.cpp)