Knowee
Questions
Features
Study Tools

What will be the output of the following code?12345678910#include <stdio.h> int main() { int x = 10; int *ptr1 = &x; int *ptr2 = ptr1; (*ptr2)++; printf("%d %d", *ptr1, *ptr2); return 0;}

Question

What will be the output of the following code?12345678910#include <stdio.h> int main() { int x = 10; int *ptr1 = &x; int *ptr2 = ptr1; (*ptr2)++; printf("%d %d", *ptr1, *ptr2); return 0;}

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

Solution

The output of the code will be "11 11".

Here's the step by step explanation:

  1. An integer variable x is declared and initialized with the value 10.

  2. A pointer ptr1 is declared and it is initialized with the address of x. So, ptr1 is pointing to x.

  3. Another pointer ptr2 is declared and it is initialized with the value of ptr1. So, ptr2 is also pointing to x.

  4. The value at the address pointed by ptr2 is incremented by 1. Since ptr2 is pointing to x, the value of x becomes 11.

  5. The printf function prints the value at the address pointed by ptr1 and ptr2. Since both ptr1 and ptr2 are pointing to x, and the value of x is 11, it prints "11 11".

This problem has been solved

Similar Questions

What will be the output of the following code?

What will be the output of the following program?1234567891011121314151617181920#include <stdio.h>void one();void two();void one() { for(int i = 0; i < 2; i++){ two(); }}void two() { printf("TWO ");}int main(){ one(); two(); return 0;}

What will print out when the following code runs?

What is the output for the following code?123456789#include <stdio.h>int main(){ char ch = '1'; for (ch = '5'; ch <= '8'; ch++) putchar(ch); return (0);}

What is the output for the following code?12345678910111213#include <stdio.h> int main() { int i = 1; while (i <= 10) { if (i % 5 == 0) { i++; continue; } printf("%d ", i); i++; } return 0; }

1/4

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.