Computer Sciences > Gate 2017 Set-2 > Functions
Consider the following function implemented in C:

void printxy(int x, int y)
{
    int *ptr;
    x = 0;
    ptr = &x;
    y = *ptr;
    *ptr = 1;
    printf("%d,%d", x, y);
}


The output of the printxy(1,1) is
A
0,0
B
0,1
C
1,0
D
1,1

Correct : F

Given:
The C function printxy(int x, int y) and the function call printxy(1, 1).
To find:
The output printed by printxy(1, 1).
Solution:
Let me trace the function step-by-step with the initial arguments x = 1 and y = 1.
First, the function sets x = 0, so the value of x is updated to 0.
Next, the pointer ptr is assigned the address of x, so ptr = &x.
Now, y is assigned the value pointed to by ptr, which means y = *ptr = x = 0.
So, at this point, y becomes 0.
Next, the statement *ptr = 1 modifies the memory location pointed to by ptr.
Since ptr points to x, this directly changes x to 1.
Notice that y is a separate variable, so its value remains 0.
Finally, the printf("%d,%d", x, y) statement executes.
Substituting the current values of x and y, it prints: 1,0.
Answer:
(C) 1,0

Similar Questions

The following function computes the maximum value contained in an integer array p[] of size n (n >= 1) int max(int *p, int n) {     int a=0, b=n...
#123 MCQ
Consider the following C program. void f(int, short); void main() {     int i = 100;     short s = 12;     short *p = &s; &...
#144 MCQ
Consider the following program: int f(int *p, int n) {     if (n
#545 Fill in the Blanks

Related Topics

c function question printxy function C GATE CS 2017 Set 2 pointer in C GATE computer science function call output C programming functions variable scope C gate computer sciences 2017

Unique Visitor Count

Total Unique Visitors

Loading......