Thread: New to C and forum, need some help!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    New to C and forum, need some help!

    Hello everyone!

    After completing most of my undergraduate computer science classes in java/c++, I decided that I wanted to teach myself C. After talking to a few profs and consulting the great internet, I settled upon "The C Programming Language 2nd Ed" by K&R, as it comes highly recommended. I've been reading/doing all of the exercises in my spare time, and I've finally come to a problem I just cannot figure out. It's exercise 4-3 for anyone with the book, but here's my code:

    Code:
     
    
    //Ex4_3-10.c
    //Adrian K.
    //2/7/09
    /* This program contains exercises 4-3 through 4-10. I seriously
     * considered skipping this program as the section is about external
     * variables, but i realized the program itself is about organization
     * of programs in c, which is different than any other language i've
     * seen */
    
    #include <stdlib.h>  /* for  atof() */
    #include <stdio.h>
    #include <ctype.h>
    #include <math.h>
    
    #define MAXOP   100  /* max size of operand or operator */
    #define NUMBER  0  /* signal that a number was found */
    #define MAXVAL  100  /* maximum depth of val stack */
    #define BUFSIZE 100 /*max buffer size for ungetch
    
    int get_top(char s[]);
    void push(double f);
    double pop(void);
    
    /* reverse Polish calculator */
    int main(void)
    {
            int type;
            double op2;
            char s[MAXOP];
            while ((type = get_top(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;
                    default:
                            printf("error: unknown command %s\n", s);
                            break;
                    }
            }
            return 0;
    }
    
    int sp = 0;          /* next free stack position */
    double val[MAXVAL];  /* value stack */
    
    /* push:  push f onto value stack */
    void push(double f)
    {
            if (sp < MAXVAL)
                    val[sp++] = f;
            else
                    printf("error: stack full, can't push %g\n", f);
    }
    
    /* pop:  pop and return top value from stack */
    double pop(void)
    {
            if (sp > 0)
                    return val[--sp];
            else {
                    printf("error: stack empty\n");
                    return 0.0;
            }
    }
    
    int getch(void);
    void ungetch(int);
    
    /* getop:  get next character or numeric operand */
    int get_top(char s[])
    {
            int i, c;
            while ((s[0] = c = getch()) == ' ' || c == '\t')
            ;
            s[1] = '\0';
            if (!isdigit(c) && c != '.')
                    return c;      /* not a number */
            i = 0;
            if (isdigit(c))    /* collect integer part */
                    while (isdigit(s[++i] = c = getch()))
                    ;
            if (c == '.')      /* collect fraction part */
                    while (isdigit(s[++i] = c = getch()))
                    ;
            s[i] = '\0';
            if (c != EOF)
                    ungetch(c);
            return NUMBER;
    }
    
    char buf[BUFSIZE];    /* buffer for ungetch */
    int bufp = 0;         /* next free position in buf */
    
    int getch(void)  /* get a (possibly pushed-back) character */
    {
            return (bufp > 0) ? buf[--bufp] : getchar();
    }
    void ungetch(int c)   /* push character back on input */
    {
            if (bufp >= BUFSIZE)
                    printf("ungetch: too many characters\n");
            else
                    buf[bufp++] = c;
    }
    I haven't even started the exercise, I'm just putting together the code provided in the book, and here's the warnings/errors I'm getting:

    Code:
    {cs2:~/test} cc Ex4_3-10.c -o Ex4_3-10.out -lm
    "Ex4_3-10.c", line 30: warning: implicit function declaration: get_top
    "Ex4_3-10.c", line 33: warning: implicit function declaration: push
    "Ex4_3-10.c", line 36: warning: implicit function declaration: pop
    "Ex4_3-10.c", line 68: identifier redeclared: push
            current : function(double) returning void
            previous: function() returning int : "Ex4_3-10.c", line 33
    "Ex4_3-10.c", line 77: identifier redeclared: pop
            current : function(void) returning double
            previous: function() returning int : "Ex4_3-10.c", line 36
    cc: acomp failed for Ex4_3-10.c
    I can't figure out what's wrong with my declaration of push()/pop(), and why nothing is wrong with get_top(). I'm sure it's something really easy but I've been stuck for hours and any help would be greatly appreciated!

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
     #define BUFSIZE 100 /*max buffer size for ungetch */
    Try closing your comment. All should be well then, at least concerning your warnings.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    Oh. My. God. Man I feel dumb. Well thanks for the help, I'm sure I'll be back with an actual question soon!

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You know, sometimes when you have been staring at some code for a while, the most obvious things are not so apparent. To be honest, I started looking at how you declared the functions (on account of the compiler warnings), but the real tip-off was when I opened it up in Emacs, and the function prototypes came up with text colour the same as for comments.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    I prefer writing C in an editor with syntax highlighting for this very reason. That and bold keywords look cool.

Popular pages Recent additions subscribe to a feed