Consider the following ANSI-C program.
#include <stdio.h>
int main(){
int *ptr, a, b, c;
a=5; b=11; c=20;
ptr=&a; *ptr=c; ptr=&c;
a=*(&b); c=*ptr-a;
printf("%d",c);
return(0);
}The output of this program is . (answer in integer)
Note: Assume that the program compiles and runs successfully.
View worked solution
Answer: 9:9
Let us trace the execution of the program step-by-step:
-
Variable Initialization:
a = 5, b = 11, c = 20
-
Pointer Assignment and Dereferencing:
ptr = &a;: The pointerptrpoints to the variablea.*ptr = c;: The value at the address pointed to byptr(which isa) is updated to the value ofc.
Thus,a = 20.ptr = &c;: The pointerptris updated to point to the variablec.
-
Value Updates:
a = *(&b);: The expression*(&b)evaluates to the value ofb.
Thus,ais updated to11.c = *ptr - a;: Sinceptrpoints toc,*ptrevaluates to the current value ofc(which is20).
Thus,c = 20 - 11 = 9.
-
Output:
printf("%d", c);prints the final value ofc, which is 9.
Free practice · Your answers on this page are not saved.
