Computer Sciences > GATE 2025 SET-1 > Pointers
The output of the given C program is ______ (Answer in integer)

Correct : 25

Explanation:
Let's trace the execution of the program line by line:

1. Variable Initialization inside main():
int a = 20, b = 25; allocates memory for two integer variables, initializing a to 20 and b to 25.
int *z; declares an integer pointer z.
z = &a; stores the memory address of variable a inside pointer z. This means z points directly to a.

2. Function Call: foo(z, b);
The program passes two arguments to the function foo:
• The pointer z (which contains the address of a) is passed by value into the local pointer parameter int *p. Therefore, inside foo, p also points to a.
• The value of b (which is 25) is passed into the local integer parameter int x.

3. Execution inside foo():
*p = x; dereferences the pointer p.
• Because p holds the address of a, modifying *p directly alters the value stored in variable a back in the main function.
• The statement assigns the value of x (25) to a. Thus, the value of a becomes 25.

4. Print Statement inside main():
• After foo(z, b) finishes executing, control returns to main.
printf("%d", a); prints the current value of a, which has been updated to 25.

Similar Questions

Consider the following C program: The output of the above program is ______ (Answer in integer)
#1469 NAT
A palindrome is a word that reads the same forwards and backwards. In a game of words, a player has the following two plates painted with letters. From...
#1 MCQ
Which number does not belong in the series below? 2, 5, 10, 17, 26, 37, 50, 64
#4 MCQ

Related Topics

No tags found

Unique Visitor Count

Total Unique Visitors

Loading......