I'm looking at K&R right now, and I just realized I've been wondering something for quite a while now.
In the functions chapter, they outline a reverse Polish calculator which uses the following code to determine if a number is a digit or not:
Code:
/*main.c*/
#include <stdio.h>
#include <math.h>
#include "calc.h"
#define MAXOP 100
#define NUMBER '0'
int main(void)
{
int type;
double op2;
char s[MAXOP];
while((type = getop(s)) != EOF){
switch(type){
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if(op2 != 0.0)
push(pop() / op2);
else
printf("Error: zero divisor!\n");
break;
case '\n':
printf("\t%.8g\n", pop());
break;
case 'q':
printf("Exiting...\n");
return 0;
default:
printf("Error: unknown command %s.\n", s);
break;
}
}
This isn't all the code, but it's enough to see what I want to know: basically, how does signal that a number has been found? I am not sure how comparing type to NUMBER would ever return true unless type was equal to 0 anyway.