Skip to content
Official PYQ · 2025

GATE CS 2025 Set 1 Q24 — Programming in C

Explain with AI

 

#include <stdio.h>
void foo(int *p, int x) {
*p=x;
}
int main(){
int *z;
int a = 20, b = 25;
z = &a;
foo(z,b);
printf("%d",a);
return 0;
}

The output of the given C\text{C} program is __________. (Answer in integer)

View worked solution

Answer: 25:25

Let's trace the execution of the program step-by-step:

  1. Variable Initialization:
    • a is initialized to 20.
    • b is initialized to 25.
    • z is a pointer to an integer, and z = &a; makes z point to the memory location of a.
  2. Function Call:
    • The function call foo(z, b); is executed.
    • This passes the pointer z (which is &a) and the value of b (which is 25) to the function foo.
  3. Inside foo(int *p, int x):
    • The parameter p receives the address of a (i.e., p = &a).
    • The parameter x receives the value 25.
    • The statement *p = x; dereferences p and assigns the value of x to the memory location it points to. Since p points to a, this updates the value of a to 25.
  4. Printing the Output:
    • Back in main(), printf("%d", a); prints the current value of a, which is now 25.

Therefore, the output of the program is 25.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions