C Output
#include <stdio.h>
int main() {
int x = 5;
int *p;
p = &x;
printf("Value of x: %d\n", x); // Step 1
printf("Address of x: %p\n", &x); // Step 2
printf("Value of p: %p\n", p); // Step 3
printf("Value pointed to by p: %d\n", *p); // Step 4
*p = 10;
printf("New value of x: %d\n", x); // Step 5
return 0;
}
Step 1: Prints the value of variable x
which is 5
.
Step 2: Prints the address of variable x
in memory using the %p
format specifier.
Step 3: Prints the value stored in the pointer p
, which is the address of variable x
.
Step 4: Prints the value stored at the memory location pointed to by p
, which is 5
.
Step 5: Modifies the value of x
indirectly through the pointer p
by assigning 10
to the memory location p
points to. Then, prints the new value of x
, which is 10
.