What will be the output of the following code?int main() { int a = 10; int *p = &a; *p = 20; printf("%d\n", a); return 0;}
Question
What will be the output of the following code?int main() { int a = 10; int *p = &a; *p = 20; printf("%d\n", a); return 0;}
Solution
The output of the code will be 20.
Here's the step by step explanation:
- An integer variable
ais declared and initialized with the value 10. - A pointer variable
pis declared and it is assigned the address ofa. So,pis now pointing toa. - The value at the address that
pis pointing to (which isa) is changed to 20. This is done using the dereference operator*. So,*p = 20changes the value ofato 20. - The value of
ais printed, which is now 20. - The program returns 0 and ends.
Similar Questions
What will be the output of the following code?int main() { int a = 10; int *p = &a; *p = 20; printf("%d\n", a); return 0;}
What will be the output of the following C code?void main() { int a[] = {1,2,3,4,5}, *p; p = a; ++*p; printf("%d ", *p);p += 2; printf("%d ", *p);}a.24b.22c.23d.34
What will be the output of the following C code?#include <stdio.h>void foo(int*);int main(){int i = 10;foo((&i)++);}void foo(int *p){printf("%d\n", *p);}
Consider the following code. What will be the output?#include <stdio.h>int main() { int arr[3] = {10, 20, 30}; int *p = arr; *(p + 1) = 40; printf("%d\n", arr[1]); return 0;}
What will be the output of the following program?12345678910#include <stdio.h> int main() { int a[10]; *a = 7; *(a+1) = 12; int p = *(a+1) / *a; printf("%d", p); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.