The output of the following C program is__________. void f1 ( int a, int b) {…
2015
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); }Attempted by 58 students.
Show answer & explanation
Correct answer: -5
Answer: -5
Explanation: The output is -5. Key points:
The function f1 receives its parameters by value, so swapping inside f1 affects only local copies. After calling f1, the variables in main remain a = 4, b = 5, c = 6.
The function f2 receives pointers. Calling f2(&b, &c) swaps the actual values of b and c in main. After f2, b = 6 and c = 5.
Now compute the expression c - a - b using these final values: c = 5, a = 4, b = 6, so c - a - b = 5 - 4 - 6 = -5.
5 - 4 = 1
1 - 6 = -5
A video solution is available for this question — log in and enroll to watch it.