Very confused

This is a discussion on Very confused within the C Programming forums, part of the General Programming Boards category; I'm looking at K&R right now, and I just realized I've been wondering something for quite a while now. In ...

  1. #1
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115

    Very confused

    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
    Code:
    define NUMBER 100
    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.
    I live in a giant bucket.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    If you assume that getop() reads in a string, and returns either an operator (eg '+') or the value which is in NUMBER, depending on what was typed in.

    In short, look at the code for getop(), or read about it if it's some predefined function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    Oh, right... I totally missed that somehow. Prolly because I've been trying to read this book straight through without writing any code to practice, because I can't make Linux work properly at the moment.

    On that subject, anyone know where I can get shell/compiler access, without HUGE(>1 second) command delays?
    Last edited by Aerie; 01-23-2005 at 11:14 AM.
    I live in a giant bucket.

  4. #4
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>anyone know where I can get shell/compiler access, without
    >>HUGE(>1 second) command delays?
    You mean from a C program? Calling a system command processor will always be slower than you'd like because it has to make external requests to the environment. However, you may find that nonportable functions like exec* or spawn* are faster at it than the standard system function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 03:04 PM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 02:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21