The GATE Grind

GATE 2015 CS – Question 34

Programming and Data Structures · Programming in C · 1 mark · Numerical answer

The output of the following C program is ________.

void f1(int a, int b) {
    int c;
    c = a; a = b; b = c;
}

void f2(int *a, int *b) {
    int c;
    c = *a; *a = *b; *b = c;
}

int main() {
    int a = 4, b = 5, c = 6;
    f1(a, b);
    f2(&b, &c);
    printf("%d", c - a - b);
}

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: -5

Explanation

`f1` receives copies of `a` and `b`, so it changes nothing in `main`. `f2(&b, &c)` swaps `b` and `c`, so `b` becomes 6 and `c` becomes 5, while `a` stays 4. The program prints $c - a - b = 5 - 4 - 6 = -5$.