Knowee
Questions
Features
Study Tools

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;}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be 20.

Here's the step by step explanation:

  1. An integer variable a is declared and initialized with the value 10.
  2. A pointer variable p is declared and it is assigned the address of a. So, p is now pointing to a.
  3. The value at the address that p is pointing to (which is a) is changed to 20. This is done using the dereference operator *. So, *p = 20 changes the value of a to 20.
  4. The value of a is printed, which is now 20.
  5. The program returns 0 and ends.

This problem has been solved

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;}

1/3

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.