216k views
4 votes
What is the output of the following code fragments?

int trail(int* a, int b)
{
if (b > *a)
{
*a = b
return -*a;
{
else
{
return 0;
}
}
int x = 0, y= 10,z;
z = trial(&y,x);
printf(""%d%d%d\\"". z,x,y);
A) 10 0 0
B) 0 0 10
C) 0 10 0
D) -10 0 0

1 Answer

2 votes

Final answer:

The output of the given C code fragment is '0 0 10', corresponding to the values of the variables z, x, and y after the execution of the trial function. The correct option is B) 0 0 10.

Step-by-step explanation:

The student is asking about the output of a specific C code fragment involving a function trial and some variables. Let's go through the code step-by-step. The function is executed with trial(&y,x).

We see that within the function trial, it checks if b is greater than the value pointed to by pointer a. If true, it sets the value at that address to b and returns the negation of that value. Since x is 0 and y is 10, the condition b > *a is false, and thus, return 0; is executed.

After calling trial, z is assigned the return value of the function, which we've determined is 0. The values of x and y remain unchanged, since x is passed by value and y is not modified by the function.

The printf statement outputs the values of z, x, and y. Hence, the output would be 0 0 10, and the correct option answer is B) 0 0 10.

User Celso Wellington
by
7.8k points