C Programming · Question 21 · Medium

What will be the output of the following C program?

#include <stdio.h>

void f(int *p, int q) {
    *p += q;
    q += *p;
}

int main(void) {
    int x = 2, y = 3;
    f(&x, y);
    printf("%d %d", x, y);
    return 0;
}
  1. 2 3
  2. 2 5
  3. 5 8
  4. 5 3
Answer and explanation

Answer: D. 5 3

Explanation: x is passed by address, so *p += q changes x to 5. y is passed by value, so changing q inside f does not affect y, which stays 3.

Previous: What will be the output of the following C program? · Next: What will be the output of the following C program?

Practise c programming

Try a fresh set of c programming questions with an explanation after every answer, free and without signing up: C Programming practice. Or sit a full placement mock test.

More c programming questions

More technical & programming practice

Java · Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge