Consider the following C program: #include <stdio.h> #define EOF -1 void…

GATE · 2007 · ITModified — slightly modified from the official paper; see the solution

Consider the following C program:

#include <stdio.h>
#define EOF -1

void push(int); /* push the argument on the stack */
int pop(void); /* pop the top of the stack */
void flagError();

int main()
{
    int c, m, n, r;
    while ((c = getchar()) != EOF)
    {
        if (isdigit(c))
            push(c - '0');
        else if ((c == '+') || (c == '*'))
        {
            m = pop();
            n = pop();
            r = (c == '+') ? n+m : n*m;
            push(r);
        }
        else if (c != ' ')
            flagError();
    }
    printf("%d", pop());
}

What is the output of the program for the following input?

5 2 * 3 3 2 + * +
  1. A.

    15

  2. B.

    25

  3. C.

    30

  4. D.

    150

Attempted by 476 students.

Show answer

Correct answer: B

The worked solution is available to enrolled students.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…