Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:54:24+00:00 2026-05-23T02:54:24+00:00

For some reason, when I execute this program with the following expression 12 12

  • 0

For some reason, when I execute this program with the following expression 12 12 * 66 + it returns an invalid and negative answer -46. I’m curious as is to what is going wrong, and would like to fix it asap.

#include <stdio.h>
#include <stdlib.h>
#include "usefunc.h"
#define OFFSET '0'

/*
* I only use one constant.....I really don't need another. and in fact took more
* memory to use the constant than without. several bytes!!
*/

/*

* for this program, I found a library that contained C implementations
* of a stack. I thought I'd use it, since I understand everything it
* does fully. you can quiz me on the stuff I did not write, if you want.

*/

//setting up the stack
typedef char stackElementT;

//it's a struct, so it can contain several elements
//like maxSize, top, and of course the contents
//let's call it stackT
typedef struct {
  stackElementT *contents;
  int maxSize;
  int top;
  int min2;
} stackT;

//allocates the memory for the size passed to the function
//then checks if there's enough memory
//then sets the size to maxSize, `top` to -1 (empty), and contents to newContents
void StackInit(stackT *stackP, int maxSize) {
    stackElementT *newContents;
    newContents = (stackElementT *)malloc(sizeof(stackElementT)*maxSize);
    if (newContents == NULL) {
        fprintf(stderr, "Not enough memory.\n");
        exit(1);
    }

    stackP->contents = newContents;
    stackP->maxSize = maxSize;
    stackP->top = -1; //empty...
}

//frees the allocated memory
//sets it all to NULL
//sets the size to 0, all that good stuff
void StackDestroy(stackT *stackP) {
    free(stackP->contents);
    stackP->contents = NULL;
    stackP->maxSize = 0;
    stackP->top = -1; //empty
}

//just referencing top
int StackIsEmpty(stackT *stackP) {
    return stackP->top < 0;
}

//and top again
int StackIsFull(stackT *stackP) {
    return stackP->top >= stackP->maxSize-1;
}

//and top once more
//adds one to the top of the stack by first incrementing the top, and then adding the element in at the top
//unless of course it's full
void StackPush(stackT *stackP, stackElementT element) {
    if(StackIsFull(stackP)) {
        fprintf(stderr, "Can't push element: stack is full.\n");
        exit(1);
    }
    stackP->contents[++stackP->top] = element;
}
//pops it, and then subsequently updates `top`
//unless of course it's empty
stackElementT StackPop(stackT *stackP) {
    if(StackIsEmpty(stackP)) {
        fprintf(stderr, "Can't pop element: stack is empty.\n");
        exit(1);
    }
    return stackP->contents[stackP->top--];
}

/*

* my work starts here:

*/

double eval(double x, double y, char operand) {
    switch(operand) {
        case '+': return x+y;
        case '-': return x-y;
        case '/': return x/y;
        case '*': return x*y;
        case '^': return pow(x, y);
    }
}

int is_operand(char x) {
    switch(x) {
        case '+':
        case '-':
        case '/':
        case '*':
        case '^': return 1;
        default: return 0;
    }
}

double postfix(char* expr, int length) {
    //counter, and two temporary variables
    int i;
    double temp = 0, temp2;
    //and of course the stack
    stackT stack;
    //lotta frames...
    StackInit(&stack, 1000);
    //loops through string input. as of now, it only works with one digit numbers :(
    for (i = 0; i < length; i++) {
        //printf("%d : %c\n", expr[i], expr[i]);
        //is it a number? yes? push the decimal version of it to the stack
        if ((expr[i] >= '0') && (expr[i] <= '9')) {
            //printf("temp=%f\n", temp);
            temp *= 10;
            temp += expr[i]-OFFSET;
            //printf("temp=%f\n", temp);
        }
        else if (expr[i] == ' ') {
            StackPush(&stack, temp);
            temp = 0;
        }

        //no?
        else {
            //switch-case statement, easy peasy
            switch (expr[i]) {
                //operations checking
                case '+': {
                    temp = StackPop(&stack);
                    temp2 = StackPop(&stack);
                    //printf("%f+%f\n", temp2, temp);
                    StackPush(&stack, temp2+temp);
                    temp = temp2+temp;
                }
                    break;
                case '-': {
                    temp = StackPop(&stack);
                    temp2 = StackPop(&stack);
                    //printf("%f-%f\n", temp2, temp);
                    StackPush(&stack, temp2-temp);
                    temp = temp2-temp;
                }
                    break;
                case '/': {
                    temp = StackPop(&stack);
                    temp2 = StackPop(&stack);
                    //printf("%f/%f\n", temp2, temp);
                    StackPush(&stack, temp2/temp);
                    temp = temp2/temp;
                }
                    break;
                case '*': {
                    temp = StackPop(&stack);
                    temp2 = StackPop(&stack);
                    //printf("%f*%f\n", temp2, temp);
                    StackPush(&stack, temp2*temp);
                    temp = temp2*temp;
                }
                    break;
                //POWERS?!?! whoa. this wasn't required.
                case '^': {
                    temp = StackPop(&stack);
                    temp2 = StackPop(&stack);
                    //printf("%f^%f\n", temp2, temp);
                    StackPush(&stack, pow(temp2, temp));
                    temp = pow(temp2, temp);
                }
                default:
                    break;
            }
        }
    }
    //just giving the number it pushed
    return StackPop(&stack);
}

int main() {
    //so many counters...!
    //almost a shell
    while (1) {
        //prompt
        printf("postfix expression:\t");
        //getLine
        char *expr = GetLine();
        //aaaaaaaaaand evaluate it
        printf("%.0f\n\n", postfix(expr, strlen(expr)));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:54:25+00:00Added an answer on May 23, 2026 at 2:54 am

    The problem is that your stack elements are characters which appear to be signed on your platform, and you are wrapping around SCHAR_MAX.

    (12*12)+66 = 210. 210 in hex, for clarity, is 0xd2. However, interpreting 0xd2 as a twos-complement signed character value gives us -46.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason I never see this done. Is there a reason why not?
For some reason this code executes the parental commands immediately, terminating my semaphores and
For some reason my database is failing to execute a .delete method. I have
For some reason when I attempt to make a request to an Ajax.net web
For some reason when I create a new namespace in Visual Studio 2008 its
For some reason, when I try to install SQL Server 2008 Express , I
For some reason, lately the *.UDL files on many of my client systems are
For some reason, Section 1 works but Section 2 does not. When run in
For some reason beyond me I can't access the mysql server on a machine.
For some reason the Windows command prompt is special in that you have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.