GATE CS 2017 Set 1 — Question 44

Numerical answer 2 marks Question 44 2017

Question 44

NAT 2 marks · no negative Programming and Data Structures

Consider the following C program:

#include <stdio.h>

int *A, stkTop;

int stkFunc(int opcode, int val) {
    static int size = 0, stkTop = 0;
    switch (opcode) {
        case -1: size = val; break;
        case  0: if (stkTop < size) A[stkTop++] = val; break;
        default: if (stkTop) return A[--stkTop];
    }
    return -1;
}

int main() {
    int B[20];
    A = B;
    stkTop = -1;
    stkFunc(-1, 10);
    stkFunc(0, 5);
    stkFunc(0, 10);
    printf("%d\n", stkFunc(1, 0) + stkFunc(1, 0));
    return 0;
}

The value printed by the program is ______.

Answers and explanations are free — they just need an account.

Where this question comes from

Source: GATE 2017 Computer Science and Information Technology, Set 1, Q44 (set and question number approximate)